From 40b71f8299bf0d63ea80d7a92b671be241d849d0 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Sat, 5 Sep 2020 15:08:10 +0200 Subject: [PATCH] auth-backend: refactory oauth refresh handler to receive a single request object --- plugins/auth-backend/src/lib/oauth/OAuthAdapter.ts | 8 ++++++-- plugins/auth-backend/src/lib/oauth/index.ts | 1 + plugins/auth-backend/src/lib/oauth/types.ts | 10 ++++++---- plugins/auth-backend/src/providers/auth0/provider.ts | 7 ++++--- plugins/auth-backend/src/providers/google/provider.ts | 7 ++++--- .../auth-backend/src/providers/microsoft/provider.ts | 7 ++++--- plugins/auth-backend/src/providers/oauth2/provider.ts | 7 ++++--- plugins/auth-backend/src/providers/okta/provider.ts | 7 ++++--- 8 files changed, 33 insertions(+), 21 deletions(-) diff --git a/plugins/auth-backend/src/lib/oauth/OAuthAdapter.ts b/plugins/auth-backend/src/lib/oauth/OAuthAdapter.ts index a0583cdd9e..62cb8e444d 100644 --- a/plugins/auth-backend/src/lib/oauth/OAuthAdapter.ts +++ b/plugins/auth-backend/src/lib/oauth/OAuthAdapter.ts @@ -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); diff --git a/plugins/auth-backend/src/lib/oauth/index.ts b/plugins/auth-backend/src/lib/oauth/index.ts index 20a1ca6f99..564a0e1e7c 100644 --- a/plugins/auth-backend/src/lib/oauth/index.ts +++ b/plugins/auth-backend/src/lib/oauth/index.ts @@ -24,4 +24,5 @@ export type { OAuthResponse, OAuthState, OAuthStartRequest, + OAuthRefreshRequest, } from './types'; diff --git a/plugins/auth-backend/src/lib/oauth/types.ts b/plugins/auth-backend/src/lib/oauth/types.ts index f3a50e8596..b2b7915a4e 100644 --- a/plugins/auth-backend/src/lib/oauth/types.ts +++ b/plugins/auth-backend/src/lib/oauth/types.ts @@ -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>; + refresh?(req: OAuthRefreshRequest): Promise>; /** * (Optional) Sign out of the auth provider. diff --git a/plugins/auth-backend/src/providers/auth0/provider.ts b/plugins/auth-backend/src/providers/auth0/provider.ts index d71ee1bbed..668ab17ee1 100644 --- a/plugins/auth-backend/src/providers/auth0/provider.ts +++ b/plugins/auth-backend/src/providers/auth0/provider.ts @@ -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 { + async refresh(req: OAuthRefreshRequest): Promise { const { accessToken, params } = await executeRefreshTokenStrategy( this._strategy, - refreshToken, - scope, + req.refreshToken, + req.scope, ); const profile = await executeFetchUserProfileStrategy( diff --git a/plugins/auth-backend/src/providers/google/provider.ts b/plugins/auth-backend/src/providers/google/provider.ts index 2eb7b8573b..3cc585605c 100644 --- a/plugins/auth-backend/src/providers/google/provider.ts +++ b/plugins/auth-backend/src/providers/google/provider.ts @@ -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 { + async refresh(req: OAuthRefreshRequest): Promise { const { accessToken, params } = await executeRefreshTokenStrategy( this._strategy, - refreshToken, - scope, + req.refreshToken, + req.scope, ); const profile = await executeFetchUserProfileStrategy( diff --git a/plugins/auth-backend/src/providers/microsoft/provider.ts b/plugins/auth-backend/src/providers/microsoft/provider.ts index 2eba3b5191..baa66f0662 100644 --- a/plugins/auth-backend/src/providers/microsoft/provider.ts +++ b/plugins/auth-backend/src/providers/microsoft/provider.ts @@ -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 { + async refresh(req: OAuthRefreshRequest): Promise { const { accessToken, params } = await executeRefreshTokenStrategy( this._strategy, - refreshToken, - scope, + req.refreshToken, + req.scope, ); const profile = await executeFetchUserProfileStrategy( diff --git a/plugins/auth-backend/src/providers/oauth2/provider.ts b/plugins/auth-backend/src/providers/oauth2/provider.ts index 4a510f8c25..a657ea2195 100644 --- a/plugins/auth-backend/src/providers/oauth2/provider.ts +++ b/plugins/auth-backend/src/providers/oauth2/provider.ts @@ -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 { + async refresh(req: OAuthRefreshRequest): Promise { const refreshTokenResponse = await executeRefreshTokenStrategy( this._strategy, - refreshToken, - scope, + req.refreshToken, + req.scope, ); const { accessToken, diff --git a/plugins/auth-backend/src/providers/okta/provider.ts b/plugins/auth-backend/src/providers/okta/provider.ts index 0ac75beaf8..09597696ba 100644 --- a/plugins/auth-backend/src/providers/okta/provider.ts +++ b/plugins/auth-backend/src/providers/okta/provider.ts @@ -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 { + async refresh(req: OAuthRefreshRequest): Promise { const { accessToken, params } = await executeRefreshTokenStrategy( this._strategy, - refreshToken, - scope, + req.refreshToken, + req.scope, ); const profile = await executeFetchUserProfileStrategy(