From bb8bc7dd42d1d07bd47a40e58351ad932e558b71 Mon Sep 17 00:00:00 2001 From: "J Shamsul Bahri (jibone))" Date: Mon, 21 Sep 2020 15:00:56 +0800 Subject: [PATCH] merge master and start using discoveryApi --- app-config.yaml | 5 +-- .../core-api/src/apis/definitions/auth.ts | 2 +- .../implementations/auth/saml/SamlAuth.ts | 12 +++---- .../lib/AuthConnector/SamlAuthConnector.ts | 32 ++++++++----------- packages/core/src/api-wrappers/defaultApis.ts | 9 ++++++ 5 files changed, 31 insertions(+), 29 deletions(-) diff --git a/app-config.yaml b/app-config.yaml index 5b704f9b8e..562a2b0e2e 100644 --- a/app-config.yaml +++ b/app-config.yaml @@ -131,8 +131,9 @@ auth: $secret: env: GITLAB_BASE_URL saml: - entryPoint: 'http://localhost:7001/' - issuer: 'passport-saml' + development: + entryPoint: 'http://localhost:7001/' + issuer: 'passport-saml' okta: development: clientId: diff --git a/packages/core-api/src/apis/definitions/auth.ts b/packages/core-api/src/apis/definitions/auth.ts index ec99bae6b2..933a78b693 100644 --- a/packages/core-api/src/apis/definitions/auth.ts +++ b/packages/core-api/src/apis/definitions/auth.ts @@ -332,5 +332,5 @@ export const oauth2ApiRef = createApiRef< */ export const samlAuthApiRef = createApiRef({ id: 'core.auth.saml', - description: 'provides authentication towards saml based provider', + description: 'Example of how to use SAML custom provider', }); diff --git a/packages/core-api/src/apis/implementations/auth/saml/SamlAuth.ts b/packages/core-api/src/apis/implementations/auth/saml/SamlAuth.ts index cb93dcea97..aeede356ef 100644 --- a/packages/core-api/src/apis/implementations/auth/saml/SamlAuth.ts +++ b/packages/core-api/src/apis/implementations/auth/saml/SamlAuth.ts @@ -25,7 +25,7 @@ import { AuthRequestOptions, SamlApi, } from '../../../definitions/auth'; -import { AuthProvider } from '../../../definitions'; +import { AuthProvider, DiscoveryApi } from '../../../definitions'; import { SamlSession } from './types'; import { SamlAuthSessionManager, @@ -33,8 +33,7 @@ import { } from '../../../../lib/AuthSessionManager'; type CreateOptions = { - apiOrigin: string; - basePath: string; + discoveryApi: DiscoveryApi; environment?: string; provider?: AuthProvider & { id: string }; }; @@ -53,14 +52,12 @@ const DEFAULT_PROVIDER = { class SamlAuth implements SamlApi { static create({ - apiOrigin, - basePath, + discoveryApi, environment = 'development', provider = DEFAULT_PROVIDER, }: CreateOptions) { const connector = new SamlAuthConnector({ - apiOrigin, - basePath, + discoveryApi, environment, provider, }); @@ -74,7 +71,6 @@ class SamlAuth implements SamlApi { storageKey: 'samlSession', }); - // return new SamlAuth(authSessionStore); return new SamlAuth(authSessionStore); } diff --git a/packages/core-api/src/lib/AuthConnector/SamlAuthConnector.ts b/packages/core-api/src/lib/AuthConnector/SamlAuthConnector.ts index 4a6babe224..47d8f25820 100644 --- a/packages/core-api/src/lib/AuthConnector/SamlAuthConnector.ts +++ b/packages/core-api/src/lib/AuthConnector/SamlAuthConnector.ts @@ -17,15 +17,13 @@ import { AuthProvider, ProfileInfo, BackstageIdentity, + DiscoveryApi, } from '../../apis/definitions'; import { AuthConnector } from './types'; import { showLoginPopup } from '../loginPopup'; -const DEFAULT_BASE_PATH = '/api/auth'; - type Options = { - apiOrigin?: string; - basePath?: string; + discoveryApi: DiscoveryApi; environment?: string; provider: AuthProvider & { id: string }; }; @@ -38,30 +36,24 @@ export type SamlResponse = { export class SamlAuthConnector implements AuthConnector { - private readonly apiOrigin: string; - private readonly basePath: string; + private readonly discoveryApi: DiscoveryApi; private readonly environment: string | undefined; private readonly provider: AuthProvider & { id: string }; constructor(options: Options) { - const { - apiOrigin = window.location.origin, - basePath = DEFAULT_BASE_PATH, - environment, - provider, - } = options; + const { discoveryApi, environment, provider } = options; - this.apiOrigin = apiOrigin; - this.basePath = basePath; + this.discoveryApi = discoveryApi; this.environment = environment; this.provider = provider; } async createSession(): Promise { + const popupUrl = await this.buildUrl('/start'); const payload = await showLoginPopup({ - url: 'http://localhost:7000/auth/saml/start', // FIXME: remove hardcoded here. should do something like buildUrl + url: popupUrl, name: 'SAML Login', // FIXME: change this to provider name? and not hardcode the name - origin: this.apiOrigin, + origin: new URL(popupUrl).origin, width: 450, height: 730, }); @@ -76,8 +68,7 @@ export class SamlAuthConnector async refreshSession(): Promise {} async removeSession(): Promise { - // FIXME: remove hardcoded url here... should do something like buildUrl - const res = await fetch('http://localhost:7000/auth/saml/logout', { + const res = await fetch(await this.buildUrl('/logout'), { method: 'POST', headers: { 'x-requested-with': 'XMLHttpRequest', @@ -93,4 +84,9 @@ export class SamlAuthConnector throw error; } } + + private async buildUrl(path: string): Promise { + const baseUrl = await this.discoveryApi.getBaseUrl('auth'); + return `${baseUrl}/${this.provider.id}${path}?env=${this.environment}`; + } } diff --git a/packages/core/src/api-wrappers/defaultApis.ts b/packages/core/src/api-wrappers/defaultApis.ts index a3f0cb0251..dedadf0456 100644 --- a/packages/core/src/api-wrappers/defaultApis.ts +++ b/packages/core/src/api-wrappers/defaultApis.ts @@ -44,6 +44,8 @@ import { createApiFactory, configApiRef, UrlPatternDiscovery, + samlAuthApiRef, + SamlAuth, } from '@backstage/core-api'; export const defaultApis = [ @@ -132,4 +134,11 @@ export const defaultApis = [ factory: ({ discoveryApi, oauthRequestApi }) => OAuth2.create({ discoveryApi, oauthRequestApi }), }), + createApiFactory({ + api: samlAuthApiRef, + deps: { + discoveryApi: discoveryApiRef, + }, + factory: ({ discoveryApi }) => SamlAuth.create({ discoveryApi }), + }), ];