From 6f71a53d8ffcea4e05607728c7ac2fb371496dd4 Mon Sep 17 00:00:00 2001 From: Ian K Smith Date: Mon, 16 Mar 2020 20:30:02 -0600 Subject: [PATCH] Add logoutByIssuer method --- src/admin-sdk/modules/token/index.ts | 4 ++ src/admin-sdk/modules/users/index.ts | 12 ++++++ test/spec/modules/token/getIssuer.spec.ts | 10 +++++ .../spec/modules/users/logoutByIssuer.spec.ts | 42 +++++++++++++++++++ .../users/logoutByPublicAddress.spec.ts | 2 +- 5 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 test/spec/modules/token/getIssuer.spec.ts create mode 100644 test/spec/modules/users/logoutByIssuer.spec.ts diff --git a/src/admin-sdk/modules/token/index.ts b/src/admin-sdk/modules/token/index.ts index 85afcff..a3f8545 100644 --- a/src/admin-sdk/modules/token/index.ts +++ b/src/admin-sdk/modules/token/index.ts @@ -68,4 +68,8 @@ export class TokenModule extends BaseModule { const claimedIssuer = claim.iss.split(':')[2]; return claimedIssuer; } + + public getIssuer(DIDToken: string): string { + return this.decode(DIDToken)[1].iss; + } } diff --git a/src/admin-sdk/modules/users/index.ts b/src/admin-sdk/modules/users/index.ts index 5b62916..c6e4327 100644 --- a/src/admin-sdk/modules/users/index.ts +++ b/src/admin-sdk/modules/users/index.ts @@ -27,4 +27,16 @@ export class UsersModule extends BaseModule { body, }); } + + public async logoutByIssuer(issuer: string) { + if (!this.sdk.secretApiKey) throw createApiKeyMissingError(); + + const body = JSON.stringify({ publicaddress: issuer.split(':')[2] }); + + return fetch(`${this.sdk.apiBaseUrl}/v1/admin/auth/user/logout`, { + method: 'POST', + headers: { 'X-Magic-Secret-key': this.sdk.secretApiKey }, + body, + }); + } } diff --git a/test/spec/modules/token/getIssuer.spec.ts b/test/spec/modules/token/getIssuer.spec.ts new file mode 100644 index 0000000..96c7d73 --- /dev/null +++ b/test/spec/modules/token/getIssuer.spec.ts @@ -0,0 +1,10 @@ +import test from 'ava'; +import { createMagicAdminSDK } from '../../../lib/factories'; +import { VALID_DIDT, VALID_DIDT_PARSED_CLAIMS } from '../../../lib/constants'; + +test('#01: Successfully gets issuer from DIDT', t => { + const sdk = createMagicAdminSDK(); + const result = sdk.token.getIssuer(VALID_DIDT); + const expected = VALID_DIDT_PARSED_CLAIMS.iss; + t.is(result, expected); +}); diff --git a/test/spec/modules/users/logoutByIssuer.spec.ts b/test/spec/modules/users/logoutByIssuer.spec.ts new file mode 100644 index 0000000..aaba249 --- /dev/null +++ b/test/spec/modules/users/logoutByIssuer.spec.ts @@ -0,0 +1,42 @@ +import test from 'ava'; +import sinon from 'sinon'; +import fetch from 'node-fetch'; +import { createMagicAdminSDK } from '../../../lib/factories'; +import { VALID_DIDT, API_KEY } from '../../../lib/constants'; +import { createApiKeyMissingError, MagicAdminSDKError } from '../../../../src/admin-sdk/core/sdk-exceptions'; + +test('#01: Successfully POSTs to logout endpoint via DIDT', async t => { + const sdk = createMagicAdminSDK('https://example.com'); + + const fetchStub = sinon.stub(); + (fetch as any) = fetchStub; + + await t.notThrowsAsync(sdk.users.logoutByIssuer('did:ethr:0x1234')); + + const fetchArguments = fetchStub.args[0]; + t.deepEqual(fetchArguments, [ + 'https://example.com/v1/admin/auth/user/logout', + { + method: 'POST', + headers: { 'X-Magic-Secret-key': API_KEY }, + body: '{"publicaddress":"0x1234"}', + }, + ]); +}); + +test('#02: Fails POST if API key is missing', async t => { + const sdk = createMagicAdminSDK('https://example.com'); + + const fetchStub = sinon.stub(); + (fetch as any) = fetchStub; + + const expectedError = createApiKeyMissingError(); + + (sdk as any).secretApiKey = undefined; + + const error: MagicAdminSDKError = await t.throwsAsync(sdk.users.logoutByIssuer('did:ethr:0x1234')); + + t.false(fetchStub.called); + t.is(error.code, expectedError.code); + t.is(error.message, expectedError.message); +}); diff --git a/test/spec/modules/users/logoutByPublicAddress.spec.ts b/test/spec/modules/users/logoutByPublicAddress.spec.ts index e6b0a71..2df678f 100644 --- a/test/spec/modules/users/logoutByPublicAddress.spec.ts +++ b/test/spec/modules/users/logoutByPublicAddress.spec.ts @@ -34,7 +34,7 @@ test('#02: Fails POST if API key is missing', async t => { (sdk as any).secretApiKey = undefined; - const error: MagicAdminSDKError = await t.throwsAsync(sdk.users.logoutByPublicAddress(VALID_DIDT)); + const error: MagicAdminSDKError = await t.throwsAsync(sdk.users.logoutByPublicAddress('0x0123')); t.false(fetchStub.called); t.is(error.code, expectedError.code);