diff --git a/src/app/api/idnot-user/UserInfoController.ts b/src/app/api/idnot-user/UserInfoController.ts new file mode 100644 index 00000000..f3854a1d --- /dev/null +++ b/src/app/api/idnot-user/UserInfoController.ts @@ -0,0 +1,32 @@ +import { Response, Request } from "express"; + import { Controller,Post } from "@ControllerPattern/index"; + import ApiController from "@Common/system/controller-pattern/ApiController"; + import { Service } from "typedi"; +import AuthService from "@Services/private-services/AuthService/AuthService"; +//import User from "le-coffre-resources/dist/Notary"; + + @Controller() + @Service() + export default class UserInfoController extends ApiController { + constructor(private authService: AuthService) { + super(); + } + + /** + * @description Get user created from IdNot authentification + * @returns User + */ + @Post("/api/v1/idnot-user/:code") + protected async getUserInfosFromIdnot(req: Request, response: Response) { + try { + const code = req.params["code"]; + const user = await this.authService.getUserFromIdNotTokens(code!); + //success + this.httpSuccess(response, user); + } catch (error) { + this.httpBadRequest(response, error); + return; + } + } + +} \ No newline at end of file diff --git a/src/app/index.ts b/src/app/index.ts index f9dded35..d81c7281 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -8,6 +8,7 @@ import DeedsController from "./api/super-admin/DeedsController"; import DeedTypesController from "./api/super-admin/DeedTypesController"; import DocumentsController from "./api/super-admin/DocumentsController"; import DocumentTypesController from "./api/super-admin/DocumentTypesController"; +import IdNotUserInfoController from "./api/idnot-user/UserInfoController"; /** * @description This allow to declare all controllers used in the application @@ -23,5 +24,6 @@ export default { Container.get(DeedTypesController); Container.get(DocumentsController); Container.get(DocumentTypesController); + Container.get(IdNotUserInfoController); }, }; diff --git a/src/common/config/variables/Variables.ts b/src/common/config/variables/Variables.ts index 2ba6dbde..b6432b98 100644 --- a/src/common/config/variables/Variables.ts +++ b/src/common/config/variables/Variables.ts @@ -33,6 +33,18 @@ export class BackendVariables { public readonly NODE_ENV = process.env.NODE_ENV; + @IsNotEmpty() + public readonly IDNOT_CONNEXION_URL!: string; + + @IsNotEmpty() + public readonly IDNOT_CLIENT_ID!: string; + + @IsNotEmpty() + public readonly IDNOT_CLIENT_SECRET!: string; + + @IsNotEmpty() + public readonly IDNOT_REDIRECT_URL!: string; + public constructor() { dotenv.config(); this.DATABASE_PORT = process.env["DATABASE_PORT"]!; @@ -44,6 +56,10 @@ export class BackendVariables { this.APP_PORT = process.env["APP_PORT"]!; this.APP_ROOT_URL = process.env["APP_ROOT_URL"]!; this.APP_LABEL = process.env["APP_LABEL"]!; + this.IDNOT_CONNEXION_URL = process.env["IDNOT_CONNEXION_URL"]!; + this.IDNOT_CLIENT_ID = process.env["IDNOT_CLIENT_ID"]!; + this.IDNOT_CLIENT_SECRET = process.env["IDNOT_CLIENT_SECRET"]!; + this.IDNOT_REDIRECT_URL = process.env["IDNOT_REDIRECT_URL"]!; } public async validate() { await validateOrReject(this); diff --git a/src/services/private-services/AuthService/AuthService.ts b/src/services/private-services/AuthService/AuthService.ts new file mode 100644 index 00000000..8ea6d527 --- /dev/null +++ b/src/services/private-services/AuthService/AuthService.ts @@ -0,0 +1,52 @@ +import jwt from "jsonwebtoken"; +import BaseService from "@Services/BaseService"; +import "reflect-metadata"; +import { BackendVariables } from "@Common/config/variables/Variables"; +import Container, { Service } from "typedi"; + +type IdNotTokens = { + access_token: string, + id_token: string +} + +@Service() +export default class AuthService extends BaseService { + protected readonly variables = Container.get(BackendVariables); + private constructor() { + super(); + } + + /** + * @description : Get IdNot id_token and access_token + * @throws {Error} If jwt pair cannot be get + */ + public async getUserFromIdNotTokens(code: string) { + const tokens = await this.getIdNotTokens(code); + return jwt.decode(tokens.id_token); + } + + private async getIdNotTokens(code: string): Promise { + const url = new URL(this.variables.IDNOT_CONNEXION_URL.concat("?") + new URLSearchParams({ + client_id: this.variables.IDNOT_CLIENT_ID, + client_secret: this.variables.IDNOT_CLIENT_SECRET, + redirect_uri: this.variables.IDNOT_REDIRECT_URL, + code: code, + grant_type: "authorization_code", + })); + console.log(url.toString()); + try { + const headers = new Headers({ + "Content-Type": "application/x-www-form-urlencoded", + }); + const res = await fetch(url, { + method: "POST", + headers: headers, + }); + const data = await res.json() + return data as IdNotTokens; + } catch (error) { + console.log(error) + throw new Error(); + } + } +}