From 071c0bba3c0593c4f03af2033b179deb54054863 Mon Sep 17 00:00:00 2001 From: joonashak Date: Wed, 6 Nov 2024 20:41:51 +0200 Subject: [PATCH] Refactor --- lib/src/api/clone-bay-sso.module.ts | 2 ++ lib/src/sso/sso.controller.ts | 38 +++------------------- lib/src/sso/sso.service.ts | 50 +++++++++++++++++++++++++++++ lib/test/testing-app.ts | 2 ++ 4 files changed, 58 insertions(+), 34 deletions(-) create mode 100644 lib/src/sso/sso.service.ts diff --git a/lib/src/api/clone-bay-sso.module.ts b/lib/src/api/clone-bay-sso.module.ts index 89eed0e..45da20d 100644 --- a/lib/src/api/clone-bay-sso.module.ts +++ b/lib/src/api/clone-bay-sso.module.ts @@ -2,6 +2,7 @@ import { SsoModule as EveAuthSsoModule } from "@joonashak/nestjs-eve-auth"; import { Module } from "@nestjs/common"; import { CharacterModule } from "../entities/character/character.module"; import { SsoController } from "../sso/sso.controller"; +import { SsoService } from "../sso/sso.service"; /** * Ready-to-use HTTP implementation for EVE SSO login. @@ -15,6 +16,7 @@ import { SsoController } from "../sso/sso.controller"; */ @Module({ imports: [EveAuthSsoModule, CharacterModule], + providers: [SsoService], controllers: [SsoController], }) export class CloneBaySsoModule {} diff --git a/lib/src/sso/sso.controller.ts b/lib/src/sso/sso.controller.ts index acc07b3..65285b4 100644 --- a/lib/src/sso/sso.controller.ts +++ b/lib/src/sso/sso.controller.ts @@ -1,7 +1,6 @@ import { EveSsoCallbackParams, RequireSsoAuth, - SsoService, } from "@joonashak/nestjs-eve-auth"; import { Controller, @@ -14,22 +13,12 @@ import { ValidationPipe, } from "@nestjs/common"; import { Response } from "express"; -import { AuthenticationService } from "../authentication/authentication.service"; -import { getUserId, setUserId } from "../common/utils/session.util"; -import { ModuleConfigService } from "../config/module-config.service"; -import { CharacterService } from "../entities/character/character.service"; -import { AltService } from "../entities/user/alt.service"; import { HttpExceptionFilter } from "../filters/http-exception.filter"; +import { SsoService } from "./sso.service"; @Controller() export class SsoController { - constructor( - private ssoService: SsoService, - private characterService: CharacterService, - private authenticationService: AuthenticationService, - private altService: AltService, - private moduleConfigService: ModuleConfigService, - ) {} + constructor(private ssoService: SsoService) {} /** * Start SSO login process. @@ -66,26 +55,7 @@ export class SsoController { @Session() session: Record, @Res() response: Response, ) { - const { - character: { id: characterId }, - tokens, - } = await this.ssoService.callback(callbackParams, session); - - const esiCharacter = await this.characterService.addPublicInfoFromEsi({ - eveId: characterId, - ...tokens, - }); - - const loggedInUserId = getUserId(session); - if (loggedInUserId) { - this.altService.addAlt(esiCharacter, loggedInUserId); - } else { - const user = await this.authenticationService.ssoLogin(esiCharacter); - setUserId(session, user.id); - } - - response.redirect( - session.afterLoginUrl || this.moduleConfigService.config.afterLoginUrl, - ); + const redirectUrl = await this.ssoService.login(callbackParams, session); + response.redirect(redirectUrl); } } diff --git a/lib/src/sso/sso.service.ts b/lib/src/sso/sso.service.ts new file mode 100644 index 0000000..8fdb70c --- /dev/null +++ b/lib/src/sso/sso.service.ts @@ -0,0 +1,50 @@ +import { + SsoService as EveAuthSsoService, + EveSsoCallbackParams, +} from "@joonashak/nestjs-eve-auth"; +import { Injectable } from "@nestjs/common"; +import { get } from "lodash"; +import { AuthenticationService } from "../authentication/authentication.service"; +import { getUserId, setUserId } from "../common/utils/session.util"; +import { ModuleConfigService } from "../config/module-config.service"; +import { CharacterService } from "../entities/character/character.service"; +import { AltService } from "../entities/user/alt.service"; + +@Injectable() +export class SsoService { + constructor( + private eveAuthSsoService: EveAuthSsoService, + private characterService: CharacterService, + private authenticationService: AuthenticationService, + private altService: AltService, + private moduleConfigService: ModuleConfigService, + ) {} + + async login( + callbackParams: EveSsoCallbackParams, + session: unknown, + ): Promise { + const { + character: { id: characterId }, + tokens, + } = await this.eveAuthSsoService.callback(callbackParams, session); + + const esiCharacter = await this.characterService.addPublicInfoFromEsi({ + eveId: characterId, + ...tokens, + }); + + const loggedInUserId = getUserId(session); + if (loggedInUserId) { + this.altService.addAlt(esiCharacter, loggedInUserId); + } else { + const user = await this.authenticationService.ssoLogin(esiCharacter); + setUserId(session, user.id); + } + + return ( + get(session, "afterLoginUrl") || + this.moduleConfigService.config.afterLoginUrl + ); + } +} diff --git a/lib/test/testing-app.ts b/lib/test/testing-app.ts index 59ecdc0..19bc2dd 100644 --- a/lib/test/testing-app.ts +++ b/lib/test/testing-app.ts @@ -10,6 +10,7 @@ import { UserCacheService } from "../src/entities/user/user-cache.service"; import { User, UserSchema } from "../src/entities/user/user.model"; import { UserService } from "../src/entities/user/user.service"; import { SsoController } from "../src/sso/sso.controller"; +import { SsoService } from "../src/sso/sso.service"; import { MockCacheService } from "./mocks/cache.service.mock"; import { provideMockDynamicConfigService } from "./mocks/dynamic-config.service.mock"; import { MockEsiService } from "./mocks/esi-service.mock"; @@ -37,6 +38,7 @@ export const createTestingApp = async (): Promise => { UserService, UserCacheService, AltService, + SsoService, ], }).compile();