needed to support refreshing the token

Signed-off-by: Aramis <sennyeyaramis@gmail.com>
Signed-off-by: aramissennyeydd <aramis.sennyey@doordash.com>
This commit is contained in:
Aramis
2024-01-27 16:08:35 -05:00
committed by aramissennyeydd
parent a8d046319e
commit 08b7c8a594
5 changed files with 61 additions and 12 deletions
@@ -24,10 +24,10 @@ import {
BackstageIdentityResponse,
} from '@backstage/core-plugin-api';
import { Observable } from '@backstage/types';
import { DirectAuthConnector } from '../../../../lib/AuthConnector';
import { RefreshingAuthSessionManager } from '../../../../lib/AuthSessionManager';
import { SessionManager } from '../../../../lib/AuthSessionManager/types';
import { AuthApiCreateOptions } from '../types';
import { RefreshingDirectAuthConnector } from '../../../../lib/AuthConnector/RefreshingDirectAuthConnector';
type GuestSession = {
profile: ProfileInfo;
@@ -55,7 +55,7 @@ export default class GuestAuth
provider = DEFAULT_PROVIDER,
} = options;
const connector = new DirectAuthConnector<GuestSession>({
const connector = new RefreshingDirectAuthConnector<GuestSession>({
discoveryApi,
environment,
provider,
@@ -70,7 +70,7 @@ export class DirectAuthConnector<DirectAuthResponse> {
}
}
private async buildUrl(path: string): Promise<string> {
protected async buildUrl(path: string): Promise<string> {
const baseUrl = await this.discoveryApi.getBaseUrl('auth');
return `${baseUrl}/${this.provider.id}${path}?env=${this.environment}`;
}
@@ -0,0 +1,54 @@
/*
* Copyright 2024 The Backstage Authors
*
* 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 { DirectAuthConnector } from './DirectAuthConnector';
export class RefreshingDirectAuthConnector<
DirectAuthResponse,
> extends DirectAuthConnector<DirectAuthResponse> {
async refreshSession(): Promise<any> {
const res = await fetch(
`${await this.buildUrl('/refresh')}&optional=true`,
{
headers: {
'x-requested-with': 'XMLHttpRequest',
},
credentials: 'include',
},
).catch(error => {
throw new Error(`Auth refresh request failed, ${error}`);
});
if (!res.ok) {
const error: any = new Error(
`Auth refresh request failed, ${res.statusText}`,
);
error.status = res.status;
throw error;
}
const authInfo = await res.json();
if (authInfo.error) {
const error = new Error(authInfo.error.message);
if (authInfo.error.name) {
error.name = authInfo.error.name;
}
throw error;
}
return authInfo;
}
}
@@ -56,6 +56,7 @@ export function createGuestAuthRouteHandlers(
const profileTransform = options.profileTransform ?? defaultTransform;
return {
async start(_, res): Promise<void> {
// We are the auth provider for guests, skip this step.
res.redirect('handler/frame');
},
@@ -66,9 +67,7 @@ export function createGuestAuthRouteHandlers(
);
const response: ClientAuthResponse<GuestInfo> = {
profile,
providerInfo: {
name: 'Guest',
},
providerInfo: DEFAULT_RESULT,
};
if (signInResolver) {
const identity = await signInResolver(
@@ -97,7 +96,7 @@ export function createGuestAuthRouteHandlers(
const response: ClientAuthResponse<{}> = {
profile,
providerInfo: {},
providerInfo: DEFAULT_RESULT,
backstageIdentity: prepareBackstageIdentityResponse(identity),
};
@@ -17,11 +17,7 @@ import {
coreServices,
createBackendModule,
} from '@backstage/backend-plugin-api';
import {
createOAuthProviderFactory,
commonSignInResolvers,
authProvidersExtensionPoint,
} from '@backstage/plugin-auth-node';
import { authProvidersExtensionPoint } from '@backstage/plugin-auth-node';
import { createGuestAuthProviderFactory } from './createGuestAuthFactory';
export const authModuleGuestProvider = createBackendModule({