This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
155 lines (124 loc) · 4.21 KB
/
index.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
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
const { randomBytes } = require('crypto')
const { createVerifiableCredentialJwt, verifyCredential } = require('did-jwt-vc')
const { verifyJWT } = require('did-jwt')
const { getResolver } = require('ethr-did-resolver')
const { Resolver } = require('did-resolver')
const { keccak256 } = require('js-sha3')
const challenges = {}
const tokenRequestCounter = {}
let
providerConfig, ethrDidResolver, didResolver, identity,
challengeExpirationInSeconds, maxRequestsPerToken, rpcUrl, authExpirationInHours
const initializeAuth = (env) => {
if (!env) {
throw new Error('Missing env object')
}
rpcUrl = env.rpcUrl || 'https://did.testnet.rsk.co:4444'
challengeExpirationInSeconds = env.challengeExpirationInSeconds || 300
authExpirationInHours = env.authExpirationInHours || 10
maxRequestsPerToken = env.maxRequestsPerToken || 20
const { did, signer } = env
if (!did) {
throw new Error('Missing env variable: did')
}
if (!signer) {
throw new Error('Missing env variable: signer')
}
providerConfig = {
networks: [{ name: 'rsk:testnet', registry: '0xdca7ef03e98e0dc2b855be647c39abe984fcf21b', rpcUrl }]
}
ethrDidResolver = getResolver(providerConfig)
didResolver = new Resolver(ethrDidResolver)
identity = { signer, did }
}
const getChallenge = (did) => {
if (!identity) {
throw new Error('Library not initialized')
}
if (!did) {
throw new Error('Invalid did')
}
const challenge = randomBytes(64).toString('hex')
challenges[did] = challenge
setTimeout(() => delete challenges[did], challengeExpirationInSeconds * 1000)
return challenge
}
const getAuthToken = async (jwt) => {
if (!identity) {
throw new Error('Library not initialized')
}
if (!jwt) {
throw new Error('JWT VC is required')
}
const { payload, issuer } = await verifyJWT(jwt, { resolver: didResolver })
if (!challenges[issuer]) {
throw new Error('Request for a challenge before auth')
}
const challengeClaim = payload.vc.credentialSubject.claims.find(
(claim) => claim.claimType === 'challenge'
)
if (!challengeClaim || !challengeClaim.claimValue) {
throw new Error('Invalid payload, missing challenge claim')
}
if (challenges[issuer] !== challengeClaim.claimValue) {
throw new Error('Invalid challenge')
}
const expirationInSeconds = Math.floor(authExpirationInHours * 60 * 60)
const token = await createVerifiableCredentialJwt({
sub: issuer,
iss: identity.did,
nbf: Math.round((+Date.now()) / 1000),
exp: Math.round((+Date.now() / 1000) + expirationInSeconds),
vc: {
'@context': ['https://www.w3.org/2018/credentials/v1'],
type: ['VerifiableCredential'],
credentialSubject: {
claims: [
{ claimType: 'authToken' }
]
}
}
}, identity)
const hash = keccak256(token).toString('hex')
tokenRequestCounter[hash] = 0
setTimeout(() => delete tokenRequestCounter[hash], expirationInSeconds * 1000)
return token
}
const authExpressMiddleware = async (req, res, next) => {
if (!identity) {
throw new Error('Library not initialized')
}
// eslint-disable-next-line dot-notation
const token = req.headers['authorization']
if (token) {
try {
const { issuer } = await verifyCredential(token, didResolver)
if (issuer !== identity.did) {
res.status(401).send('Invalid VC issuer')
} else {
const hash = keccak256(token).toString('hex')
if (tokenRequestCounter[hash] >= 0) {
tokenRequestCounter[hash]++
if (tokenRequestCounter[hash] <= maxRequestsPerToken) {
next()
} else {
res.status(401).send('Max amount of requests reached')
}
} else {
res.status(401).send('Expired token')
}
}
} catch (err) {
if (err.message.toLowerCase().includes('jwt has expired')) {
res.status(401).send('Expired token')
} else if (err.message.toLowerCase().includes('incorrect format jwt')) {
res.status(401).send('Invalid token')
} else {
res.status(401).send(err.message)
}
}
} else {
res.status(401).send('No authorization header present')
}
}
module.exports = { getAuthToken, authExpressMiddleware, initializeAuth, getChallenge }