Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
joonashak committed Nov 6, 2024
1 parent 9a2fe8f commit 071c0bb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 34 deletions.
2 changes: 2 additions & 0 deletions lib/src/api/clone-bay-sso.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -15,6 +16,7 @@ import { SsoController } from "../sso/sso.controller";
*/
@Module({
imports: [EveAuthSsoModule, CharacterModule],
providers: [SsoService],
controllers: [SsoController],
})
export class CloneBaySsoModule {}
38 changes: 4 additions & 34 deletions lib/src/sso/sso.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
EveSsoCallbackParams,
RequireSsoAuth,
SsoService,
} from "@joonashak/nestjs-eve-auth";
import {
Controller,
Expand All @@ -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.
Expand Down Expand Up @@ -66,26 +55,7 @@ export class SsoController {
@Session() session: Record<string, any>,
@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);
}
}
50 changes: 50 additions & 0 deletions lib/src/sso/sso.service.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
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
);
}
}
2 changes: 2 additions & 0 deletions lib/test/testing-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -37,6 +38,7 @@ export const createTestingApp = async (): Promise<INestApplication> => {
UserService,
UserCacheService,
AltService,
SsoService,
],
}).compile();

Expand Down

0 comments on commit 071c0bb

Please sign in to comment.