add user auth from idNot
This commit is contained in:
parent
1051ed71e2
commit
04420ab5ed
32
src/app/api/idnot-user/UserInfoController.ts
Normal file
32
src/app/api/idnot-user/UserInfoController.ts
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
},
|
||||
};
|
||||
|
@ -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);
|
||||
|
52
src/services/private-services/AuthService/AuthService.ts
Normal file
52
src/services/private-services/AuthService/AuthService.ts
Normal file
@ -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<IdNotTokens> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user