-
Notifications
You must be signed in to change notification settings - Fork 0
/
sso.service.ts
52 lines (49 loc) · 1.71 KB
/
sso.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { SsoInfo } from './../models/sso.model';
import { ForbiddenException, Injectable, InternalServerErrorException, UnauthorizedException } from '@nestjs/common';
import qs from 'qs';
import { AppLogger } from 'src/utils/app-logger.util';
import { HttpService } from '@nestjs/axios';
import { firstValueFrom } from 'rxjs';
@Injectable()
export class SsoService {
constructor(
private readonly _http: HttpService,
private readonly _logger: AppLogger,
) { }
/**
* Loggin with the SSO portal
* In case of bad credential throw an error
* @returns a sso token to possibly get user infos
*/
public async login(username: string, password: string): Promise<string> {
try {
const response = await firstValueFrom(this._http.post('https://sso-portal.isep.fr', qs.stringify({ user: username, password })));
return response.headers["set-cookie"][0].match(/lemonldap=([^;]+);/)[1];
} catch (e) {
if (e.response.data?.error == 5)
throw new UnauthorizedException("Bad credentials");
else {
this._logger.error("Sso error", e);
throw new InternalServerErrorException("SSO error");
}
}
}
/**
* Get user infos from the SSO portal
* @param token The token from the authentified user
* @returns A Promise with the user infos
*/
public async getUser(token: string): Promise<SsoInfo> {
try {
const response = await firstValueFrom(this._http.get<SsoInfo>(`https://sso-portal.isep.fr/session/my/global`, {
headers: {
Cookie: `lemonldap=${token};`
}
}));
return response.data;
} catch (e) {
this._logger.error("Sso error", e);
throw new InternalServerErrorException("SSO error");
}
}
}