forked from heusalagroup/fi.hg.backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJwtDecodeServiceImpl.ts
88 lines (72 loc) · 2.91 KB
/
JwtDecodeServiceImpl.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) 2021-2023. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.
import { decode as jwsDecode } from "jws";
import { JwtDecodeService } from "../core/jwt/JwtDecodeService";
import { isReadonlyJsonObject, ReadonlyJsonObject } from "../core/Json";
import { isBoolean } from "../core/types/Boolean";
import { LogService } from "../core/LogService";
import { isString } from "../core/types/String";
import { LogLevel } from "../core/types/LogLevel";
const LOG = LogService.createLogger('JwtDecodeServiceImpl');
/**
* Jwt service implemented using "jws" NPM module.
*/
export class JwtDecodeServiceImpl implements JwtDecodeService {
public static setLogLevel (level: LogLevel) {
LOG.setLogLevel(level);
}
public static decodePayload (token: string) : ReadonlyJsonObject {
const decoded = jwsDecode(token);
LOG.debug(`decodePayload: Parsing decoded = `, decoded);
const payload = decoded?.payload;
return isReadonlyJsonObject(payload) ? payload : JSON.parse(payload);
}
public static decodePayloadAudience (token: string) : string {
const payload = JwtDecodeServiceImpl.decodePayload(token);
const aud = payload?.aud;
if (!isString(aud)) {
LOG.debug(`payload: `, payload);
throw new TypeError(`decodePayloadAudience: Payload "aud" not string: ` + token);
}
return aud;
}
public static decodePayloadSubject (token: string) : string {
const payload = JwtDecodeServiceImpl.decodePayload(token);
const sub = payload?.sub;
if (!isString(sub)) {
LOG.debug(`payload: `, payload);
throw new TypeError(`decodePayloadSubject: Payload "sub" not string: ` + token);
}
return sub;
}
public static decodePayloadVerified (token: string) : boolean {
const payload = JwtDecodeServiceImpl.decodePayload(token);
const verified = payload?.verified;
if (!isBoolean(verified)) {
LOG.debug(`payload: `, payload);
throw new TypeError(`decodePayloadVerified: Payload "verified" not boolean: ` + token);
}
return verified;
}
protected constructor (
) {
}
public static create (
) : JwtDecodeService {
return JwtDecodeServiceImpl;
}
public decodePayload (token: string): ReadonlyJsonObject {
return JwtDecodeServiceImpl.decodePayload(token);
}
public decodePayloadAudience (token: string): string {
return JwtDecodeServiceImpl.decodePayloadAudience(token);
}
public decodePayloadSubject (token: string): string {
return JwtDecodeServiceImpl.decodePayloadSubject(token);
}
public decodePayloadVerified (token: string): boolean {
return JwtDecodeServiceImpl.decodePayloadVerified(token);
}
public setLogLevel (level: LogLevel): void {
return JwtDecodeServiceImpl.setLogLevel(level);
}
}