Merge pull request #2863 from SDA-SE/feat/default-scope-auth-providers

feat: configurable default scopes of auth providers
This commit is contained in:
Patrik Oldsberg
2020-11-05 10:53:43 +01:00
committed by GitHub
10 changed files with 95 additions and 123 deletions
+29
View File
@@ -0,0 +1,29 @@
---
'@backstage/core-api': patch
---
Updated the AuthApi `.create` methods to configure the default scope of the corresponding Auth Api. As a result the
default scope is configurable when overwriting the Core Api in the app.
```
GithubAuth.create({
discoveryApi,
oauthRequestApi,
defaultScopes: ['read:user', 'repo'],
}),
```
Replaced redundant CreateOptions of each Auth Api with the OAuthApiCreateOptions type.
```
export type OAuthApiCreateOptions = AuthApiCreateOptions & {
oauthRequestApi: OAuthRequestApi;
defaultScopes?: string[];
};
export type AuthApiCreateOptions = {
discoveryApi: DiscoveryApi;
environment?: string;
provider?: AuthProvider & { id: string };
};
```
@@ -16,20 +16,8 @@
import Auth0Icon from '@material-ui/icons/AcUnit';
import { auth0AuthApiRef } from '../../../definitions/auth';
import {
OAuthRequestApi,
AuthProvider,
DiscoveryApi,
} from '../../../definitions';
import { OAuth2 } from '../oauth2';
type CreateOptions = {
discoveryApi: DiscoveryApi;
oauthRequestApi: OAuthRequestApi;
environment?: string;
provider?: AuthProvider & { id: string };
};
import { OAuthApiCreateOptions } from '../types';
const DEFAULT_PROVIDER = {
id: 'auth0',
@@ -43,13 +31,14 @@ class Auth0Auth {
environment = 'development',
provider = DEFAULT_PROVIDER,
oauthRequestApi,
}: CreateOptions): typeof auth0AuthApiRef.T {
defaultScopes = ['openid', `email`, `profile`],
}: OAuthApiCreateOptions): typeof auth0AuthApiRef.T {
return OAuth2.create({
discoveryApi,
oauthRequestApi,
provider,
environment,
defaultScopes: ['openid', `email`, `profile`],
defaultScopes,
});
}
}
@@ -25,26 +25,13 @@ import {
BackstageIdentity,
AuthRequestOptions,
} from '../../../definitions/auth';
import {
OAuthRequestApi,
AuthProvider,
DiscoveryApi,
} from '../../../definitions';
import { SessionManager } from '../../../../lib/AuthSessionManager/types';
import {
AuthSessionStore,
StaticAuthSessionManager,
} from '../../../../lib/AuthSessionManager';
import { Observable } from '../../../../types';
type CreateOptions = {
discoveryApi: DiscoveryApi;
oauthRequestApi: OAuthRequestApi;
defaultScopes?: string[];
environment?: string;
provider?: AuthProvider & { id: string };
};
import { OAuthApiCreateOptions } from '../types';
export type GithubAuthResponse = {
providerInfo: {
@@ -69,7 +56,7 @@ class GithubAuth implements OAuthApi, SessionApi {
provider = DEFAULT_PROVIDER,
oauthRequestApi,
defaultScopes = ['read:user'],
}: CreateOptions) {
}: OAuthApiCreateOptions) {
const connector = new DefaultAuthConnector({
discoveryApi,
environment,
@@ -16,20 +16,8 @@
import GitlabIcon from '@material-ui/icons/AcUnit';
import { gitlabAuthApiRef } from '../../../definitions/auth';
import {
OAuthRequestApi,
AuthProvider,
DiscoveryApi,
} from '../../../definitions';
import { OAuth2 } from '../oauth2';
type CreateOptions = {
discoveryApi: DiscoveryApi;
oauthRequestApi: OAuthRequestApi;
environment?: string;
provider?: AuthProvider & { id: string };
};
import { OAuthApiCreateOptions } from '../types';
const DEFAULT_PROVIDER = {
id: 'gitlab',
@@ -43,13 +31,14 @@ class GitlabAuth {
environment = 'development',
provider = DEFAULT_PROVIDER,
oauthRequestApi,
}: CreateOptions): typeof gitlabAuthApiRef.T {
defaultScopes = ['read_user'],
}: OAuthApiCreateOptions): typeof gitlabAuthApiRef.T {
return OAuth2.create({
discoveryApi,
oauthRequestApi,
provider,
environment,
defaultScopes: ['read_user'],
defaultScopes,
});
}
}
@@ -16,20 +16,8 @@
import GoogleIcon from '@material-ui/icons/AcUnit';
import { googleAuthApiRef } from '../../../definitions/auth';
import {
OAuthRequestApi,
AuthProvider,
DiscoveryApi,
} from '../../../definitions';
import { OAuth2 } from '../oauth2';
type CreateOptions = {
discoveryApi: DiscoveryApi;
oauthRequestApi: OAuthRequestApi;
environment?: string;
provider?: AuthProvider & { id: string };
};
import { OAuthApiCreateOptions } from '../types';
const DEFAULT_PROVIDER = {
id: 'google',
@@ -37,25 +25,26 @@ const DEFAULT_PROVIDER = {
icon: GoogleIcon,
};
const SCOPE_PREFIX = 'https://www.googleapis.com/auth/';
class GoogleAuth {
static create({
discoveryApi,
oauthRequestApi,
environment = 'development',
provider = DEFAULT_PROVIDER,
}: CreateOptions): typeof googleAuthApiRef.T {
const SCOPE_PREFIX = 'https://www.googleapis.com/auth/';
defaultScopes = [
'openid',
`${SCOPE_PREFIX}userinfo.email`,
`${SCOPE_PREFIX}userinfo.profile`,
],
}: OAuthApiCreateOptions): typeof googleAuthApiRef.T {
return OAuth2.create({
discoveryApi,
oauthRequestApi,
provider,
environment,
defaultScopes: [
'openid',
`${SCOPE_PREFIX}userinfo.email`,
`${SCOPE_PREFIX}userinfo.profile`,
],
defaultScopes,
scopeTransform(scopes: string[]) {
return scopes.map(scope => {
if (scope === 'openid') {
@@ -16,21 +16,8 @@
import MicrosoftIcon from '@material-ui/icons/AcUnit';
import { microsoftAuthApiRef } from '../../../definitions/auth';
import {
OAuthRequestApi,
AuthProvider,
DiscoveryApi,
} from '../../../definitions';
import { OAuth2 } from '../oauth2';
type CreateOptions = {
discoveryApi: DiscoveryApi;
oauthRequestApi: OAuthRequestApi;
environment?: string;
provider?: AuthProvider & { id: string };
};
import { OAuthApiCreateOptions } from '../types';
const DEFAULT_PROVIDER = {
id: 'microsoft',
@@ -44,19 +31,20 @@ class MicrosoftAuth {
provider = DEFAULT_PROVIDER,
oauthRequestApi,
discoveryApi,
}: CreateOptions): typeof microsoftAuthApiRef.T {
defaultScopes = [
'openid',
'offline_access',
'profile',
'email',
'User.Read',
],
}: OAuthApiCreateOptions): typeof microsoftAuthApiRef.T {
return OAuth2.create({
discoveryApi,
oauthRequestApi,
provider,
environment,
defaultScopes: [
'openid',
'offline_access',
'profile',
'email',
'User.Read',
],
defaultScopes,
});
}
}
@@ -19,11 +19,6 @@ import { DefaultAuthConnector } from '../../../../lib/AuthConnector';
import { RefreshingAuthSessionManager } from '../../../../lib/AuthSessionManager';
import { SessionManager } from '../../../../lib/AuthSessionManager/types';
import { Observable } from '../../../../types';
import {
AuthProvider,
OAuthRequestApi,
DiscoveryApi,
} from '../../../definitions';
import {
AuthRequestOptions,
BackstageIdentity,
@@ -36,19 +31,14 @@ import {
BackstageIdentityApi,
} from '../../../definitions/auth';
import { OAuth2Session } from './types';
import { OAuthApiCreateOptions } from '../types';
type Options = {
sessionManager: SessionManager<OAuth2Session>;
scopeTransform: (scopes: string[]) => string[];
};
type CreateOptions = {
discoveryApi: DiscoveryApi;
oauthRequestApi: OAuthRequestApi;
environment?: string;
provider?: AuthProvider & { id: string };
defaultScopes?: string[];
type CreateOptions = OAuthApiCreateOptions & {
scopeTransform?: (scopes: string[]) => string[];
};
@@ -16,20 +16,8 @@
import OktaIcon from '@material-ui/icons/AcUnit';
import { oktaAuthApiRef } from '../../../definitions/auth';
import {
OAuthRequestApi,
AuthProvider,
DiscoveryApi,
} from '../../../definitions';
import { OAuth2 } from '../oauth2';
type CreateOptions = {
discoveryApi: DiscoveryApi;
oauthRequestApi: OAuthRequestApi;
environment?: string;
provider?: AuthProvider & { id: string };
};
import { OAuthApiCreateOptions } from '../types';
const DEFAULT_PROVIDER = {
id: 'okta',
@@ -55,13 +43,14 @@ class OktaAuth {
environment = 'development',
provider = DEFAULT_PROVIDER,
oauthRequestApi,
}: CreateOptions): typeof oktaAuthApiRef.T {
defaultScopes = ['openid', 'email', 'profile', 'offline_access'],
}: OAuthApiCreateOptions): typeof oktaAuthApiRef.T {
return OAuth2.create({
discoveryApi,
oauthRequestApi,
provider,
environment,
defaultScopes: ['openid', 'email', 'profile', 'offline_access'],
defaultScopes,
scopeTransform(scopes) {
return scopes.map(scope => {
if (OKTA_OIDC_SCOPES.has(scope)) {
@@ -27,18 +27,12 @@ import {
BackstageIdentityApi,
SessionApi,
} from '../../../definitions/auth';
import { AuthProvider, DiscoveryApi } from '../../../definitions';
import { SamlSession } from './types';
import {
AuthSessionStore,
StaticAuthSessionManager,
} from '../../../../lib/AuthSessionManager';
type CreateOptions = {
discoveryApi: DiscoveryApi;
environment?: string;
provider?: AuthProvider & { id: string };
};
import { AuthApiCreateOptions } from '../types';
export type SamlAuthResponse = {
profile: ProfileInfo;
@@ -56,7 +50,7 @@ class SamlAuth implements ProfileInfoApi, BackstageIdentityApi, SessionApi {
discoveryApi,
environment = 'development',
provider = DEFAULT_PROVIDER,
}: CreateOptions) {
}: AuthApiCreateOptions) {
const connector = new DirectAuthConnector<SamlSession>({
discoveryApi,
environment,
@@ -0,0 +1,28 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AuthProvider, DiscoveryApi, OAuthRequestApi } from '../../definitions';
export type OAuthApiCreateOptions = AuthApiCreateOptions & {
oauthRequestApi: OAuthRequestApi;
defaultScopes?: string[];
};
export type AuthApiCreateOptions = {
discoveryApi: DiscoveryApi;
environment?: string;
provider?: AuthProvider & { id: string };
};