-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoidcApi.js
230 lines (199 loc) · 6.5 KB
/
oidcApi.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* eslint-disable func-names */
/* eslint-disable no-console */
/* eslint-disable no-use-before-define */
/**
* Class to handle json api calls
* @type {*}
*/
const https = require('https')
const http = require('http')
const discovery = require('./discovery')
const tokenCall = require('./tokenCall')
module.exports = (function () {
/**
* Wrap our constructor in a factory method.
* @param options our options object
* @returns {Api} a new instance of an Api class
*/
function factory() {
return new Api()
}
/**
* Constructor for the Api class
* @param options the options object:
* {
* port : int / default: 443
* method : string / default: GET
* host : string / default: none / MANDATORY
* path : string / default: ''
* debugMode : boolean / default: false
* https : boolean / default: true
* headers : object / default: undefined
* }
* @constructor
*/
function Api() {}
// Init the module instance
Api.prototype.init = function (options, onSuccess, onError) {
const self = this
this.options = options || {}
this.options.debugMode = options.debugMode || false
this.options.oidcHost = options.oidcHost
this.options.oidcPath = options.oidcPath
this.options.clientKey = options.clientKey
this.options.clientSecret = options.clientSecret
this.options.loginUrl = options.loginUrl
if (this.handleAuthentication) {
const discoveryService = discovery({
host: this.options.oidcHost,
path: this.options.oidcPath,
port: this.options.oidcPort,
})
discoveryService.discovery(
discoveryData => {
console.log('Discovery done')
self.options.discoveryData = discoveryData
onSuccess()
},
err => {
onError(err)
}
)
}
}
/**
* Sends a request to the configured api
*
* @param onSuccess callback for success, recieves the response json data as a parameter
* @param onError callback for then call fails, recieves the error as a parameter
* @param originalRequest
* @param originalResponse
*/
Api.prototype.request = function (onSuccess, onError, originalRequest, originalResponse) {
const self = this
if (originalRequest.user) {
self.requestOptions.headers.Authentication = 'Bearer ' + originalRequest.user
}
const requestOptions = {
host: self.requestOptions.host,
port: self.requestOptions.port,
path: self.requestOptions.path,
method: self.requestOptions.method,
headers: self.requestOptions.headers,
}
self.debugPrint('Request settings: ' + JSON.stringify(requestOptions))
const protocol = self.options.https ? https : http
const apiRequest = protocol.request(
requestOptions,
_onResponse(onSuccess, onError, originalRequest, originalResponse, self)
)
apiRequest.end()
apiRequest.on('error', err => {
if (onError != null) {
onError(err)
}
})
}
// Configure the next request
Api.prototype.configureRequest = function (options) {
this.requestOptions = options || {}
this.requestOptions.port = options.port || 443
this.requestOptions.method = options.method || 'GET'
this.requestOptions.path = options.path || ''
this.requestOptions.https = options.https || true
this.debugPrint = function (msg) {
if (this.requestOptions.debugMode) {
console.log(msg)
}
}
}
/**
* Wrapper function for handling response data.
* The wrapping is done to allow the callback to http|https.request to
* take more parameters than the original single 'response'.
*
* @param onSuccess success callback
* @param onError error callback
* @param self the prototype object
* @returns {Function} a function pointer that can be used as a http|https.request callback
* @private
*/
function _onResponse(onSuccess, onError, originalRequest, originalResponse, self) {
const err = onError || function () {}
const succ = onSuccess || function () {}
return function (response) {
let responseData = ''
response.on('data', data => {
responseData += data
})
response.on('end', () => {
self.debugPrint('Response from api: ' + responseData)
_handleResponseFromApi(response, responseData, succ, err, originalRequest, originalResponse, self)
/* var json
try {
json = JSON.parse(responseData)
} catch (err) {
onError(err)
}
onSuccess(json); */
})
}
}
function _handleResponseFromApi(response, responseData, onSuccess, onError, originalRequest, originalResponse, self) {
switch (response.statusCode) {
case 401:
// Unauthorized - token failed and no higher elevation is possible
console.log('Unauthorized')
onError({ error: '401 - Unauthorized. Token not valid for scope.' })
break
case 403:
// Forbidden - token failed, try elevating authentication level
console.log('Forbidden')
originalResponse.redirect('/redirected/to/login/from/apicall?originalUrl=' + originalRequest.url)
break
case 412: {
// Precondition failed - token was missing but should exist for resource
console.log('Precondition failed')
const TokenCall = tokenCall({
clientKey: self.options.clientKey,
clientSecret: self.options.clientSecret,
tokenEndpoint: self.discoveryData.token_endpoint,
})
TokenCall.getClientToken(
token => {
console.log('Got client token')
self.headers.Authentication = 'Bearer ' + token // eslint-disable-line no-param-reassign
self.request(onSuccess, onError)
},
err => {
onError({
error: '500 - Internal server error. Error while fetching client access token: ' + err,
})
}
)
break
}
case 200:
// Everything went fine
console.log('200 OK')
onSuccess(JSON.parse(responseData))
break
case 500:
// Internal server error
console.log('500 ISE')
onError({
error: '500 - Internal server error. Something went wrong. Response data: ' + responseData,
})
break
case 400:
// Bad request - wrong url?
console.log('400 BR')
onError({ error: '400 - Bad request. Wrong url?' })
break
default:
console.log('Default')
break
}
}
return factory
})()