From f0e1f14c95089e143651559306014734bcaefcc6 Mon Sep 17 00:00:00 2001 From: Adrian Andersen Date: Wed, 18 Oct 2023 10:21:04 +0200 Subject: [PATCH] Cleanup & Deployment Fix (#474) * refactor: remove dead Feide logic * build: add .nvmrc file * test: remove feide references --- .nvmrc | 1 + src/application-config.ts | 3 - src/auth/bl.auth.ts | 2 - src/auth/feide/feide.auth.ts | 113 ------------------ .../user/user-provider/user-provider.spec.ts | 10 +- 5 files changed, 6 insertions(+), 123 deletions(-) create mode 100644 .nvmrc delete mode 100644 src/auth/feide/feide.auth.ts diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 00000000..3f430af8 --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +v18 diff --git a/src/application-config.ts b/src/application-config.ts index 0abb5569..2829be91 100644 --- a/src/application-config.ts +++ b/src/application-config.ts @@ -88,9 +88,6 @@ export const APP_CONFIG = { local: { name: "local", }, - feide: { - name: "oauth2", - }, }, token: { refresh: { diff --git a/src/auth/bl.auth.ts b/src/auth/bl.auth.ts index 2fdbb34e..6e4be400 100644 --- a/src/auth/bl.auth.ts +++ b/src/auth/bl.auth.ts @@ -1,7 +1,6 @@ import { Router } from "express"; import { FacebookAuth } from "./facebook/facebook.auth"; -import { FeideAuth } from "./feide/feide.auth"; import { GoogleAuth } from "./google/google.auth"; import { LocalLoginCreator } from "./local/local-login-creator/local-login-creator"; import { LocalLoginHandler } from "./local/local-login.handler"; @@ -54,6 +53,5 @@ export class BlAuth { new FacebookAuth(router, resHandler); new LocalAuth(router, resHandler, localLoginValidator, tokenHandler); new TokenEndpoint(router, resHandler, tokenHandler); - new FeideAuth(router, resHandler); } } diff --git a/src/auth/feide/feide.auth.ts b/src/auth/feide/feide.auth.ts deleted file mode 100644 index 6a8c4c5d..00000000 --- a/src/auth/feide/feide.auth.ts +++ /dev/null @@ -1,113 +0,0 @@ -import { BlError } from "@boklisten/bl-model"; -import { Router } from "express"; -import passport from "passport"; -import OAuth2Strategy from "passport-oauth2"; - -import { APP_CONFIG } from "../../application-config"; -import { ApiPath } from "../../config/api-path"; -import { HttpHandler } from "../../http/http.handler"; -import { SEResponseHandler } from "../../response/se.response.handler"; -import { UserProvider } from "../user/user-provider/user-provider"; - -export class FeideAuth { - private apiPath: ApiPath; - private httpHandler: HttpHandler; - private _userProvider: UserProvider; - - constructor( - router: Router, - private resHandler: SEResponseHandler, - ) { - this.apiPath = new ApiPath(); - this.createPassportStrategy(); - this.createAuthGet(router); - this.createCallbackGet(router); - this.httpHandler = new HttpHandler(); - this._userProvider = new UserProvider(); - } - - private createPassportStrategy() { - passport.use( - new OAuth2Strategy( - { - authorizationURL: process.env.FEIDE_AUTHORIZATION_URL, - tokenURL: process.env.FEIDE_TOKEN_URL, - clientID: process.env.FEIDE_CLIENT_ID, - clientSecret: process.env.FEIDE_SECRET, - passReqToCallback: true, - callbackURL: - process.env.BL_API_URI + - this.apiPath.createPath("auth/feide/callback"), - }, - async (req, feideAccessToken, refreshToken, profile, done) => { - let feideUserInfo; - - try { - feideUserInfo = await this.httpHandler.get( - process.env.FEIDE_USER_INFO_URL, - "Bearer " + feideAccessToken, - ); - } catch (e) { - done(new Error("something went wrong with feide login")); - } - - const feideUser = feideUserInfo["user"]; - const feideEmail = feideUser["email"]; - const feideUserId = feideUser["userid"]; - const provider = APP_CONFIG.login.feide.name; - - let userAndTokens; - - try { - userAndTokens = await this._userProvider.loginOrCreate( - feideEmail, - provider, - feideUserId, - ); - } catch (e) { - return done( - null, - null, - new BlError("could not create user").code(902), - ); - } - - done(null, userAndTokens.tokens); - }, - ), - ); - } - - private createAuthGet(router: Router) { - router.get( - this.apiPath.createPath("auth/feide"), - passport.authenticate(APP_CONFIG.login.feide.name, { - scope: ["profile", "email", "userid"], - }), - ); - } - - private createCallbackGet(router: Router) { - router.get(this.apiPath.createPath("auth/feide/callback"), (req, res) => { - passport.authenticate( - APP_CONFIG.login.feide.name, - (err, tokens, blError: BlError) => { - if (!tokens && (err || blError)) { - return res.redirect( - process.env.CLIENT_URI + - APP_CONFIG.path.client.auth.socialLoginFailure, - ); - } - if (tokens) { - this.resHandler.sendAuthTokens( - res, - tokens.accessToken, - tokens.refreshToken, - this.apiPath.retrieveRefererPath(req.headers), - ); - } - }, - )(req, res); - }); - } -} diff --git a/src/auth/user/user-provider/user-provider.spec.ts b/src/auth/user/user-provider/user-provider.spec.ts index 7d63b6a3..7b7303e9 100644 --- a/src/auth/user/user-provider/user-provider.spec.ts +++ b/src/auth/user/user-provider/user-provider.spec.ts @@ -47,7 +47,7 @@ describe("UserProvider", () => { userValidStub.rejects(new BlError("user is not valid")); return expect( - userProvider.loginOrCreate("username@mail.com", "feide", "abcdef"), + userProvider.loginOrCreate("username@mail.com", "local", "abcdef"), ).to.eventually.be.rejectedWith(BlError, /user is not valid/); }); @@ -59,7 +59,7 @@ describe("UserProvider", () => { ); return expect( - userProvider.loginOrCreate("username@mail.com", "feide", "abcde"), + userProvider.loginOrCreate("username@mail.com", "local", "abcde"), ).to.eventually.be.rejectedWith( BlError, /local login could not be created/, @@ -72,7 +72,7 @@ describe("UserProvider", () => { userCreateStub.rejects(new BlError("user could not be created")); return expect( - userProvider.loginOrCreate("username@mail.com", "feide", "abcde"), + userProvider.loginOrCreate("username@mail.com", "local", "abcde"), ).to.eventually.be.rejectedWith(BlError, /user could not be created/); }); @@ -86,7 +86,7 @@ describe("UserProvider", () => { createTokenStub.resolves(tokens); return expect( - userProvider.loginOrCreate("username@mail.com", "feide", "abcdefg"), + userProvider.loginOrCreate("username@mail.com", "local", "abcdefg"), ).to.eventually.be.eql({ user: user, tokens: tokens }); }); }); @@ -102,7 +102,7 @@ describe("UserProvider", () => { createTokenStub.resolves(tokens); return expect( - userProvider.loginOrCreate("username@mail.com", "feide", "abcdefg"), + userProvider.loginOrCreate("username@mail.com", "local", "abcdefg"), ).to.eventually.be.eql({ user: user, tokens: tokens }); }); });