-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
76 lines (69 loc) · 1.82 KB
/
index.js
File metadata and controls
76 lines (69 loc) · 1.82 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
/**
* Zoominfo API Authentication Client
*
* Get JWT access token using one of the following ways
* - PKI Auth flow using username, client id and private key
* - Username and Password
*/
const axios = require('axios');
const rs = require('jsrsasign');
const AUTH_URL = 'https://api.zoominfo.com/authenticate';
module.exports = {
getAccessTokenViaPKI: getAccessTokenViaPKI,
getAccessTokenViaBasicAuth: getAccessTokenViaBasicAuth
}
function getAccessTokenViaBasicAuth(username, password) {
return axios.post(AUTH_URL,
{
username: username,
password: password
})
.then(res => {
return res.data.jwt;
})
.catch(err => {
return err
});
}
function getAccessTokenViaPKI(username, clientId, privateKey) {
const dtNow = Date.now();
let alg = "RS256";
let iss = "zoominfo-api-auth-client-nodejs";
let aud = "enterprise_api";
let header = {
"typ": "JWT",
"alg": alg
};
let data = {
"aud": aud,
"iss": iss,
"username": username,
"client_id": clientId,
"iat": getIAT(dtNow),
"exp": getEXP(dtNow)
};
let sHeader = JSON.stringify(header);
let sPayload = JSON.stringify(data);
let clientJWT = rs.jws.JWS.sign(header.alg, sHeader, sPayload, privateKey);
return axios.post(AUTH_URL,
{},
{
headers: {
'Authorization': 'Bearer ' + clientJWT
}
}).then(res => {
return res.data.jwt;
}).catch(err => {
return err
});
}
function getIAT(dtNow) {
let iat = Math.floor(dtNow / 1000);
iat = iat - 60;
return iat;
}
function getEXP(dtNow) {
let exp = Math.floor(dtNow / 1000) + (5 * 60);
exp = exp - 60;
return exp;
}