-
Notifications
You must be signed in to change notification settings - Fork 1
/
jwtUtils.js
41 lines (34 loc) · 1.15 KB
/
jwtUtils.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
var config = require('./config');
var jwt = require('jsonwebtoken');
var serviceAccount = require("./creds");
const crypto = require('crypto');
class JwtUtils {
constructor(){
this.audience = config.AUDIENCE;
this.type = config.JWT_TYPE;
this.iss = config.SERVICE_ACCOUNT_EMAIL_ADDRESS;
this.origins = config.ORIGINS;
this.iat = Math.floor(Date.now() / 1000) - 30 ;
this.payload = {};
}
addOfferObject(resourcePayload){
this.payload['offerObjects'] = [resourcePayload];
console.log(this.payload);
}
async generateUnsignedJwt(){
let unsignedJwt = {};
unsignedJwt['iss'] = this.iss;
unsignedJwt['aud'] = this.audience;
unsignedJwt['typ'] = this.type;
unsignedJwt['iat'] = this.iat;
unsignedJwt['payload'] = this.payload;
unsignedJwt['origins'] = this.origins;
return unsignedJwt
}
async generateSignedJwt(){
let jwtToSign = await this.generateUnsignedJwt();
let signedJwt = jwt.sign(jwtToSign, serviceAccount.private_key, { algorithm: 'RS256' });
return signedJwt;
}
}
module.exports = JwtUtils;