diff --git a/plugins/auth-backend/src/providers/config.ts b/plugins/auth-backend/src/providers/config.ts index b8df8b7e99..96b4a8bca5 100644 --- a/plugins/auth-backend/src/providers/config.ts +++ b/plugins/auth-backend/src/providers/config.ts @@ -1,3 +1,19 @@ +/* + * Copyright 2020 Spotify AB + * + * 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. + */ + export const providers = [ { provider: 'google', diff --git a/plugins/auth-backend/src/providers/google/provider.ts b/plugins/auth-backend/src/providers/google/provider.ts index 94f6ca7fcc..089be9ad3d 100644 --- a/plugins/auth-backend/src/providers/google/provider.ts +++ b/plugins/auth-backend/src/providers/google/provider.ts @@ -1,3 +1,19 @@ +/* + * Copyright 2020 Spotify AB + * + * 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 passport from 'passport'; import express from 'express'; import { Strategy as GoogleStrategy } from 'passport-google-oauth20'; @@ -7,6 +23,22 @@ import { AuthResponse, } from './../types'; +const postMessageResponse = (res: express.Response, data: AuthResponse) => { + const jsonData = JSON.stringify(data); + const base64Data = Buffer.from(jsonData, 'utf8').toString('base64'); + + res.setHeader('X-Frame-Options', 'sameorigin'); + res.end(` + + + + + + `); +}; + export class GoogleAuthProvider implements AuthProvider, AuthProviderRouteHandlers { providerConfig: any; @@ -31,7 +63,7 @@ export class GoogleAuthProvider res: express.Response, next: express.NextFunction, ) { - return passport.authenticate('google', function (_, user) { + return passport.authenticate('google', (_, user) => { postMessageResponse(res, { type: 'auth-result', payload: user, @@ -39,42 +71,22 @@ export class GoogleAuthProvider })(req, res, next); } - logout( - req: express.Request, - res: express.Response, - next: express.NextFunction, - ) { + logout(_req: express.Request, res: express.Response) { return res.send('logout!'); } strategy(): passport.Strategy { return new GoogleStrategy( { ...this.providerConfig.options, passReqToCallback: true }, - function ( + ( _req: any, accessToken: any, refreshToken: any, profile: any, cb: any, - ) { + ) => { cb(undefined, { profile, accessToken, refreshToken }); }, ); } } - -const postMessageResponse = (res: express.Response, data: AuthResponse) => { - const jsonData = JSON.stringify(data); - const base64Data = Buffer.from(jsonData, 'utf8').toString('base64'); - - res.setHeader('X-Frame-Options', 'sameorigin'); - res.end(` - - - - - - `); -}; diff --git a/plugins/auth-backend/src/providers/index.ts b/plugins/auth-backend/src/providers/index.ts index ea58df2fad..61b3ecdda7 100644 --- a/plugins/auth-backend/src/providers/index.ts +++ b/plugins/auth-backend/src/providers/index.ts @@ -1,3 +1,19 @@ +/* + * Copyright 2020 Spotify AB + * + * 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 Router from 'express-promise-router'; import { AuthProviderRouteHandlers, AuthProviderFactories } from './types'; @@ -7,18 +23,6 @@ const providerFactories: AuthProviderFactories = { google: GoogleAuthProvider, }; -export const makeProvider = (config: any) => { - const provider = config.provider; - const providerImpl = providerFactories[provider]; - if (!providerImpl) { - throw Error(`Provider Implementation missing for provider: ${provider}`); - } - const providerInstance = new providerImpl(config); - const strategy = providerInstance.strategy(); - const providerRouter = defaultRouter(providerInstance); - return { provider, strategy, providerRouter }; -}; - export const defaultRouter = (provider: AuthProviderRouteHandlers) => { const router = Router(); router.get('/start', provider.start); @@ -29,3 +33,15 @@ export const defaultRouter = (provider: AuthProviderRouteHandlers) => { } return router; }; + +export const makeProvider = (config: any) => { + const provider = config.provider; + const ProviderImpl = providerFactories[provider]; + if (!ProviderImpl) { + throw Error(`Provider Implementation missing for provider: ${provider}`); + } + const providerInstance = new ProviderImpl(config); + const strategy = providerInstance.strategy(); + const providerRouter = defaultRouter(providerInstance); + return { provider, strategy, providerRouter }; +}; diff --git a/plugins/auth-backend/src/providers/types.ts b/plugins/auth-backend/src/providers/types.ts index 1a9c609724..3b200d906a 100644 --- a/plugins/auth-backend/src/providers/types.ts +++ b/plugins/auth-backend/src/providers/types.ts @@ -1,3 +1,19 @@ +/* + * Copyright 2020 Spotify AB + * + * 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 express from 'express'; import passport from 'passport'; diff --git a/plugins/auth-backend/src/service/router.ts b/plugins/auth-backend/src/service/router.ts index b22dbe310c..f6b476b8dd 100644 --- a/plugins/auth-backend/src/service/router.ts +++ b/plugins/auth-backend/src/service/router.ts @@ -29,19 +29,21 @@ export async function createRouter( options: RouterOptions, ): Promise { const router = Router(); + const logger = options.logger.child({ plugin: 'auth' }); // configure all the providers for (const providerConfig of providers) { + logger.info('Configuring providers'); const { provider, strategy, providerRouter } = makeProvider(providerConfig); passport.use(strategy); router.use(`/${provider}`, providerRouter); } - passport.serializeUser(function (user, done) { + passport.serializeUser((user, done) => { done(null, user); }); - passport.deserializeUser(function (user, done) { + passport.deserializeUser((user, done) => { done(null, user); });