From 645c7f444bd07368fdc644a92018c1e0f36482d5 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 19 Aug 2020 09:21:59 +0200 Subject: [PATCH] core-api: switch auth provider apiOrigin option to backendUrl --- packages/app/src/apis.ts | 19 ++++--- .../implementations/auth/auth0/Auth0Auth.ts | 12 ++--- .../implementations/auth/github/GithubAuth.ts | 6 +-- .../implementations/auth/gitlab/GitlabAuth.ts | 6 +-- .../implementations/auth/google/GoogleAuth.ts | 6 +-- .../implementations/auth/oauth2/OAuth2.ts | 6 +-- .../implementations/auth/okta/OktaAuth.ts | 52 +++++++++---------- .../DefaultAuthConnector.test.ts | 8 +-- .../lib/AuthConnector/DefaultAuthConnector.ts | 15 +++--- packages/dev-utils/src/devApp/apiFactories.ts | 8 +-- .../ProfileCatalog/ProfileCatalog.test.tsx | 2 +- 11 files changed, 70 insertions(+), 70 deletions(-) diff --git a/packages/app/src/apis.ts b/packages/app/src/apis.ts index 3bc0c82b31..9f54e244df 100644 --- a/packages/app/src/apis.ts +++ b/packages/app/src/apis.ts @@ -66,7 +66,10 @@ import { } from '@backstage/plugin-github-actions'; import { jenkinsApiRef, JenkinsApi } from '@backstage/plugin-jenkins'; -import { TravisCIApi, travisCIApiRef } from '@roadiehq/backstage-plugin-travis-ci'; +import { + TravisCIApi, + travisCIApiRef, +} from '@roadiehq/backstage-plugin-travis-ci'; export const apis = (config: ConfigApi) => { // eslint-disable-next-line no-console @@ -106,7 +109,7 @@ export const apis = (config: ConfigApi) => { builder.add( googleAuthApiRef, GoogleAuth.create({ - apiOrigin: backendUrl, + backendUrl, basePath: '/auth/', oauthRequestApi, }), @@ -115,7 +118,7 @@ export const apis = (config: ConfigApi) => { const githubAuthApi = builder.add( githubAuthApiRef, GithubAuth.create({ - apiOrigin: backendUrl, + backendUrl, basePath: '/auth/', oauthRequestApi, }), @@ -124,7 +127,7 @@ export const apis = (config: ConfigApi) => { builder.add( oktaAuthApiRef, OktaAuth.create({ - apiOrigin: backendUrl, + backendUrl, basePath: '/auth/', oauthRequestApi, }), @@ -133,16 +136,16 @@ export const apis = (config: ConfigApi) => { builder.add( gitlabAuthApiRef, GitlabAuth.create({ - apiOrigin: backendUrl, + backendUrl, basePath: '/auth/', oauthRequestApi, }), ); - + builder.add( auth0AuthApiRef, Auth0Auth.create({ - apiOrigin: backendUrl, + backendUrl, basePath: '/auth/', oauthRequestApi, }), @@ -151,7 +154,7 @@ export const apis = (config: ConfigApi) => { builder.add( oauth2ApiRef, OAuth2.create({ - apiOrigin: backendUrl, + backendUrl, basePath: '/auth/', oauthRequestApi, }), diff --git a/packages/core-api/src/apis/implementations/auth/auth0/Auth0Auth.ts b/packages/core-api/src/apis/implementations/auth/auth0/Auth0Auth.ts index 3d8a42a4cc..e69733741f 100644 --- a/packages/core-api/src/apis/implementations/auth/auth0/Auth0Auth.ts +++ b/packages/core-api/src/apis/implementations/auth/auth0/Auth0Auth.ts @@ -34,7 +34,7 @@ import { Observable } from '../../../../types'; type CreateOptions = { // TODO(Following the words of Rugvip): These two should be grabbed from global config when available, they're not unique to Auth0Auth - apiOrigin: string; + backendUrl: string; basePath: string; oauthRequestApi: OAuthRequestApi; @@ -67,14 +67,14 @@ class Auth0Auth BackstageIdentityApi, SessionStateApi { static create({ - apiOrigin, + backendUrl, basePath, environment = 'development', provider = DEFAULT_PROVIDER, oauthRequestApi, }: CreateOptions) { const connector = new DefaultAuthConnector({ - apiOrigin, + backendUrl, basePath, environment, provider, @@ -96,11 +96,7 @@ class Auth0Auth const sessionManager = new RefreshingAuthSessionManager({ connector, - defaultScopes: new Set([ - 'openid', - `email`, - `profile`, - ]), + defaultScopes: new Set(['openid', `email`, `profile`]), sessionScopes: (session: Auth0Session) => session.providerInfo.scopes, sessionShouldRefresh: (session: Auth0Session) => { const expiresInSec = diff --git a/packages/core-api/src/apis/implementations/auth/github/GithubAuth.ts b/packages/core-api/src/apis/implementations/auth/github/GithubAuth.ts index aaea681cef..1ad3e08699 100644 --- a/packages/core-api/src/apis/implementations/auth/github/GithubAuth.ts +++ b/packages/core-api/src/apis/implementations/auth/github/GithubAuth.ts @@ -35,7 +35,7 @@ import { Observable } from '../../../../types'; type CreateOptions = { // TODO(Rugvip): These two should be grabbed from global config when available, they're not unique to GithubAuth - apiOrigin: string; + backendUrl: string; basePath: string; oauthRequestApi: OAuthRequestApi; @@ -62,14 +62,14 @@ const DEFAULT_PROVIDER = { class GithubAuth implements OAuthApi, SessionStateApi { static create({ - apiOrigin, + backendUrl, basePath, environment = 'development', provider = DEFAULT_PROVIDER, oauthRequestApi, }: CreateOptions) { const connector = new DefaultAuthConnector({ - apiOrigin, + backendUrl, basePath, environment, provider, diff --git a/packages/core-api/src/apis/implementations/auth/gitlab/GitlabAuth.ts b/packages/core-api/src/apis/implementations/auth/gitlab/GitlabAuth.ts index 20e13f8a03..e612c4ca61 100644 --- a/packages/core-api/src/apis/implementations/auth/gitlab/GitlabAuth.ts +++ b/packages/core-api/src/apis/implementations/auth/gitlab/GitlabAuth.ts @@ -31,7 +31,7 @@ import { StaticAuthSessionManager } from '../../../../lib/AuthSessionManager'; import { Observable } from '../../../../types'; type CreateOptions = { - apiOrigin: string; + backendUrl: string; basePath: string; oauthRequestApi: OAuthRequestApi; @@ -58,14 +58,14 @@ const DEFAULT_PROVIDER = { class GitlabAuth implements OAuthApi, SessionStateApi { static create({ - apiOrigin, + backendUrl, basePath, environment = 'development', provider = DEFAULT_PROVIDER, oauthRequestApi, }: CreateOptions) { const connector = new DefaultAuthConnector({ - apiOrigin, + backendUrl, basePath, environment, provider, diff --git a/packages/core-api/src/apis/implementations/auth/google/GoogleAuth.ts b/packages/core-api/src/apis/implementations/auth/google/GoogleAuth.ts index 594116fc1a..c6a21dbb20 100644 --- a/packages/core-api/src/apis/implementations/auth/google/GoogleAuth.ts +++ b/packages/core-api/src/apis/implementations/auth/google/GoogleAuth.ts @@ -35,7 +35,7 @@ import { Observable } from '../../../../types'; type CreateOptions = { // TODO(Rugvip): These two should be grabbed from global config when available, they're not unique to GoogleAuth - apiOrigin: string; + backendUrl: string; basePath: string; oauthRequestApi: OAuthRequestApi; @@ -71,14 +71,14 @@ class GoogleAuth BackstageIdentityApi, SessionStateApi { static create({ - apiOrigin, + backendUrl, basePath, environment = 'development', provider = DEFAULT_PROVIDER, oauthRequestApi, }: CreateOptions) { const connector = new DefaultAuthConnector({ - apiOrigin, + backendUrl, basePath, environment, provider, diff --git a/packages/core-api/src/apis/implementations/auth/oauth2/OAuth2.ts b/packages/core-api/src/apis/implementations/auth/oauth2/OAuth2.ts index d2a3531d05..4b6177bed9 100644 --- a/packages/core-api/src/apis/implementations/auth/oauth2/OAuth2.ts +++ b/packages/core-api/src/apis/implementations/auth/oauth2/OAuth2.ts @@ -33,7 +33,7 @@ import { import { OAuth2Session } from './types'; type CreateOptions = { - apiOrigin: string; + backendUrl: string; basePath: string; oauthRequestApi: OAuthRequestApi; @@ -64,14 +64,14 @@ const SCOPE_PREFIX = ''; class OAuth2 implements OAuthApi, OpenIdConnectApi, ProfileInfoApi, SessionStateApi { static create({ - apiOrigin, + backendUrl, basePath, environment = 'development', provider = DEFAULT_PROVIDER, oauthRequestApi, }: CreateOptions) { const connector = new DefaultAuthConnector({ - apiOrigin, + backendUrl, basePath, environment, provider, diff --git a/packages/core-api/src/apis/implementations/auth/okta/OktaAuth.ts b/packages/core-api/src/apis/implementations/auth/okta/OktaAuth.ts index f10ec4ebce..f60b182118 100644 --- a/packages/core-api/src/apis/implementations/auth/okta/OktaAuth.ts +++ b/packages/core-api/src/apis/implementations/auth/okta/OktaAuth.ts @@ -34,7 +34,7 @@ import { RefreshingAuthSessionManager } from '../../../../lib/AuthSessionManager import { Observable } from '../../../../types'; type CreateOptions = { - apiOrigin: string; + backendUrl: string; basePath: string; oauthRequestApi: OAuthRequestApi; @@ -60,28 +60,34 @@ const DEFAULT_PROVIDER = { icon: OktaIcon, }; -const OKTA_OIDC_SCOPES: Set = new Set( - ['openid', 'profile', 'email', 'phone', 'address', 'groups', 'offline_access'] -) +const OKTA_OIDC_SCOPES: Set = new Set([ + 'openid', + 'profile', + 'email', + 'phone', + 'address', + 'groups', + 'offline_access', +]); -const OKTA_SCOPE_PREFIX: string = 'okta.' +const OKTA_SCOPE_PREFIX: string = 'okta.'; -class OktaAuth implements - OAuthApi, - OpenIdConnectApi, - ProfileInfoApi, - BackstageIdentityApi, - SessionStateApi -{ +class OktaAuth + implements + OAuthApi, + OpenIdConnectApi, + ProfileInfoApi, + BackstageIdentityApi, + SessionStateApi { static create({ - apiOrigin, + backendUrl, basePath, environment = 'development', provider = DEFAULT_PROVIDER, oauthRequestApi, }: CreateOptions) { const connector = new DefaultAuthConnector({ - apiOrigin, + backendUrl, basePath, environment, provider, @@ -103,12 +109,7 @@ class OktaAuth implements const sessionManager = new RefreshingAuthSessionManager({ connector, - defaultScopes: new Set([ - 'openid', - 'email', - 'profile', - 'offline_access', - ]), + defaultScopes: new Set(['openid', 'email', 'profile', 'offline_access']), sessionScopes: session => session.scopes, sessionShouldRefresh: session => { const expiresInSec = @@ -126,10 +127,7 @@ class OktaAuth implements constructor(private readonly sessionManager: SessionManager) {} - async getAccessToken( - scope?: string, - options?: AuthRequestOptions - ) { + async getAccessToken(scope?: string, options?: AuthRequestOptions) { const session = await this.sessionManager.getSession({ ...options, scopes: OktaAuth.normalizeScopes(scope), @@ -175,12 +173,12 @@ class OktaAuth implements if (scope.startsWith(OKTA_SCOPE_PREFIX)) { return scope; } - - return `${OKTA_SCOPE_PREFIX}${scope}` + + return `${OKTA_SCOPE_PREFIX}${scope}`; }); return new Set(normalizedScopes); } } -export default OktaAuth; \ No newline at end of file +export default OktaAuth; diff --git a/packages/core-api/src/lib/AuthConnector/DefaultAuthConnector.test.ts b/packages/core-api/src/lib/AuthConnector/DefaultAuthConnector.test.ts index bfbb2dbc0b..b6c31405ae 100644 --- a/packages/core-api/src/lib/AuthConnector/DefaultAuthConnector.test.ts +++ b/packages/core-api/src/lib/AuthConnector/DefaultAuthConnector.test.ts @@ -22,7 +22,7 @@ import * as loginPopup from '../loginPopup'; const anyFetch = fetch as any; const defaultOptions = { - apiOrigin: 'my-origin', + backendUrl: 'http://my-origin', environment: 'production', provider: { id: 'my-provider', @@ -114,7 +114,8 @@ describe('DefaultAuthConnector', () => { expect(popupSpy).toBeCalledTimes(1); expect(popupSpy.mock.calls[0][0]).toMatchObject({ - url: 'my-origin/api/auth/my-provider/start?scope=a%20b&env=production', + url: + 'http://my-origin/api/auth/my-provider/start?scope=a%20b&env=production', }); await expect(sessionPromise).resolves.toEqual({ @@ -162,7 +163,8 @@ describe('DefaultAuthConnector', () => { expect(popupSpy).toBeCalledTimes(1); expect(popupSpy.mock.calls[0][0]).toMatchObject({ - url: 'my-origin/api/auth/my-provider/start?scope=-ab-&env=production', + url: + 'http://my-origin/api/auth/my-provider/start?scope=-ab-&env=production', }); }); }); diff --git a/packages/core-api/src/lib/AuthConnector/DefaultAuthConnector.ts b/packages/core-api/src/lib/AuthConnector/DefaultAuthConnector.ts index f0f57c1c12..41af7f4670 100644 --- a/packages/core-api/src/lib/AuthConnector/DefaultAuthConnector.ts +++ b/packages/core-api/src/lib/AuthConnector/DefaultAuthConnector.ts @@ -23,9 +23,9 @@ const DEFAULT_BASE_PATH = '/api/auth/'; type Options = { /** - * Origin of auth requests, defaults to location.origin + * The base URL of the auth backend. */ - apiOrigin?: string; + backendUrl?: string; /** * Base path of the auth requests, defaults to /api/auth/ */ @@ -64,7 +64,7 @@ function defaultJoinScopes(scopes: Set) { */ export class DefaultAuthConnector implements AuthConnector { - private readonly apiOrigin: string; + private readonly backendUrl: string; private readonly basePath: string; private readonly environment: string; private readonly provider: AuthProvider & { id: string }; @@ -74,7 +74,7 @@ export class DefaultAuthConnector constructor(options: Options) { const { - apiOrigin = window.location.origin, + backendUrl = window.location.origin, basePath = DEFAULT_BASE_PATH, environment, provider, @@ -88,7 +88,7 @@ export class DefaultAuthConnector onAuthRequest: scopes => this.showPopup(scopes), }); - this.apiOrigin = apiOrigin; + this.backendUrl = backendUrl; this.basePath = basePath; this.environment = environment; this.provider = provider; @@ -154,11 +154,12 @@ export class DefaultAuthConnector private async showPopup(scopes: Set): Promise { const scope = this.joinScopesFunc(scopes); const popupUrl = this.buildUrl('/start', { scope }); + const { origin } = new URL(this.backendUrl); const payload = await showLoginPopup({ url: popupUrl, name: `${this.provider.title} Login`, - origin: this.apiOrigin, + origin, width: 450, height: 730, }); @@ -175,7 +176,7 @@ export class DefaultAuthConnector env: this.environment, }); - return `${this.apiOrigin}${this.basePath}${this.provider.id}${path}${queryString}`; + return `${this.backendUrl}${this.basePath}${this.provider.id}${path}${queryString}`; } private buildQueryString(query?: { diff --git a/packages/dev-utils/src/devApp/apiFactories.ts b/packages/dev-utils/src/devApp/apiFactories.ts index b88611f062..5acf64e0d0 100644 --- a/packages/dev-utils/src/devApp/apiFactories.ts +++ b/packages/dev-utils/src/devApp/apiFactories.ts @@ -63,7 +63,7 @@ export const googleAuthApiFactory = createApiFactory({ deps: { oauthRequestApi: oauthRequestApiRef }, factory: ({ oauthRequestApi }) => GoogleAuth.create({ - apiOrigin: 'http://localhost:7000', + backendUrl: 'http://localhost:7000', basePath: '/auth/', oauthRequestApi, }), @@ -74,7 +74,7 @@ export const githubAuthApiFactory = createApiFactory({ deps: { oauthRequestApi: oauthRequestApiRef }, factory: ({ oauthRequestApi }) => GithubAuth.create({ - apiOrigin: 'http://localhost:7000', + backendUrl: 'http://localhost:7000', basePath: '/auth/', oauthRequestApi, }), @@ -85,7 +85,7 @@ export const gitlabAuthApiFactory = createApiFactory({ deps: { oauthRequestApi: oauthRequestApiRef }, factory: ({ oauthRequestApi }) => GitlabAuth.create({ - apiOrigin: 'http://localhost:7000', + backendUrl: 'http://localhost:7000', basePath: '/auth/', oauthRequestApi, }), @@ -96,7 +96,7 @@ export const auth0AuthApiFactory = createApiFactory({ deps: { oauthRequestApi: oauthRequestApiRef }, factory: ({ oauthRequestApi }) => Auth0Auth.create({ - apiOrigin: 'http://localhost:7000', + backendUrl: 'http://localhost:7000', basePath: '/auth/', oauthRequestApi, }), diff --git a/plugins/gitops-profiles/src/components/ProfileCatalog/ProfileCatalog.test.tsx b/plugins/gitops-profiles/src/components/ProfileCatalog/ProfileCatalog.test.tsx index 50268e6a50..a502f6fb4e 100644 --- a/plugins/gitops-profiles/src/components/ProfileCatalog/ProfileCatalog.test.tsx +++ b/plugins/gitops-profiles/src/components/ProfileCatalog/ProfileCatalog.test.tsx @@ -37,7 +37,7 @@ describe('ProfileCatalog', () => { [ githubAuthApiRef, GithubAuth.create({ - apiOrigin: 'http://localhost:7000', + backendUrl: 'http://localhost:7000', basePath: '/auth/', oauthRequestApi, }),