Skip to content

Commit

Permalink
Add scope option to vue and sdkcore
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeLo123 committed Apr 25, 2024
1 parent a669565 commit d5ce207
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 17 deletions.
5 changes: 5 additions & 0 deletions packages/core/src/SDKConfig/SDKConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export interface SDKConfig {
*/
redirectUri: string;

/**
* The OAuth2 scope parameter passed to the `/oauth2/authorize` endpoint. If not specified fusionauth will default this to `openid offline_access`.
*/
scope?: string;

/**
* The redirect URI for post-logout. Defaults the provided `redirectUri`.
*/
Expand Down
12 changes: 11 additions & 1 deletion packages/core/src/SDKCore/SDKCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ export class SDKCore {

constructor(config: SDKConfig) {
this.config = config;
this.urlHelper = UrlHelper.fromSDKConfig(config);
this.urlHelper = new UrlHelper({
serverUrl: config.serverUrl,
clientId: config.clientId,
redirectUri: config.redirectUri,
scope: config.scope,
mePath: config.mePath,
loginPath: config.loginPath,
registerPath: config.registerPath,
logoutPath: config.logoutPath,
tokenRefreshPath: config.tokenRefreshPath,
});
this.tokenRefresher = new TokenRefresher(
this.urlHelper.getTokenRefreshUrl(),
);
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/UrlHelper/UrlHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('UrlHelper', () => {
serverUrl: 'http://my-server',
clientId: 'abc123',
redirectUri: 'http://my-client',
scope: 'openid email profile offline_access',
};

const urlHelper = new UrlHelper(config);
Expand All @@ -25,6 +26,7 @@ describe('UrlHelper', () => {
expect(loginUrl.pathname).toBe('/app/login');
expect(loginUrl.searchParams.get('client_id')).toBe(config.clientId);
expect(loginUrl.searchParams.get('redirect_uri')).toBe(config.redirectUri);
expect(loginUrl.searchParams.get('scope')).toBe(config.scope);
expect(loginUrl.searchParams.get('state')).toBe(stateValue);
});

Expand All @@ -37,6 +39,7 @@ describe('UrlHelper', () => {
expect(registerUrl.searchParams.get('redirect_uri')).toBe(
config.redirectUri,
);
expect(registerUrl.searchParams.get('scope')).toBe(config.scope);
expect(registerUrl.searchParams.get('state')).toBe(stateValue);
});

Expand Down
20 changes: 4 additions & 16 deletions packages/core/src/UrlHelper/UrlHelper.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { SDKConfig } from '#/SDKConfig';

import { UrlHelperConfig, UrlHelperQueryParams } from './UrlHelperTypes';

/** A class responsible for generating URLs that FusionAuth SDKs interact with. */
export class UrlHelper {
serverUrl: string;
clientId: string;
redirectUri: string;
scope?: string;

mePath: string;
loginPath: string;
Expand All @@ -18,6 +17,7 @@ export class UrlHelper {
this.serverUrl = config.serverUrl;
this.clientId = config.clientId;
this.redirectUri = config.redirectUri;
this.scope = config.scope;

this.mePath = config.mePath ?? '/app/me';
this.loginPath = config.loginPath ?? '/app/login';
Expand All @@ -34,6 +34,7 @@ export class UrlHelper {
return this.generateUrl(this.loginPath, {
client_id: this.clientId,
redirect_uri: this.redirectUri,
scope: this.scope,
state,
});
}
Expand All @@ -42,6 +43,7 @@ export class UrlHelper {
return this.generateUrl(this.registerPath, {
client_id: this.clientId,
redirect_uri: this.redirectUri,
scope: this.scope,
state,
});
}
Expand Down Expand Up @@ -84,18 +86,4 @@ export class UrlHelper {

return urlSearchParams;
}

/** A convenience method to instantiate from SDKConfig instead of picking off the needed properties. */
static fromSDKConfig(config: SDKConfig) {
return new UrlHelper({
serverUrl: config.serverUrl,
clientId: config.clientId,
redirectUri: config.redirectUri,
mePath: config.mePath,
loginPath: config.loginPath,
registerPath: config.registerPath,
logoutPath: config.logoutPath,
tokenRefreshPath: config.tokenRefreshPath,
});
}
}
2 changes: 2 additions & 0 deletions packages/core/src/UrlHelper/UrlHelperTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ export type UrlHelperConfig = Pick<
| 'registerPath'
| 'logoutPath'
| 'tokenRefreshPath'
| 'scope'
>;

/** The query params associated with URLs generated by the UrlHelper class. */
export type UrlHelperQueryParams = {
client_id: string;
redirect_uri?: string;
post_logout_redirect_uri?: string;
scope?: string;
state?: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const config: FusionAuthConfig = {
clientId: '85a03867-dccf-4882-adde-1a79aeec50df',
serverUrl: 'http://localhost:9000',
redirectUri: 'http://localhost',
scope: 'openid offline_access',
};

describe('createFusionAuth', () => {
Expand Down Expand Up @@ -112,6 +113,7 @@ describe('createFusionAuth', () => {
expectedUrl.pathname = '/app/login';
expectedUrl.searchParams.set('client_id', config.clientId);
expectedUrl.searchParams.set('redirect_uri', config.redirectUri);
expectedUrl.searchParams.set('scope', config.scope);
expectedUrl.searchParams.set('state', stateValue);

expect(mockedLocation.assign).toHaveBeenCalledWith(expectedUrl);
Expand All @@ -128,6 +130,7 @@ describe('createFusionAuth', () => {
expectedUrl.pathname = '/app/register';
expectedUrl.searchParams.set('client_id', config.clientId);
expectedUrl.searchParams.set('redirect_uri', config.redirectUri);
expectedUrl.searchParams.set('scope', config.scope);
expectedUrl.searchParams.set('state', stateValue);

expect(mockedLocation.assign).toHaveBeenCalledWith(expectedUrl);
Expand Down
5 changes: 5 additions & 0 deletions packages/sdk-vue/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export interface FusionAuthConfig {
*/
redirectUri: string;

/**
* The OAuth2 scope parameter passed to the `/oauth2/authorize` endpoint. If not specified fusionauth will default this to `openid offline_access`.
*/
scope?: string;

/**
* Enables automatic token refreshing. Defaults to false.
*/
Expand Down

0 comments on commit d5ce207

Please sign in to comment.