From c8785b9ee063fd3c76c36bdcc17e37d218c6d7db Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 3 Sep 2020 15:08:38 +0200 Subject: [PATCH] auth-backend: rename OAuthProvider to OAuthAdapter --- docs/auth/auth-backend-classes.md | 2 +- .../src/lib/flow/authFlowHelpers.test.ts | 2 +- ...hProvider.test.ts => OAuthAdapter.test.ts} | 24 ++++++++---------- .../{OAuthProvider.ts => OAuthAdapter.ts} | 25 ++++++++----------- plugins/auth-backend/src/lib/oauth/index.ts | 4 +-- plugins/auth-backend/src/lib/oauth/types.ts | 2 +- .../src/providers/auth0/provider.ts | 8 +++--- .../src/providers/github/provider.ts | 8 +++--- .../src/providers/gitlab/provider.ts | 8 +++--- .../src/providers/google/provider.ts | 8 +++--- .../src/providers/microsoft/provider.ts | 8 +++--- .../src/providers/oauth2/provider.ts | 8 +++--- .../src/providers/okta/provider.ts | 8 +++--- 13 files changed, 53 insertions(+), 62 deletions(-) rename plugins/auth-backend/src/lib/oauth/{OAuthProvider.test.ts => OAuthAdapter.test.ts} (91%) rename plugins/auth-backend/src/lib/oauth/{OAuthProvider.ts => OAuthAdapter.ts} (91%) diff --git a/docs/auth/auth-backend-classes.md b/docs/auth/auth-backend-classes.md index 258a732651..0147c66dd9 100644 --- a/docs/auth/auth-backend-classes.md +++ b/docs/auth/auth-backend-classes.md @@ -26,7 +26,7 @@ refer to the type documentation under `plugins/auth-backend/src/providers/types.ts`. There are currently two different classes for two authentication mechanisms that -implement this interface: an `OAuthProvider` for [OAuth](https://oauth.net/2/) +implement this interface: an `OAuthAdapter` for [OAuth](https://oauth.net/2/) based mechanisms and a `SAMLAuthProvider` for [SAML](http://docs.oasis-open.org/security/saml/Post2.0/sstc-saml-tech-overview-2.0.html) based mechanisms. diff --git a/plugins/auth-backend/src/lib/flow/authFlowHelpers.test.ts b/plugins/auth-backend/src/lib/flow/authFlowHelpers.test.ts index b525e98e12..f42e4c4270 100644 --- a/plugins/auth-backend/src/lib/flow/authFlowHelpers.test.ts +++ b/plugins/auth-backend/src/lib/flow/authFlowHelpers.test.ts @@ -18,7 +18,7 @@ import express from 'express'; import { ensuresXRequestedWith, postMessageResponse } from './authFlowHelpers'; import { WebMessageResponse } from './types'; -describe('OAuthProvider Utils', () => { +describe('oauth helpers', () => { describe('postMessageResponse', () => { const appOrigin = 'http://localhost:3000'; it('should post a message back with payload success', () => { diff --git a/plugins/auth-backend/src/lib/oauth/OAuthProvider.test.ts b/plugins/auth-backend/src/lib/oauth/OAuthAdapter.test.ts similarity index 91% rename from plugins/auth-backend/src/lib/oauth/OAuthProvider.test.ts rename to plugins/auth-backend/src/lib/oauth/OAuthAdapter.test.ts index ce78733211..d2b31213f2 100644 --- a/plugins/auth-backend/src/lib/oauth/OAuthProvider.test.ts +++ b/plugins/auth-backend/src/lib/oauth/OAuthAdapter.test.ts @@ -15,13 +15,9 @@ */ import express from 'express'; -import { - THOUSAND_DAYS_MS, - TEN_MINUTES_MS, - OAuthProvider, -} from './OAuthProvider'; +import { THOUSAND_DAYS_MS, TEN_MINUTES_MS, OAuthAdapter } from './OAuthAdapter'; import { encodeState } from './helpers'; -import { OAuthProviderHandlers } from './types'; +import { OAuthHandlers } from './types'; const mockResponseData = { providerInfo: { @@ -38,8 +34,8 @@ const mockResponseData = { }, }; -describe('OAuthProvider', () => { - class MyAuthProvider implements OAuthProviderHandlers { +describe('OAuthAdapter', () => { + class MyAuthProvider implements OAuthHandlers { async start() { return { url: '/url', @@ -71,7 +67,7 @@ describe('OAuthProvider', () => { }; it('sets the correct headers in start', async () => { - const oauthProvider = new OAuthProvider( + const oauthProvider = new OAuthAdapter( providerInstance, oAuthProviderOptions, ); @@ -106,7 +102,7 @@ describe('OAuthProvider', () => { }); it('sets the refresh cookie if refresh is enabled', async () => { - const oauthProvider = new OAuthProvider(providerInstance, { + const oauthProvider = new OAuthAdapter(providerInstance, { ...oAuthProviderOptions, disableRefresh: false, }); @@ -140,7 +136,7 @@ describe('OAuthProvider', () => { }); it('does not set the refresh cookie if refresh is disabled', async () => { - const oauthProvider = new OAuthProvider(providerInstance, { + const oauthProvider = new OAuthAdapter(providerInstance, { ...oAuthProviderOptions, disableRefresh: true, }); @@ -165,7 +161,7 @@ describe('OAuthProvider', () => { }); it('removes refresh cookie when logging out', async () => { - const oauthProvider = new OAuthProvider(providerInstance, { + const oauthProvider = new OAuthAdapter(providerInstance, { ...oAuthProviderOptions, disableRefresh: false, }); @@ -190,7 +186,7 @@ describe('OAuthProvider', () => { it('gets new access-token when refreshing', async () => { oAuthProviderOptions.disableRefresh = false; - const oauthProvider = new OAuthProvider(providerInstance, { + const oauthProvider = new OAuthAdapter(providerInstance, { ...oAuthProviderOptions, disableRefresh: false, }); @@ -219,7 +215,7 @@ describe('OAuthProvider', () => { }); it('handles refresh without capabilities', async () => { - const oauthProvider = new OAuthProvider(providerInstance, { + const oauthProvider = new OAuthAdapter(providerInstance, { ...oAuthProviderOptions, disableRefresh: true, }); diff --git a/plugins/auth-backend/src/lib/oauth/OAuthProvider.ts b/plugins/auth-backend/src/lib/oauth/OAuthAdapter.ts similarity index 91% rename from plugins/auth-backend/src/lib/oauth/OAuthProvider.ts rename to plugins/auth-backend/src/lib/oauth/OAuthAdapter.ts index 1fe62845a0..0f092ae780 100644 --- a/plugins/auth-backend/src/lib/oauth/OAuthProvider.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, encodeState } from './helpers'; import { postMessageResponse, ensuresXRequestedWith } from '../flow'; -import { OAuthProviderHandlers } from './types'; +import { OAuthHandlers } from './types'; export const THOUSAND_DAYS_MS = 1000 * 24 * 60 * 60 * 1000; export const TEN_MINUTES_MS = 600 * 1000; @@ -42,20 +42,20 @@ export type Options = { tokenIssuer: TokenIssuer; }; -export class OAuthProvider implements AuthProviderRouteHandlers { +export class OAuthAdapter implements AuthProviderRouteHandlers { static fromConfig( config: AuthProviderConfig, - providerHandlers: OAuthProviderHandlers, + handlers: OAuthHandlers, options: Pick< Options, 'providerId' | 'persistScopes' | 'disableRefresh' | 'tokenIssuer' >, - ): OAuthProvider { + ): OAuthAdapter { const { origin: appOrigin } = new URL(config.appUrl); const secure = config.baseUrl.startsWith('https://'); const url = new URL(config.baseUrl); const cookiePath = `${url.pathname}/${options.providerId}`; - return new OAuthProvider(providerHandlers, { + return new OAuthAdapter(handlers, { ...options, appOrigin, cookieDomain: url.hostname, @@ -65,7 +65,7 @@ export class OAuthProvider implements AuthProviderRouteHandlers { } constructor( - private readonly providerHandlers: OAuthProviderHandlers, + private readonly handlers: OAuthHandlers, private readonly options: Options, ) {} @@ -94,10 +94,7 @@ export class OAuthProvider implements AuthProviderRouteHandlers { state: stateParameter, }; - const { url, status } = await this.providerHandlers.start( - req, - queryParameters, - ); + const { url, status } = await this.handlers.start(req, queryParameters); res.statusCode = status || 302; res.setHeader('Location', url); @@ -113,9 +110,7 @@ export class OAuthProvider implements AuthProviderRouteHandlers { // verify nonce cookie and state cookie on callback verifyNonce(req, this.options.providerId); - const { response, refreshToken } = await this.providerHandlers.handler( - req, - ); + const { response, refreshToken } = await this.handlers.handler(req); if (this.options.persistScopes) { const grantedScopes = this.getScopesFromCookie( @@ -172,7 +167,7 @@ export class OAuthProvider implements AuthProviderRouteHandlers { return; } - if (!this.providerHandlers.refresh || this.options.disableRefresh) { + if (!this.handlers.refresh || this.options.disableRefresh) { res.send( `Refresh token not supported for provider: ${this.options.providerId}`, ); @@ -191,7 +186,7 @@ export class OAuthProvider implements AuthProviderRouteHandlers { const scope = req.query.scope?.toString() ?? ''; // get new access_token - const response = await this.providerHandlers.refresh(refreshToken, scope); + const response = await this.handlers.refresh(refreshToken, scope); 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 eac43141a4..05c8bd9d3d 100644 --- a/plugins/auth-backend/src/lib/oauth/index.ts +++ b/plugins/auth-backend/src/lib/oauth/index.ts @@ -15,9 +15,9 @@ */ export { OAuthEnvironmentHandler } from './OAuthEnvironmentHandler'; -export { OAuthProvider } from './OAuthProvider'; +export { OAuthAdapter } from './OAuthAdapter'; export type { - OAuthProviderHandlers, + OAuthHandlers, OAuthProviderInfo, OAuthProviderOptions, OAuthResponse, diff --git a/plugins/auth-backend/src/lib/oauth/types.ts b/plugins/auth-backend/src/lib/oauth/types.ts index 440b89ed6f..a854326bed 100644 --- a/plugins/auth-backend/src/lib/oauth/types.ts +++ b/plugins/auth-backend/src/lib/oauth/types.ts @@ -72,7 +72,7 @@ export type OAuthState = { * handlers for different methods to perform authentication, get access tokens, * refresh tokens and perform sign out. */ -export interface OAuthProviderHandlers { +export interface OAuthHandlers { /** * This method initiates a sign in request with an auth provider. * @param {express.Request} req diff --git a/plugins/auth-backend/src/providers/auth0/provider.ts b/plugins/auth-backend/src/providers/auth0/provider.ts index a091a37e90..3b7817ffe9 100644 --- a/plugins/auth-backend/src/providers/auth0/provider.ts +++ b/plugins/auth-backend/src/providers/auth0/provider.ts @@ -18,9 +18,9 @@ import express from 'express'; import passport from 'passport'; import Auth0Strategy from './strategy'; import { - OAuthProvider, + OAuthAdapter, OAuthProviderOptions, - OAuthProviderHandlers, + OAuthHandlers, OAuthResponse, OAuthEnvironmentHandler, } from '../../lib/oauth'; @@ -42,7 +42,7 @@ export type Auth0AuthProviderOptions = OAuthProviderOptions & { domain: string; }; -export class Auth0AuthProvider implements OAuthProviderHandlers { +export class Auth0AuthProvider implements OAuthHandlers { private readonly _strategy: Auth0Strategy; constructor(options: Auth0AuthProviderOptions) { @@ -167,7 +167,7 @@ export const createAuth0Provider: AuthProviderFactory = ({ domain, }); - return OAuthProvider.fromConfig(globalConfig, provider, { + return OAuthAdapter.fromConfig(globalConfig, provider, { disableRefresh: true, providerId, tokenIssuer, diff --git a/plugins/auth-backend/src/providers/github/provider.ts b/plugins/auth-backend/src/providers/github/provider.ts index ab52b628e7..2205fb6794 100644 --- a/plugins/auth-backend/src/providers/github/provider.ts +++ b/plugins/auth-backend/src/providers/github/provider.ts @@ -24,9 +24,9 @@ import { } from '../../lib/passport'; import { RedirectInfo, AuthProviderFactory } from '../types'; import { - OAuthProvider, + OAuthAdapter, OAuthProviderOptions, - OAuthProviderHandlers, + OAuthHandlers, OAuthResponse, OAuthEnvironmentHandler, } from '../../lib/oauth'; @@ -38,7 +38,7 @@ export type GithubAuthProviderOptions = OAuthProviderOptions & { authorizationUrl?: string; }; -export class GithubAuthProvider implements OAuthProviderHandlers { +export class GithubAuthProvider implements OAuthHandlers { private readonly _strategy: GithubStrategy; static transformPassportProfile(rawProfile: any): passport.Profile { @@ -166,7 +166,7 @@ export const createGithubProvider: AuthProviderFactory = ({ authorizationUrl, }); - return OAuthProvider.fromConfig(globalConfig, provider, { + return OAuthAdapter.fromConfig(globalConfig, provider, { disableRefresh: true, persistScopes: true, providerId, diff --git a/plugins/auth-backend/src/providers/gitlab/provider.ts b/plugins/auth-backend/src/providers/gitlab/provider.ts index 26e849aa5c..97f0b2bd22 100644 --- a/plugins/auth-backend/src/providers/gitlab/provider.ts +++ b/plugins/auth-backend/src/providers/gitlab/provider.ts @@ -24,9 +24,9 @@ import { } from '../../lib/passport'; import { RedirectInfo, AuthProviderFactory } from '../types'; import { - OAuthProvider, + OAuthAdapter, OAuthProviderOptions, - OAuthProviderHandlers, + OAuthHandlers, OAuthResponse, OAuthEnvironmentHandler, } from '../../lib/oauth'; @@ -36,7 +36,7 @@ export type GitlabAuthProviderOptions = OAuthProviderOptions & { baseUrl: string; }; -export class GitlabAuthProvider implements OAuthProviderHandlers { +export class GitlabAuthProvider implements OAuthHandlers { private readonly _strategy: GitlabStrategy; static transformPassportProfile(rawProfile: any): passport.Profile { @@ -157,7 +157,7 @@ export const createGitlabProvider: AuthProviderFactory = ({ baseUrl, }); - return OAuthProvider.fromConfig(globalConfig, provider, { + return OAuthAdapter.fromConfig(globalConfig, provider, { disableRefresh: true, providerId, tokenIssuer, diff --git a/plugins/auth-backend/src/providers/google/provider.ts b/plugins/auth-backend/src/providers/google/provider.ts index 62e1d90577..9ee2e1d680 100644 --- a/plugins/auth-backend/src/providers/google/provider.ts +++ b/plugins/auth-backend/src/providers/google/provider.ts @@ -26,8 +26,8 @@ import { } from '../../lib/passport'; import { RedirectInfo, AuthProviderFactory } from '../types'; import { - OAuthProvider, - OAuthProviderHandlers, + OAuthAdapter, + OAuthHandlers, OAuthProviderOptions, OAuthResponse, OAuthEnvironmentHandler, @@ -38,7 +38,7 @@ type PrivateInfo = { refreshToken: string; }; -export class GoogleAuthProvider implements OAuthProviderHandlers { +export class GoogleAuthProvider implements OAuthHandlers { private readonly _strategy: GoogleStrategy; constructor(options: OAuthProviderOptions) { @@ -162,7 +162,7 @@ export const createGoogleProvider: AuthProviderFactory = ({ callbackUrl, }); - return OAuthProvider.fromConfig(globalConfig, provider, { + return OAuthAdapter.fromConfig(globalConfig, provider, { disableRefresh: false, providerId, tokenIssuer, diff --git a/plugins/auth-backend/src/providers/microsoft/provider.ts b/plugins/auth-backend/src/providers/microsoft/provider.ts index 97e7dc3781..edc5509d84 100644 --- a/plugins/auth-backend/src/providers/microsoft/provider.ts +++ b/plugins/auth-backend/src/providers/microsoft/provider.ts @@ -30,9 +30,9 @@ import { import { RedirectInfo, AuthProviderFactory } from '../types'; import { - OAuthProvider, + OAuthAdapter, OAuthProviderOptions, - OAuthProviderHandlers, + OAuthHandlers, OAuthResponse, OAuthEnvironmentHandler, } from '../../lib/oauth'; @@ -48,7 +48,7 @@ export type MicrosoftAuthProviderOptions = OAuthProviderOptions & { tokenUrl?: string; }; -export class MicrosoftAuthProvider implements OAuthProviderHandlers { +export class MicrosoftAuthProvider implements OAuthHandlers { private readonly _strategy: MicrosoftStrategy; static transformAuthResponse( @@ -226,7 +226,7 @@ export const createMicrosoftProvider: AuthProviderFactory = ({ tokenUrl, }); - return OAuthProvider.fromConfig(globalConfig, provider, { + return OAuthAdapter.fromConfig(globalConfig, provider, { disableRefresh: false, providerId, tokenIssuer, diff --git a/plugins/auth-backend/src/providers/oauth2/provider.ts b/plugins/auth-backend/src/providers/oauth2/provider.ts index fea7d58e5c..5a4882fa6f 100644 --- a/plugins/auth-backend/src/providers/oauth2/provider.ts +++ b/plugins/auth-backend/src/providers/oauth2/provider.ts @@ -18,9 +18,9 @@ import express from 'express'; import passport from 'passport'; import { Strategy as OAuth2Strategy } from 'passport-oauth2'; import { - OAuthProvider, + OAuthAdapter, OAuthProviderOptions, - OAuthProviderHandlers, + OAuthHandlers, OAuthResponse, OAuthEnvironmentHandler, } from '../../lib/oauth'; @@ -43,7 +43,7 @@ export type OAuth2AuthProviderOptions = OAuthProviderOptions & { tokenUrl: string; }; -export class OAuth2AuthProvider implements OAuthProviderHandlers { +export class OAuth2AuthProvider implements OAuthHandlers { private readonly _strategy: OAuth2Strategy; constructor(options: OAuth2AuthProviderOptions) { @@ -177,7 +177,7 @@ export const createOAuth2Provider: AuthProviderFactory = ({ tokenUrl, }); - return OAuthProvider.fromConfig(globalConfig, provider, { + return OAuthAdapter.fromConfig(globalConfig, provider, { disableRefresh: false, providerId, tokenIssuer, diff --git a/plugins/auth-backend/src/providers/okta/provider.ts b/plugins/auth-backend/src/providers/okta/provider.ts index 3d33868e84..0368bd8415 100644 --- a/plugins/auth-backend/src/providers/okta/provider.ts +++ b/plugins/auth-backend/src/providers/okta/provider.ts @@ -15,9 +15,9 @@ */ import express from 'express'; import { - OAuthProvider, + OAuthAdapter, OAuthProviderOptions, - OAuthProviderHandlers, + OAuthHandlers, OAuthResponse, OAuthEnvironmentHandler, } from '../../lib/oauth'; @@ -42,7 +42,7 @@ export type OktaAuthProviderOptions = OAuthProviderOptions & { audience: string; }; -export class OktaAuthProvider implements OAuthProviderHandlers { +export class OktaAuthProvider implements OAuthHandlers { private readonly _strategy: any; /** @@ -186,7 +186,7 @@ export const createOktaProvider: AuthProviderFactory = ({ callbackUrl, }); - return OAuthProvider.fromConfig(globalConfig, provider, { + return OAuthAdapter.fromConfig(globalConfig, provider, { disableRefresh: false, providerId, tokenIssuer,