auth-backend: refactory oauth refresh handler to receive a single request object

This commit is contained in:
Patrik Oldsberg
2020-09-05 15:08:10 +02:00
parent 9e07bdb43d
commit 40b71f8299
8 changed files with 33 additions and 21 deletions
@@ -26,7 +26,7 @@ import { InputError } from '@backstage/backend-common';
import { TokenIssuer } from '../../identity';
import { verifyNonce } from './helpers';
import { postMessageResponse, ensuresXRequestedWith } from '../flow';
import { OAuthHandlers, OAuthStartRequest } from './types';
import { OAuthHandlers, OAuthStartRequest, OAuthRefreshRequest } from './types';
export const THOUSAND_DAYS_MS = 1000 * 24 * 60 * 60 * 1000;
export const TEN_MINUTES_MS = 600 * 1000;
@@ -182,8 +182,12 @@ export class OAuthAdapter implements AuthProviderRouteHandlers {
const scope = req.query.scope?.toString() ?? '';
const forwardReq = Object.assign(req, { scope, refreshToken });
// get new access_token
const response = await this.handlers.refresh(refreshToken, scope);
const response = await this.handlers.refresh(
forwardReq as OAuthRefreshRequest,
);
await this.populateIdentity(response.backstageIdentity);
@@ -24,4 +24,5 @@ export type {
OAuthResponse,
OAuthState,
OAuthStartRequest,
OAuthRefreshRequest,
} from './types';
+6 -4
View File
@@ -72,6 +72,11 @@ export type OAuthStartRequest = express.Request<{}> & {
state: OAuthState;
};
export type OAuthRefreshRequest = express.Request<{}> & {
scope: string;
refreshToken: string;
};
/**
* Any OAuth provider needs to implement this interface which has provider specific
* handlers for different methods to perform authentication, get access tokens,
@@ -101,10 +106,7 @@ export interface OAuthHandlers {
* @param {string} refreshToken
* @param {string} scope
*/
refresh?(
refreshToken: string,
scope: string,
): Promise<AuthResponse<OAuthProviderInfo>>;
refresh?(req: OAuthRefreshRequest): Promise<AuthResponse<OAuthProviderInfo>>;
/**
* (Optional) Sign out of the auth provider.
@@ -25,6 +25,7 @@ import {
OAuthEnvironmentHandler,
OAuthStartRequest,
encodeState,
OAuthRefreshRequest,
} from '../../lib/oauth';
import {
executeFetchUserProfileStrategy,
@@ -106,11 +107,11 @@ export class Auth0AuthProvider implements OAuthHandlers {
};
}
async refresh(refreshToken: string, scope: string): Promise<OAuthResponse> {
async refresh(req: OAuthRefreshRequest): Promise<OAuthResponse> {
const { accessToken, params } = await executeRefreshTokenStrategy(
this._strategy,
refreshToken,
scope,
req.refreshToken,
req.scope,
);
const profile = await executeFetchUserProfileStrategy(
@@ -33,6 +33,7 @@ import {
OAuthEnvironmentHandler,
OAuthStartRequest,
encodeState,
OAuthRefreshRequest,
} from '../../lib/oauth';
import passport from 'passport';
@@ -104,11 +105,11 @@ export class GoogleAuthProvider implements OAuthHandlers {
};
}
async refresh(refreshToken: string, scope: string): Promise<OAuthResponse> {
async refresh(req: OAuthRefreshRequest): Promise<OAuthResponse> {
const { accessToken, params } = await executeRefreshTokenStrategy(
this._strategy,
refreshToken,
scope,
req.refreshToken,
req.scope,
);
const profile = await executeFetchUserProfileStrategy(
@@ -37,6 +37,7 @@ import {
OAuthEnvironmentHandler,
OAuthStartRequest,
encodeState,
OAuthRefreshRequest,
} from '../../lib/oauth';
import got from 'got';
@@ -134,11 +135,11 @@ export class MicrosoftAuthProvider implements OAuthHandlers {
};
}
async refresh(refreshToken: string, scope: string): Promise<OAuthResponse> {
async refresh(req: OAuthRefreshRequest): Promise<OAuthResponse> {
const { accessToken, params } = await executeRefreshTokenStrategy(
this._strategy,
refreshToken,
scope,
req.refreshToken,
req.scope,
);
const profile = await executeFetchUserProfileStrategy(
@@ -25,6 +25,7 @@ import {
OAuthEnvironmentHandler,
OAuthStartRequest,
encodeState,
OAuthRefreshRequest,
} from '../../lib/oauth';
import {
executeFetchUserProfileStrategy,
@@ -109,11 +110,11 @@ export class OAuth2AuthProvider implements OAuthHandlers {
};
}
async refresh(refreshToken: string, scope: string): Promise<OAuthResponse> {
async refresh(req: OAuthRefreshRequest): Promise<OAuthResponse> {
const refreshTokenResponse = await executeRefreshTokenStrategy(
this._strategy,
refreshToken,
scope,
req.refreshToken,
req.scope,
);
const {
accessToken,
@@ -22,6 +22,7 @@ import {
OAuthEnvironmentHandler,
OAuthStartRequest,
encodeState,
OAuthRefreshRequest,
} from '../../lib/oauth';
import { Strategy as OktaStrategy } from 'passport-okta-oauth';
import passport from 'passport';
@@ -126,11 +127,11 @@ export class OktaAuthProvider implements OAuthHandlers {
};
}
async refresh(refreshToken: string, scope: string): Promise<OAuthResponse> {
async refresh(req: OAuthRefreshRequest): Promise<OAuthResponse> {
const { accessToken, params } = await executeRefreshTokenStrategy(
this._strategy,
refreshToken,
scope,
req.refreshToken,
req.scope,
);
const profile = await executeFetchUserProfileStrategy(