-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathnjsTokenCallback.c
309 lines (269 loc) · 11.9 KB
/
njsTokenCallback.c
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright (c) 2022, Oracle and/or its affiliates.
//-----------------------------------------------------------------------------
//
// This software is dual-licensed to you under the Universal Permissive License
// (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
// 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
// either license.
//
// If you elect to accept the software under the Apache License, Version 2.0,
// the following applies:
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// NAME
// njsTokenCallback.c
//
// DESCRIPTION
// Implementation of methods for token callback feature
// in token based authentication.
//
//-----------------------------------------------------------------------------
#include "njsModule.h"
// other methods
static void njsTokenCallback_onStopNotifications(uv_handle_t *handle);
static bool njsTokenCallback_onStopNotificationsHelper(napi_env env,
njsTokenCallback *callback);
static void njsTokenCallback_processNotification(uv_async_t *handle);
static bool njsTokenCallback_processNotificationHelper(
njsTokenCallback *callback);
static void njsTokenCallback_waitOnBarrier(njsTokenCallback *callback);
//-----------------------------------------------------------------------------
// njsTokenCallback_eventHandler()
// This method is called by the ODPI-C as callback every time an event is
// received and is called from outside the event loop in a thread that
// Javascript does not know anything about. Since multiple calls to
// uv_async_send() result in only one call to the posted callback, at least
// until after the callback has completed, a barrier is used.
//-----------------------------------------------------------------------------
int njsTokenCallback_eventHandler(njsTokenCallback *callback,
dpiAccessToken *tokenRefresh)
{
uv_mutex_lock(&callback->mutex);
uv_barrier_init(&callback->barrier, 2);
callback->result = false;
uv_async_send(&callback->async);
njsTokenCallback_waitOnBarrier(callback);
if (!callback->result)
return DPI_FAILURE;
tokenRefresh->token = callback->accessToken->token;
tokenRefresh->tokenLength = callback->accessToken->tokenLength;
tokenRefresh->privateKey = callback->accessToken->privateKey;
tokenRefresh->privateKeyLength = callback->accessToken->privateKeyLength;
uv_mutex_unlock(&callback->mutex);
return DPI_SUCCESS;
}
//-----------------------------------------------------------------------------
// njsTokenCallback_new()
// This method allocates memory to structure accessTokenCallback.
//-----------------------------------------------------------------------------
bool njsTokenCallback_new(njsBaton *baton, napi_env env,
napi_value userCallback, napi_value accessTokenConfig)
{
njsTokenCallback *callback;
napi_value jsPool;
callback = calloc(1, sizeof(njsTokenCallback));
if (!callback)
return njsBaton_setErrorInsufficientMemory(baton);
baton->accessTokenCallback = callback;
callback->accessToken = calloc(1, sizeof(dpiAccessToken));
if (!callback->accessToken)
return njsBaton_setErrorInsufficientMemory(baton);
callback->env = env;
callback->globals = baton->globals;
NJS_CHECK_NAPI(env, napi_create_reference(env, userCallback, 1,
&callback->jsCallback))
if (accessTokenConfig)
NJS_CHECK_NAPI(env, napi_create_reference(env, accessTokenConfig, 1,
&callback->jsAccessTokenConfig))
NJS_CHECK_NAPI(env, napi_get_reference_value(env, baton->jsCallingObjRef,
&jsPool))
NJS_CHECK_NAPI(env, napi_create_reference(env, jsPool, 1,
&callback->jsPool))
return true;
}
//-----------------------------------------------------------------------------
// njsTokenCallback_startNotifications()
// Start getting return value from callback. An async handle is
// created to ensure the event loop doesn't terminate until the callback is
// deregistered at time of pool close.
//-----------------------------------------------------------------------------
bool njsTokenCallback_startNotifications(njsTokenCallback *callback,
napi_env env)
{
uv_loop_t *loop;
// initialize UV handling
NJS_CHECK_NAPI(env, napi_get_uv_event_loop(env, &loop))
uv_mutex_init(&callback->mutex);
uv_async_init(loop, &callback->async,
njsTokenCallback_processNotification);
callback->async.data = callback;
return true;
}
//-----------------------------------------------------------------------------
// njsTokenCallback_processNotification()
// This method is called inside the event loop in the JavaScript main thread.
// It works together with the event handler method and lets that function know
// when its work is complete by "waiting" for the barrier.
//-----------------------------------------------------------------------------
static void njsTokenCallback_processNotification(uv_async_t *handle)
{
njsTokenCallback *callback = (njsTokenCallback*) handle->data;
napi_handle_scope scope;
bool result;
if (napi_open_handle_scope(callback->env, &scope) != napi_ok)
return;
result = njsTokenCallback_processNotificationHelper(callback);
if (!result)
njsTokenCallback_waitOnBarrier(callback);
napi_close_handle_scope(callback->env, scope);
}
//-----------------------------------------------------------------------------
// njsTokenCallback_processNotificationHelper()
// Helper method for processing notifications so that the scope that is
// opened can be easily destroyed.
//-----------------------------------------------------------------------------
static bool njsTokenCallback_processNotificationHelper(
njsTokenCallback *callback)
{
napi_value jsAccessTokenConfig = NULL, jsCallback,
jsCallbackHandler, jsPool, refresh, result;
napi_value jsCallbackArgs[4], externalObj;
napi_env env = callback->env;
NJS_CHECK_NAPI(env, napi_get_reference_value(env, callback->jsPool,
&jsPool))
NJS_CHECK_NAPI(env, napi_get_reference_value(env, callback->jsCallback,
&jsCallback))
if (callback->jsAccessTokenConfig)
NJS_CHECK_NAPI(env, napi_get_reference_value(env, callback->jsAccessTokenConfig,
&jsAccessTokenConfig))
NJS_CHECK_NAPI(env, napi_get_named_property(env, jsPool,
"_accessTokenHandler", &jsCallbackHandler))
NJS_CHECK_NAPI(env, napi_create_external(env, callback, NULL, NULL,
&externalObj))
NJS_CHECK_NAPI(env, napi_get_boolean(env, true, &refresh))
jsCallbackArgs[0] = jsCallback;
jsCallbackArgs[1] = externalObj;
jsCallbackArgs[2] = refresh;
jsCallbackArgs[3] = jsAccessTokenConfig;
NJS_CHECK_NAPI(env, napi_make_callback(env, NULL, jsPool,
jsCallbackHandler, 4, jsCallbackArgs, &result));
return true;
}
//-----------------------------------------------------------------------------
// njsTokenCallback_returnAccessTokenHelper()
// Helper function for njsTokenCallback_returnAccessToken().
//-----------------------------------------------------------------------------
static bool njsTokenCallback_returnAccessTokenHelper(njsTokenCallback *callback,
napi_env env, napi_value payloadObj)
{
dpiAccessToken *accessToken = callback->accessToken;
napi_valuetype actualType;
size_t tempLength;
napi_value temp;
NJS_CHECK_NAPI(env, napi_typeof(env, payloadObj, &actualType))
if (actualType == napi_undefined) {
accessToken->token = NULL;
accessToken->tokenLength = 0;
accessToken->privateKey = NULL;
accessToken->privateKeyLength = 0;
} else if (actualType == napi_string) {
if (!njsUtils_copyStringFromJS(env, payloadObj,
(char**) &accessToken->token, &tempLength))
return false;
accessToken->tokenLength = (uint32_t) tempLength;
} else if (actualType == napi_object) {
// Read "token" property
NJS_CHECK_NAPI(env, napi_get_named_property(env, payloadObj, "token",
&temp))
if (!njsUtils_copyStringFromJS(env, temp, (char**) &accessToken->token,
&tempLength))
return false;
accessToken->tokenLength = (uint32_t) tempLength;
// Read "privateKey" property
NJS_CHECK_NAPI(env, napi_get_named_property(env, payloadObj,
"privateKey", &temp))
if (!njsUtils_copyStringFromJS(env, temp,
(char**) &accessToken->privateKey, &tempLength))
return false;
accessToken->privateKeyLength = (uint32_t) tempLength;
}
return true;
}
//-----------------------------------------------------------------------------
// njsTokenCallback_returnAccessToken()
// Called by the JavaScript callback handler when it has completed. If an
// error has taken place, the value returned is the "undefined" Javascript
// value.
//-----------------------------------------------------------------------------
bool njsTokenCallback_returnAccessToken(njsTokenCallback *callback,
napi_env env, napi_value payloadObj)
{
callback->result = njsTokenCallback_returnAccessTokenHelper(callback, env,
payloadObj);
njsTokenCallback_waitOnBarrier(callback);
return true;
}
//-----------------------------------------------------------------------------
// njsTokenCallback_stopNotifications()
// Stop sending/receiving notifications to/from the supplied callback.
//-----------------------------------------------------------------------------
bool njsTokenCallback_stopNotifications(njsTokenCallback *callback)
{
if (!uv_is_closing((uv_handle_t*)&callback->async)) {
uv_close((uv_handle_t*) &callback->async,
njsTokenCallback_onStopNotifications);
}
return true;
}
//-----------------------------------------------------------------------------
// njsTokenCallback_waitOnBarrier()
// Wait on barrier
//-----------------------------------------------------------------------------
static void njsTokenCallback_waitOnBarrier(njsTokenCallback *callback)
{
if (uv_barrier_wait(&callback->barrier) > 0) {
uv_barrier_destroy(&callback->barrier);
}
}
//-----------------------------------------------------------------------------
// njsTokenCallback_onStopNotifications()
//-----------------------------------------------------------------------------
static void njsTokenCallback_onStopNotifications(uv_handle_t *handle)
{
njsTokenCallback *callback = (njsTokenCallback*) handle->data;
napi_handle_scope scope;
if (napi_open_handle_scope(callback->env, &scope) != napi_ok)
return;
njsTokenCallback_onStopNotificationsHelper(callback->env, callback);
napi_close_handle_scope(callback->env, scope);
}
//-----------------------------------------------------------------------------
// njsTokenCallback_onStopNotificationsHelper()
// Helper method for stopping notifications so that the scope that is opened
// can be easily destroyed.
//-----------------------------------------------------------------------------
static bool njsTokenCallback_onStopNotificationsHelper(napi_env env,
njsTokenCallback *callback)
{
// perform cleanup
uv_mutex_destroy(&callback->mutex);
NJS_DELETE_REF_AND_CLEAR(callback->jsPool);
NJS_DELETE_REF_AND_CLEAR(callback->jsCallback);
if (callback->accessToken) {
NJS_FREE_AND_CLEAR(callback->accessToken->token)
NJS_FREE_AND_CLEAR(callback->accessToken->privateKey)
NJS_FREE_AND_CLEAR(callback->accessToken)
}
return true;
}