Fix build and add okta oauth2 scope normalization

This commit is contained in:
Nicholas Pirrello
2020-06-25 18:20:13 -04:00
parent 5b4407d92b
commit 144a3b9294
4 changed files with 51 additions and 4 deletions
@@ -18,6 +18,8 @@ import OktaAuth from './OktaAuth';
const theFuture = new Date(Date.now() + 3600000);
const thePast = new Date(Date.now() - 10);
const PREFIX = 'okta.';
describe('OktaAuth', () => {
it('should get refreshed access token', async () => {
const getSession = jest.fn().mockResolvedValue({
@@ -108,4 +110,20 @@ describe('OktaAuth', () => {
await expect(promise3).resolves.toBe('token2');
expect(getSession).toBeCalledTimes(4); // De-duping of session requests happens in client
});
it.each([
['openid', ['openid']],
['profile email', ['profile', 'email']],
[`${PREFIX}groups.manage`, [`${PREFIX}groups.manage`]],
['groups.read', [`${PREFIX}groups.read`]],
[`${PREFIX}groups.manage groups.read, openid`, [`${PREFIX}groups.manage`, `${PREFIX}groups.read`, 'openid']],
[`email\t ${PREFIX}groups.read`, ['email', `${PREFIX}groups.read`]],
// Some incorrect scopes that we don't try to fix
[`${PREFIX}email`, [`${PREFIX}email`]],
[`${PREFIX}profile`, [`${PREFIX}profile`]],
[`${PREFIX}openid`, [`${PREFIX}openid`]],
])(`should normalize scopes correctly - %p`, (scope, scopes) => {
expect(OktaAuth.normalizeScopes(scope)).toEqual(new Set(scopes));
});
});
@@ -60,6 +60,12 @@ const DEFAULT_PROVIDER = {
icon: OktaIcon,
};
const OKTA_OIDC_SCOPES: Set<String> = new Set(
['openid', 'profile', 'email', 'phone', 'address', 'groups', 'offline_access']
)
const OKTA_SCOPE_PREFIX: string = 'okta.'
class OktaAuth implements
OAuthApi,
OpenIdConnectApi,
@@ -152,7 +158,7 @@ class OktaAuth implements
return session?.profile;
}
static normalizeScopes(scope?: string): Set<string> {
static normalizeScopes(scope?: string | string[]): Set<string> {
if (!scope) {
return new Set();
}
@@ -161,7 +167,19 @@ class OktaAuth implements
? scope
: scope.split(/[\s|,]/).filter(Boolean);
return new Set(scopeList);
const normalizedScopes = scopeList.map(scope => {
if (OKTA_OIDC_SCOPES.has(scope)) {
return scope;
}
if (scope.startsWith(OKTA_SCOPE_PREFIX)) {
return scope;
}
return `${OKTA_SCOPE_PREFIX}${scope}`
});
return new Set(normalizedScopes);
}
}
+11
View File
@@ -6,11 +6,13 @@ import {
OAuthRequestManager,
googleAuthApiRef,
githubAuthApiRef,
oktaAuthApiRef,
AlertApiForwarder,
ErrorApiForwarder,
ErrorAlerter,
GoogleAuth,
GithubAuth,
OktaAuth,
identityApiRef,
} from '@backstage/core';
@@ -50,4 +52,13 @@ builder.add(
}),
);
builder.add(
oktaAuthApiRef,
OktaAuth.create({
apiOrigin: 'http://localhost:7000',
basePath: '/auth/',
oauthRequestApi,
}),
);
export const apis = builder.build();
@@ -59,10 +59,10 @@ export class OktaAuthProvider implements OAuthProviderHandlers {
* allowing us to avoid using express-session in order to integrate with Okta.
*/
private _store: StateStore = {
store(req: Request, cb: any) {
store({}, cb: any) {
cb(null, null);
},
verify(req: Request, state: string, cb: any) {
verify({}, {}, cb: any) {
cb(null, true);
},
}