-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathutils.js
More file actions
179 lines (165 loc) · 5.06 KB
/
utils.js
File metadata and controls
179 lines (165 loc) · 5.06 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
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
const crypto = require('crypto');
const cbor = require('cbor');
const utils = {};
/**
* Evaluates the sha256 hash of a string
* @param {string} data
* @returns {Buffer} sha256 of the input data
*/
utils.sha256 = data => {
const hash = crypto.createHash('sha256');
hash.update(data);
return hash.digest();
}
/**
* Converts a JWK to a PEM, which is compatible with
* node's crypto verify methods.
* @param {any} jwk json web token
*/
/**
* Utility function to convert a DER to a PEM
* @param {String} der base64 encoded DER
* @returns {String} PEM
*/
utils.derToPEM = der => {
return "-----BEGIN CERTIFICATE-----\n" +
der +
"\n-----END CERTIFICATE-----";
}
/**
* Turns a cert subject string into a map of its fields
* @param {string} subjectStr cert subject
* @returns {*} map of subject fields
*/
utils.parseCertSubject = (subjectStr) => {
return subjectStr
.slice(1)
.split("/")
.map(i=>i.split("="))
.reduce((a,c)=>{
a[c[0]] = c[1];
return a;
}, {});
}
/**
* Converts a COSE key to a JWK
* @param {Buffer} buffer Buffer containing cbor data with COSE key
* @returns {any} JWK object
*/
utils.coseToJwk = buffer => {
try {
let publicKeyJwk = {};
publicKeyCbor = cbor.decodeAllSync(buffer);
publicKeyCbor = publicKeyCbor[0]; //first element
if (publicKeyCbor.get(3) == -7) {
publicKeyJwk = {
kty: "EC",
crv: "P-256",
x: publicKeyCbor.get(-2).toString('base64'),
y: publicKeyCbor.get(-3).toString('base64')
}
} else if (publicKeyCbor.get(3) == -35) {
publicKeyJwk = {
kty: "EC",
crv: "P-384",
x: publicKeyCbor.get(-2).toString('base64'),
y: publicKeyCbor.get(-3).toString('base64')
}
} else if (publicKeyCbor.get(3) == -36) {
publicKeyJwk = {
kty: "EC",
crv: "P-521",
x: publicKeyCbor.get(-2).toString('base64'),
y: publicKeyCbor.get(-3).toString('base64')
}
} else if (publicKeyCbor.get(3) == -257) {
publicKeyJwk = {
kty: "RSA",
n: publicKeyCbor.get(-1).toString('base64'),
e: publicKeyCbor.get(-2).toString('base64')
}
} else if (publicKeyCbor.get(3) == -8) {
publicKeyJwk = {
key : {
kty: "OKP",
crv: "Ed25519",
x: publicKeyCbor.get(-2).toString('base64')
},
format: 'jwk'
}
} else if (publicKeyCbor.get(3) == -48) {
publicKeyJwk = {
kty: "AKP",
alg: "ML-DSA-44",
pub: publicKeyCbor.get(-1).toString('base64')
}
} else if (publicKeyCbor.get(3) == -49) {
publicKeyJwk = {
kty: "AKP",
alg: "ML-DSA-65",
pub: publicKeyCbor.get(-1).toString('base64')
}
} else if (publicKeyCbor.get(3) == -50) {
publicKeyJwk = {
kty: "AKP",
alg: "ML-DSA-87",
pub: publicKeyCbor.get(-1).toString('base64')
}
} else {
throw new Error("Unknown public key algorithm");
}
return publicKeyJwk;
} catch (e) {
throw new Error("Could not decode COSE Key");
}
}
/**
* Converts a COSE key to hex
* @param {Buffer} buffer Buffer containing cbor data with COSE key
* @returns {String} hex encoded
*/
utils.coseToHex = buffer => {
try {
publicKeyCbor = cbor.decodeAllSync(buffer);
publicKeyCbor = publicKeyCbor[0]; //first element
return cbor.encode(publicKeyCbor).toString('hex').toUpperCase();
} catch (e) {
throw new Error("Could not decode COSE Key");
}
}
/**
* Returns a default value if the provided string is undefined
* @param {string} str
* @param {string} defaultStr
* @returns {string}
*/
utils.defaultTo = (str, defaultStr) => {
if (typeof(str) === 'undefined') {
return defaultStr;
} else {
return str;
}
}
/**
* Normalizes a username coming from the client.
* @param {unknown} username
* @returns {string}
*/
utils.normalizeUsername = (username) => {
return String(username || '').trim().toLowerCase();
};
/**
* Hashes the client-provided username (after normalization) to a stable identifier.
* If UID_HASH_SECRET is set, uses HMAC-SHA256 for better resistance to offline guessing.
* @param {unknown} username
* @returns {string} hex-encoded hash
*/
utils.hashUsername = (username) => {
const normalized = utils.normalizeUsername(username);
const secret = process.env.UID_HASH_SECRET;
if (secret && String(secret).length > 0) {
return crypto.createHmac('sha256', String(secret)).update(normalized).digest('hex');
}
return crypto.createHash('sha256').update(normalized).digest('hex');
};
module.exports = utils;