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

[SDK-1924] client methods should handle partially filled arguments #561

Merged
merged 2 commits into from
Aug 27, 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
22 changes: 21 additions & 1 deletion __tests__/Auth0Client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ const setup = (
exp: Date.now() / 1000 + 86400
},
claims
)
),
user: {
sub: 'me'
}
});

return auth0;
Expand Down Expand Up @@ -192,6 +195,23 @@ describe('Auth0Client', () => {
});
});

it('should log the user in and get the user', async () => {
const auth0 = setup({ scope: 'foo' });
await login(auth0);
expect(await auth0.getUser()).toBeTruthy();
expect(await auth0.getUser({})).toBeTruthy();
expect(await auth0.getUser({ audience: 'default' })).toBeTruthy();
expect(await auth0.getUser({ scope: 'foo' })).toBeTruthy();
expect(await auth0.getUser({ audience: 'invalid' })).toBeUndefined();
expect(await auth0.getIdTokenClaims()).toBeTruthy();
expect(await auth0.getIdTokenClaims({})).toBeTruthy();
expect(await auth0.getIdTokenClaims({ audience: 'default' })).toBeTruthy();
expect(await auth0.getIdTokenClaims({ scope: 'foo' })).toBeTruthy();
expect(
await auth0.getIdTokenClaims({ audience: 'invalid' })
).toBeUndefined();
});

it('should log the user in with custom auth0Client', async () => {
const auth0Client = { name: '__test_client__', version: '0.0.0' };
const auth0 = setup({ auth0Client });
Expand Down
26 changes: 26 additions & 0 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2064,6 +2064,32 @@ describe('Auth0', () => {
const token = await auth0.getTokenWithPopup();
expect(token).toBe(TEST_ACCESS_TOKEN);
});

it('accepts empty options and config', async () => {
const { auth0 } = await localSetup({ audience: 'foo' });

await auth0.getTokenWithPopup();
expect(auth0.loginWithPopup).toHaveBeenCalledWith(
{
audience: 'foo',
scope: 'openid profile email'
},
{ timeoutInSeconds: 60 }
);
});

it('accepts partial options and config', async () => {
const { auth0 } = await localSetup({ audience: 'foo' });

await auth0.getTokenWithPopup({ scope: 'bar' }, { popup: 'baz' });
expect(auth0.loginWithPopup).toHaveBeenCalledWith(
{
audience: 'foo',
scope: 'openid profile email bar'
},
{ timeoutInSeconds: 60, popup: 'baz' }
);
});
});

describe('logout()', () => {
Expand Down
44 changes: 19 additions & 25 deletions src/Auth0Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ export default class Auth0Client {
* otherwise the popup will be blocked in most browsers.
*
* @param options
* @param config
*/
public async loginWithPopup(
options: PopupLoginOptions = {},
Expand Down Expand Up @@ -379,17 +380,14 @@ export default class Auth0Client {
*
* @param options
*/
public async getUser(
options: GetUserOptions = {
audience: this.options.audience || 'default',
scope: this.scope || this.defaultScope
}
) {
options.scope = getUniqueScopes(this.defaultScope, options.scope);
public async getUser(options: GetUserOptions = {}) {
const audience = options.audience || this.options.audience || 'default';
const scope = getUniqueScopes(this.defaultScope, this.scope, options.scope);

const cache = this.cache.get({
client_id: this.options.client_id,
...options
audience,
scope
});

return cache && cache.decodedToken && cache.decodedToken.user;
Expand All @@ -404,21 +402,14 @@ export default class Auth0Client {
*
* @param options
*/
public async getIdTokenClaims(
options: GetIdTokenClaimsOptions = {
audience: this.options.audience || 'default',
scope: this.scope || this.defaultScope
}
) {
options.scope = getUniqueScopes(
this.defaultScope,
this.scope,
options.scope
);
public async getIdTokenClaims(options: GetIdTokenClaimsOptions = {}) {
const audience = options.audience || this.options.audience || 'default';
const scope = getUniqueScopes(this.defaultScope, this.scope, options.scope);

const cache = this.cache.get({
client_id: this.options.client_id,
...options
audience,
scope
});

return cache && cache.decodedToken && cache.decodedToken.claims;
Expand Down Expand Up @@ -643,19 +634,22 @@ export default class Auth0Client {
* results will be valid according to their expiration times.
*
* @param options
* @param config
*/
public async getTokenWithPopup(
options: GetTokenWithPopupOptions = {
audience: this.options.audience,
scope: this.scope || this.defaultScope
},
config: PopupConfigOptions = DEFAULT_POPUP_CONFIG_OPTIONS
options: GetTokenWithPopupOptions = {},
config: PopupConfigOptions = {}
) {
options.audience = options.audience || this.options.audience;
options.scope = getUniqueScopes(
this.defaultScope,
this.scope,
options.scope
);
config = {
...DEFAULT_POPUP_CONFIG_OPTIONS,
...config
};

await this.loginWithPopup(options, config);

Expand Down
8 changes: 4 additions & 4 deletions src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,22 +201,22 @@ export interface GetUserOptions {
/**
* The scope that was used in the authentication request
*/
scope: string;
scope?: string;
/**
* The audience that was used in the authentication request
*/
audience: string;
audience?: string;
}

export interface GetIdTokenClaimsOptions {
/**
* The scope that was used in the authentication request
*/
scope: string;
scope?: string;
/**
* The audience that was used in the authentication request
*/
audience: string;
audience?: string;
}

/*
Expand Down