-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
70 lines (63 loc) · 2.05 KB
/
auth.js
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
// @ts-check
/** @typedef {import("ipfs-core/src/components").IPFSAPI} Node */
/** @typedef {import('@cuser/proto/types/payloads').PayloadUser} PayloadUser */
/** @typedef {import('@cuser/crypto/types/bearer').CoreCryptoBearer} CuserCryptoBearer */
/** @typedef {import('@cuser/crypto/types/keygen').CuserCryptoKeygenOptions} CuserCryptoKeygenOptions */
/** @typedef {import('@cuser/crypto/types/keygen').CuserCryptoKeygen} CuserCryptoKeygen */
const createBearer = require('@cuser/crypto/bearer');
const createHash = require('@cuser/crypto/hash');
const createKeygen = require('@cuser/crypto/keygen');
const userSchema = require('@cuser/proto/schemas/PayloadUser.json');
const validate = require('@cuser/validator')(userSchema);
/**
* @typedef {CuserCryptoKeygenOptions} CuserAuthOptions
*/
/**
* @typedef {String} CuserAuthAccessToken
*/
/**
* Auth controller
*/
class CuserAuth {
/**
* @param {Node|Promise<Node>} node
* @param {String} secret
* @param {CuserAuthOptions} [opts]
*/
constructor(node, secret, opts) {
const hash = createHash(secret, 'base64');
/** @type {CuserCryptoKeygen} */
this._keygen = createKeygen(node, hash, opts);
/** @type {Promise<CuserCryptoBearer>} */
this._bearer = this._keygen.generateKeys().then(({ privateKey, publicKey }) => createBearer(hash, {
privateKey,
publicKey,
}));
}
/**
* @param {PayloadUser} payload
* @returns {Promise<CuserAuthAccessToken>}
*/
async authenticate(payload) {
validate(payload);
const bearer = await this._bearer;
return bearer.encode(payload);
}
/**
* @param {String} accessToken
* @returns {Promise<PayloadUser & { iat: Number }>}
*/
async decode(accessToken) {
const bearer = await this._bearer;
return bearer.decode(accessToken);
}
}
/**
* @param {Node|Promise<Node>} node
* @param {String} secret
* @param {CuserAuthOptions} [opts]
* @returns {CuserAuth}
*/
const createAuth = (node, secret, opts) => new CuserAuth(node, secret, opts);
module.exports = createAuth;
module.exports.CuserAuth = CuserAuth;