forked from 8log/keycloak-request-token
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
144 lines (114 loc) · 4.06 KB
/
index.js
File metadata and controls
144 lines (114 loc) · 4.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
/**
* @module keycloak-request-token
*/
'use strict';
const http = require('http');
const https = require('https');
const url = require('url');
const querystring = require('querystring');
const tokenUrl = 'protocol/openid-connect/token';
const store = new Map();
function ServiceError (status, body, message) {
this.name = 'ServiceError';
this.body = body;
this.status = status;
this.message = message || 'Invalid request';
this.stack = (new Error()).stack;
}
ServiceError.prototype = Object.create(Error.prototype);
ServiceError.prototype.constructor = ServiceError;
function getRequestOpts (uri, data) {
return Object.assign(url.parse(uri), {
method: 'POST',
data: data,
headers: {'Content-type': 'application/x-www-form-urlencoded'}
});
}
function request (options) {
const caller = (options.protocol === 'https:') ? https : http;
const data = [];
return new Promise(function (resolve, reject) {
const message = 'Failed to get token';
const request = caller.request(options, res => {
res
.on('data', chunk => data.push(chunk))
.on('end', () => {
const {statusCode} = res;
try {
const stringData = Buffer.concat(data).toString();
if (statusCode === 404) {
return reject(new ServiceError(statusCode, stringData, message));
}
const parsedData = JSON.parse(stringData);
if (statusCode !== 200) {
return reject(new ServiceError(statusCode, parsedData, message));
}
resolve(parsedData);
} catch (err) {
reject(err);
}
});
});
request.on('error', e => reject(e));
request.write(querystring.stringify(options.data));
request.end();
});
}
function getToken (baseUrl, settings) {
const opts = getRequestOpts(`${baseUrl}/realms/${settings.realmName}/${tokenUrl}`, settings);
return request(opts);
}
function refreshToken (baseUrl, settings, refreshToken) {
const data = Object.assign({}, settings, {
grant_type: 'refresh_token',
refresh_token: refreshToken
});
const opts = getRequestOpts(`${baseUrl}/realms/${settings.realmName}/${tokenUrl}`, data);
return request(opts);
}
/**
Requests a new Keycloak Access Token
@param {string} baseUrl - The baseurl for the Keycloak server - ex: http://localhost:8080/auth,
@param {object} settings - an object containing the settings
@param {string} settings.username - The username to login to the keycloak server - ex: admin
@param {string} settings.password - The password to login to the keycloak server - ex: *****
@param {string} settings.grant_type - the type of authentication mechanism - ex: password,
@param {string} settings.client_id - the id of the client that is registered with Keycloak to connect to - ex: admin-cli
@param {string} settings.realmName - the name of the realm to login to - defaults to 'masterg'
@returns {Promise} A promise that will resolve with the Access Token String.
@instance
@example
const tokenRequester = require('keycloak-request-token')
const baseUrl = 'http://127.0.0.1:8080/auth'
const settings = {
username: 'admin',
password: 'admi',
grant_type: 'password',
client_id: 'admin-cli'
}
tokenRequester(baseUrl, settings)
.then((token) => {
console.log(token)
}).catch((err) => {
console.log('err', err)
})
*/
async function tokenRequester (baseUrl, settings = {}) {
settings.realmName = settings.realmName ? settings.realmName : 'master';
const storeKey = JSON.stringify(Object.assign({}, settings, {baseUrl}));
const now = Date.now();
let token = store.get(storeKey);
if (token && token.exp > now) {
return token.access_token;
}
if (token && token.refresh_exp > now) {
token = await refreshToken(baseUrl, settings, token.refresh_token);
} else {
token = await getToken(baseUrl, settings);
}
token.exp = Date.now() + token.expires_in;
token.refresh_exp = Date.now() + token.refresh_expires_in;
store.set(storeKey, token);
return token.access_token;
}
module.exports = tokenRequester;