-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidators.ts
More file actions
124 lines (111 loc) · 2.95 KB
/
Validators.ts
File metadata and controls
124 lines (111 loc) · 2.95 KB
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
import { JwtPayload } from "jsonwebtoken";
import _ from "lodash";
import {
Algorithm,
AuthMethod,
AuthMethods,
HSA,
RSA,
TokenGetter,
} from "src/interfaces";
import { getDefaultTokenGetter } from "./TokenGetter";
export const isValidAuthMethod = (method: unknown): method is AuthMethod =>
_.isString(method) && method in AuthMethods;
export const isValidAlgorithm = (algorithm: unknown): algorithm is Algorithm =>
_.includes(["HS256", "HS384", "HS512", "RS256", "RS384", "RS512"], algorithm);
export const isValidAlgorithms = (
algorithms: unknown[]
): algorithms is Algorithm[] => _.every(algorithms, isValidAlgorithm);
export const isHSA = (algorithm: unknown): algorithm is HSA =>
_.includes(["HS256", "HS384", "HS512"], algorithm);
export const isRSA = (algorithm: unknown): algorithm is RSA =>
_.includes(["RS256", "RS384", "RS512"], algorithm);
export const hasIss = (
payload: string | JwtPayload
): payload is JwtPayload & { iss: string } => {
return typeof payload === "object" && payload.iss !== undefined;
};
export const validateAndGetAlgorithms = (
algorithms?: unknown[]
): Algorithm[] => {
if (algorithms === undefined) {
return ["HS256", "RS256"];
}
if (_.isEmpty(algorithms)) {
throw new RangeError("");
}
if (!isValidAlgorithms(algorithms)) {
throw new RangeError("");
}
return algorithms;
};
export const validateAndGetMethod = (method?: unknown): AuthMethod => {
if (method === undefined) {
return "Bearer";
}
if (isValidAuthMethod(method)) {
return method;
} else {
throw new RangeError("");
}
};
export const validateAndGetTokenGetter = (
method: AuthMethod,
getToken?: unknown
): TokenGetter => {
if (getToken === undefined) {
return getDefaultTokenGetter(method);
}
if (typeof getToken === "function") {
return getToken as TokenGetter;
} else {
throw new RangeError("");
}
};
export const validateAndGetCacheTime = (cacheTime?: unknown): number => {
if (cacheTime === undefined) {
return 24;
}
if (_.isNumber(cacheTime)) {
return cacheTime;
} else {
throw new RangeError("");
}
};
export const validateAndGetRequestProperty = (
requestProperty?: unknown
): string => {
if (requestProperty === undefined) {
return "authUser";
}
if (_.isString(requestProperty)) {
return requestProperty;
} else {
throw new RangeError("");
}
};
export const validateAndGetIssuers = (issuer: unknown): string[] => {
if (issuer === undefined) {
throw new RangeError("");
} else if (_.isString(issuer)) {
return [issuer];
} else if (
_.isArray(issuer) &&
!_.isEmpty(issuer) &&
_.every(issuer, (iss) => _.isString(iss))
) {
return issuer as string[];
} else {
throw new RangeError("");
}
};
export const validateAndGetTokenKey = (tokenKey?: unknown): string => {
if (tokenKey === undefined) {
return "token";
}
if (_.isString(tokenKey)) {
return tokenKey;
} else {
throw new RangeError("");
}
};