Fix PR comments
This commit is contained in:
@@ -17,31 +17,12 @@
|
||||
import passport from 'passport';
|
||||
import express from 'express';
|
||||
import { Strategy as GoogleStrategy } from 'passport-google-oauth20';
|
||||
import {
|
||||
AuthProvider,
|
||||
AuthProviderRouteHandlers,
|
||||
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(`
|
||||
<html>
|
||||
<body>
|
||||
<script>
|
||||
(window.opener || window.parent).postMessage(JSON.parse(atob('${base64Data}')), location.origin)
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
`);
|
||||
};
|
||||
import { AuthProvider, AuthProviderRouteHandlers } from './../types';
|
||||
import { postMessageResponse } from './../utils';
|
||||
|
||||
export class GoogleAuthProvider
|
||||
implements AuthProvider, AuthProviderRouteHandlers {
|
||||
providerConfig: any;
|
||||
private readonly providerConfig: any;
|
||||
constructor(providerConfig: any) {
|
||||
this.providerConfig = providerConfig;
|
||||
}
|
||||
@@ -58,6 +39,7 @@ export class GoogleAuthProvider
|
||||
prompt: 'consent',
|
||||
})(req, res, next);
|
||||
}
|
||||
|
||||
frameHandler(
|
||||
req: express.Request,
|
||||
res: express.Response,
|
||||
|
||||
@@ -35,13 +35,13 @@ export const defaultRouter = (provider: AuthProviderRouteHandlers) => {
|
||||
};
|
||||
|
||||
export const makeProvider = (config: any) => {
|
||||
const provider = config.provider;
|
||||
const ProviderImpl = providerFactories[provider];
|
||||
const providerId = config.provider;
|
||||
const ProviderImpl = providerFactories[providerId];
|
||||
if (!ProviderImpl) {
|
||||
throw Error(`Provider Implementation missing for provider: ${provider}`);
|
||||
throw Error(`Provider Implementation missing for provider: ${providerId}`);
|
||||
}
|
||||
const providerInstance = new ProviderImpl(config);
|
||||
const strategy = providerInstance.strategy();
|
||||
const providerRouter = defaultRouter(providerInstance);
|
||||
return { provider, strategy, providerRouter };
|
||||
return { providerId, strategy, providerRouter };
|
||||
};
|
||||
|
||||
@@ -53,7 +53,6 @@ export type AuthProviderFactories = {
|
||||
export type AuthInfo = {
|
||||
profile: passport.Profile;
|
||||
accessToken: string;
|
||||
refreshToken?: string;
|
||||
expiresAt?: number;
|
||||
};
|
||||
|
||||
@@ -64,5 +63,5 @@ export type AuthResponse =
|
||||
}
|
||||
| {
|
||||
type: 'auth-result';
|
||||
error: Error | undefined;
|
||||
error: Error;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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 { AuthResponse } from './types';
|
||||
|
||||
export 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(`
|
||||
<html>
|
||||
<body>
|
||||
<script>
|
||||
(window.opener || window.parent).postMessage(JSON.parse(atob('${base64Data}')), location.origin)
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
`);
|
||||
};
|
||||
@@ -33,10 +33,12 @@ export async function createRouter(
|
||||
|
||||
// configure all the providers
|
||||
for (const providerConfig of providers) {
|
||||
logger.info('Configuring providers');
|
||||
const { provider, strategy, providerRouter } = makeProvider(providerConfig);
|
||||
const { providerId, strategy, providerRouter } = makeProvider(
|
||||
providerConfig,
|
||||
);
|
||||
logger.info(`Configuring provider: ${providerId}`);
|
||||
passport.use(strategy);
|
||||
router.use(`/${provider}`, providerRouter);
|
||||
router.use(`/${providerId}`, providerRouter);
|
||||
}
|
||||
|
||||
passport.serializeUser((user, done) => {
|
||||
|
||||
Reference in New Issue
Block a user