Fix lint issues
This commit is contained in:
@@ -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',
|
||||
|
||||
@@ -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(`
|
||||
<html>
|
||||
<body>
|
||||
<script>
|
||||
(window.opener || window.parent).postMessage(JSON.parse(atob('${base64Data}')), location.origin)
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
`);
|
||||
};
|
||||
|
||||
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(`
|
||||
<html>
|
||||
<body>
|
||||
<script>
|
||||
(window.opener || window.parent).postMessage(JSON.parse(atob('${base64Data}')), location.origin)
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
`);
|
||||
};
|
||||
|
||||
@@ -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 };
|
||||
};
|
||||
|
||||
@@ -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';
|
||||
|
||||
|
||||
@@ -29,19 +29,21 @@ export async function createRouter(
|
||||
options: RouterOptions,
|
||||
): Promise<express.Router> {
|
||||
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);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user