-
Notifications
You must be signed in to change notification settings - Fork 1
/
TokenDto.ts
297 lines (244 loc) · 8.77 KB
/
TokenDto.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import { IsArray, IsDate, IsString, validateSync, ValidationError } from 'class-validator';
export interface TokenFields {
Issuer: string;
NotBefore: Date;
IssuedAt: Date;
Expiration: Date;
Audience: Array<string>;
Scope: Array<string>;
AuthenticationMethod: Array<string>;
ClientID: string;
Subject: string;
AuthenticationTime: Date;
IdentityProvider: string;
Name: string;
KretaUserName: string;
KretaInstituteUserUniqueId: string;
KretaSchoolYearId: string;
Role: string;
KretaInstituteUserIdpUniqueId: string;
KretaInstituteCode: string;
KretaInstituteUserId: string;
KretaSchoolYearUniqueId: string;
KretaInstituteUniqueId: string;
KretaUserType: string;
JWTID: string;
}
export default class TokenDto implements Partial<TokenFields> {
@IsString()
private readonly _iss?: string;
@IsDate()
private readonly _nbf?: Date;
@IsDate()
private readonly _iat?: Date;
@IsDate()
private readonly _exp?: Date;
@IsArray()
@IsString({ each: true })
private readonly _aud?: Array<string>;
@IsArray()
@IsString({ each: true })
private readonly _scope?: Array<string>;
@IsArray()
@IsString({ each: true })
private readonly _amr?: Array<string>;
@IsString()
private readonly _client_id?: string;
@IsString()
private readonly _sub?: string;
@IsDate()
private readonly _auth_time?: Date;
@IsString()
private readonly _idp?: string;
@IsString()
private readonly _name?: string;
@IsString()
private readonly _kreta_user_name?: string;
@IsString()
private readonly _kreta_institute_user_unique_id?: string;
@IsString()
private readonly _kreta_school_year_id?: string;
@IsString()
private readonly _role?: string;
@IsString()
private readonly _kreta_institute_user_idp_unique_id?: string;
@IsString()
private readonly _kreta_institute_code?: string;
@IsString()
private readonly _kreta_institute_user_id?: string;
@IsString()
private readonly _kreta_school_year_unique_id?: string;
@IsString()
private readonly _kreta_institute_unique_id?: string;
@IsString()
private readonly _kreta_user_type?: string;
@IsString()
private readonly _jti?: string;
constructor(input: any) {
if (typeof input === 'object' && input !== null) {
this._iss = typeof input['iss'] === 'string' ? input['iss'] : undefined;
this._nbf = typeof input['nbf'] === 'number' ? new Date(input['nbf'] * 1000) : undefined;
this._iat = typeof input['iat'] === 'number' ? new Date(input['iat'] * 1000) : undefined;
this._exp = typeof input['exp'] === 'number' ? new Date(input['exp'] * 1000) : undefined;
this._aud = Array.isArray(input['aud']) ? input['aud'].map((e: any) => e.trim()) : [];
this._scope = Array.isArray(input['scope']) ? input['scope'].map((e: any) => e.trim()) : [];
this._amr = Array.isArray(input['amr']) ? input['amr'].map((e: any) => e.trim()) : [];
this._client_id = typeof input['client_id'] === 'string' ? input['client_id'] : undefined;
this._sub = typeof input['sub'] === 'string' ? input['sub'] : undefined;
this._auth_time = typeof input['auth_time'] === 'number' ? new Date(input['auth_time'] * 1000) : undefined;
this._idp = typeof input['idp'] === 'string' ? input['idp'] : undefined;
this._name = typeof input['name'] === 'string' ? input['name'] : undefined;
this._kreta_user_name = typeof input['kreta:user_name'] === 'string' ? input['kreta:user_name'] : undefined;
this._kreta_institute_user_unique_id = typeof input['kreta:institute_user_unique_id'] === 'string' ?
input['kreta:institute_user_unique_id'] : undefined;
this._kreta_school_year_id = typeof input['kreta:school_year_id'] === 'string' ? input['kreta:school_year_id'] : undefined;
this._role = typeof input['role'] === 'string' ? input['role'] : undefined;
this._kreta_institute_user_idp_unique_id = typeof input['kreta:institute_user_idp_unique_id'] === 'string' ?
input['kreta:institute_user_idp_unique_id'] : undefined;
this._kreta_institute_code = typeof input['kreta:institute_code'] === 'string' ? input['kreta:institute_code'] : undefined;
this._kreta_institute_user_id = typeof input['kreta:institute_user_id'] === 'string' ? input['kreta:institute_user_id'] : undefined;
this._kreta_school_year_unique_id = typeof input['kreta:school_year_unique_id'] === 'string' ? input['kreta:school_year_unique_id'] :
undefined;
this._kreta_institute_unique_id = typeof input['kreta:institute_unique_id'] === 'string' ? input['kreta:institute_unique_id'] :
undefined;
this._kreta_user_type = typeof input['kreta:user_type'] === 'string' ? input['kreta:user_type'] : undefined;
this._jti = typeof input['jti'] === 'string' ? input['jti'] : undefined;
}
const errors = validateSync(this, { skipMissingProperties: true });
if (errors.length > 0) {
throw this.validationErrorResponse(errors);
}
}
public get Issuer(): string | undefined {
return this._iss;
}
public get NotBefore(): Date | undefined {
return this._nbf;
}
public get IssuedAt(): Date | undefined {
return this._iat;
}
public get Expiration(): Date | undefined {
return this._exp;
}
public get Audience(): Array<string> | undefined {
return this._aud;
}
public get Scope(): Array<string> | undefined {
return this._scope;
}
public get AuthenticationMethod(): Array<string> | undefined {
return this._amr;
}
public get ClientID(): string | undefined {
return this._client_id;
}
public get Subject(): string | undefined {
return this._sub;
}
public get AuthenticationTime(): Date | undefined {
return this._auth_time;
}
public get IdentityProvider(): string | undefined {
return this._idp;
}
public get Name(): string | undefined {
return this._name;
}
public get KretaUserName(): string | undefined {
return this._kreta_user_name;
}
public get KretaInstituteUserUniqueId(): string | undefined {
return this._kreta_institute_user_unique_id;
}
public get KretaSchoolYearId(): string | undefined {
return this._kreta_school_year_id;
}
public get Role(): string | undefined {
return this._role;
}
public get KretaInstituteUserIdpUniqueId(): string | undefined {
return this._kreta_institute_user_idp_unique_id;
}
public get KretaInstituteCode(): string | undefined {
return this._kreta_institute_code;
}
public get KretaInstituteUserId(): string | undefined {
return this._kreta_institute_user_id;
}
public get KretaSchoolYearUniqueId(): string | undefined {
return this._kreta_school_year_unique_id;
}
public get KretaInstituteUniqueId(): string | undefined {
return this._kreta_institute_unique_id;
}
public get KretaUserType(): string | undefined {
return this._kreta_user_type;
}
public get JWTID(): string | undefined {
return this._jti;
}
public get json(): TokenFields {
return {
Audience: this._aud,
AuthenticationMethod: this._amr,
AuthenticationTime: this._auth_time,
ClientID: this._client_id,
Expiration: this._exp,
IdentityProvider: this._idp,
IssuedAt: this._iat,
Issuer: this._iss,
JWTID: this._jti,
KretaInstituteCode: this._kreta_institute_code,
KretaInstituteUniqueId: this._kreta_institute_unique_id,
KretaInstituteUserId: this._kreta_institute_user_id,
KretaInstituteUserIdpUniqueId: this._kreta_institute_user_idp_unique_id,
KretaInstituteUserUniqueId: this._kreta_institute_user_unique_id,
KretaSchoolYearId: this._kreta_school_year_id,
KretaSchoolYearUniqueId: this._kreta_school_year_unique_id,
KretaUserName: this._kreta_user_name,
KretaUserType: this._kreta_user_type,
Name: this._name,
NotBefore: this._nbf,
Role: this._role,
Scope: this._scope,
Subject: this._sub,
} as TokenFields;
}
private validationErrorResponse(errors: Array<ValidationError>): object {
const validFields: Partial<TokenFields> = {
Issuer: this._iss,
NotBefore: this._nbf,
IssuedAt: this._iat,
Expiration: this._exp,
Audience: this._aud,
Scope: this._scope,
AuthenticationMethod: this._amr,
ClientID: this._client_id,
Subject: this._sub,
AuthenticationTime: this._auth_time,
IdentityProvider: this._idp,
Name: this._name,
KretaUserName: this._kreta_user_name,
KretaInstituteUserUniqueId: this._kreta_institute_user_unique_id,
KretaSchoolYearId: this._kreta_school_year_id,
Role: this._role,
KretaInstituteUserIdpUniqueId: this._kreta_institute_user_idp_unique_id,
KretaInstituteCode: this._kreta_institute_code,
KretaInstituteUserId: this._kreta_institute_user_id,
KretaSchoolYearUniqueId: this._kreta_school_year_unique_id,
KretaInstituteUniqueId: this._kreta_institute_unique_id,
KretaUserType: this._kreta_user_type,
JWTID: this._jti,
};
const errorMessages: Array<string> = [];
for (const error of errors) {
validFields[error.property as keyof TokenFields] = undefined;
errorMessages.push(...Object.values(error.constraints || {}));
}
return {
valid: validFields,
errors: errorMessages,
};
}
}