packages/core: configurable scope join for DefaultAuthConnector
This commit is contained in:
+21
@@ -122,4 +122,25 @@ describe('DefaultAuthConnector', () => {
|
||||
expiresAt: expect.any(Date),
|
||||
});
|
||||
});
|
||||
|
||||
it('should use join func to join scopes', async () => {
|
||||
const mockOauth = new MockOAuthApi();
|
||||
const popupSpy = jest
|
||||
.spyOn(loginPopup, 'showLoginPopup')
|
||||
.mockResolvedValue({ scopes: '' });
|
||||
const helper = new DefaultAuthConnector({
|
||||
...defaultOptions,
|
||||
joinScopes: scopes => `-${[...scopes].join('')}-`,
|
||||
oauthRequestApi: mockOauth,
|
||||
});
|
||||
|
||||
helper.createSession(new Set(['a', 'b']));
|
||||
|
||||
await mockOauth.triggerAll();
|
||||
|
||||
expect(popupSpy).toBeCalledTimes(1);
|
||||
expect(popupSpy.mock.calls[0][0]).toMatchObject({
|
||||
url: 'my-origin/api/auth/my-provider/start?scope=-ab-&env=production',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+10
-1
@@ -43,12 +43,20 @@ type Options<AuthSession> = {
|
||||
* API used to instanciate an auth requester.
|
||||
*/
|
||||
oauthRequestApi: OAuthRequestApi;
|
||||
/**
|
||||
* Function used to join together a set of scopes, defaults to joining with whitespace.
|
||||
*/
|
||||
joinScopes?: (scopes: Set<string>) => string;
|
||||
/**
|
||||
* Function used to transform an auth response into the session type.
|
||||
*/
|
||||
sessionTransform?(response: any): AuthSession | Promise<AuthSession>;
|
||||
};
|
||||
|
||||
function defaultJoinScopes(scopes: Set<string>) {
|
||||
return [...scopes].join(' ');
|
||||
}
|
||||
|
||||
/**
|
||||
* DefaultAuthConnector is the default auth connector in Backstage. It talks to the
|
||||
* backend auth plugin through the standardized API, and requests user permission
|
||||
@@ -69,13 +77,14 @@ export class DefaultAuthConnector<AuthSession>
|
||||
basePath = DEFAULT_BASE_PATH,
|
||||
environment,
|
||||
provider,
|
||||
joinScopes = defaultJoinScopes,
|
||||
oauthRequestApi,
|
||||
sessionTransform = id => id,
|
||||
} = options;
|
||||
|
||||
this.authRequester = oauthRequestApi.createAuthRequester({
|
||||
provider,
|
||||
onAuthRequest: scopes => this.showPopup([...scopes].join(' ')),
|
||||
onAuthRequest: scopes => this.showPopup(joinScopes(scopes)),
|
||||
});
|
||||
|
||||
this.apiOrigin = apiOrigin;
|
||||
|
||||
Reference in New Issue
Block a user