merge master and start using discoveryApi

This commit is contained in:
J Shamsul Bahri (jibone))
2020-09-21 15:00:56 +08:00
parent 49a21d5686
commit bb8bc7dd42
5 changed files with 31 additions and 29 deletions
+3 -2
View File
@@ -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:
@@ -332,5 +332,5 @@ export const oauth2ApiRef = createApiRef<
*/
export const samlAuthApiRef = createApiRef<SamlApi>({
id: 'core.auth.saml',
description: 'provides authentication towards saml based provider',
description: 'Example of how to use SAML custom provider',
});
@@ -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<SamlSession>({
apiOrigin,
basePath,
discoveryApi,
environment,
provider,
});
@@ -74,7 +71,6 @@ class SamlAuth implements SamlApi {
storageKey: 'samlSession',
});
// return new SamlAuth(authSessionStore);
return new SamlAuth(authSessionStore);
}
@@ -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<SamlResponse>
implements AuthConnector<SamlResponse> {
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<SamlResponse> {
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<SamlResponse>
async refreshSession(): Promise<any> {}
async removeSession(): Promise<void> {
// 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<SamlResponse>
throw error;
}
}
private async buildUrl(path: string): Promise<string> {
const baseUrl = await this.discoveryApi.getBaseUrl('auth');
return `${baseUrl}/${this.provider.id}${path}?env=${this.environment}`;
}
}
@@ -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 }),
}),
];