54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
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",
|
|
}),
|
|
);
|
|
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) {
|
|
throw new Error();
|
|
}
|
|
}
|
|
}
|