Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logoutByIssuer method #13

Merged
merged 1 commit into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/admin-sdk/modules/token/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
12 changes: 12 additions & 0 deletions src/admin-sdk/modules/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
}
10 changes: 10 additions & 0 deletions test/spec/modules/token/getIssuer.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
42 changes: 42 additions & 0 deletions test/spec/modules/users/logoutByIssuer.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
2 changes: 1 addition & 1 deletion test/spec/modules/users/logoutByPublicAddress.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down