41
41
#include < velocypack/Builder.h>
42
42
#include < velocypack/Collection.h>
43
43
#include < velocypack/Iterator.h>
44
+ #include < velocypack/StringRef.h>
44
45
#include < velocypack/velocypack-aliases.h>
45
46
46
47
using namespace arangodb ;
47
48
using namespace arangodb ::basics;
48
49
using namespace arangodb ::velocypack;
49
50
using namespace arangodb ::rest;
51
+
52
+ namespace {
53
+ velocypack::StringRef const hs256String (" HS256" );
54
+ velocypack::StringRef const jwtString (" JWT" );
55
+ }
50
56
51
57
auth::TokenCache::TokenCache (auth::UserManager* um, double timeout)
52
58
: _userManager(um),
@@ -153,7 +159,7 @@ auth::TokenCache::Entry auth::TokenCache::checkAuthenticationBasic(std::string c
153
159
expiry += TRI_microtime ();
154
160
}
155
161
156
- auth::TokenCache::Entry entry (username, authorized, expiry);
162
+ auth::TokenCache::Entry entry (std::move ( username) , authorized, expiry);
157
163
{
158
164
WRITE_LOCKER (guard, _basicLock);
159
165
if (authorized) {
@@ -232,7 +238,7 @@ auth::TokenCache::Entry auth::TokenCache::checkAuthenticationJWT(std::string con
232
238
}
233
239
234
240
std::shared_ptr<VPackBuilder> auth::TokenCache::parseJson (std::string const & str,
235
- std::string const & hint) {
241
+ char const * hint) {
236
242
std::shared_ptr<VPackBuilder> result;
237
243
VPackParser parser;
238
244
try {
@@ -255,7 +261,7 @@ std::shared_ptr<VPackBuilder> auth::TokenCache::parseJson(std::string const& str
255
261
bool auth::TokenCache::validateJwtHeader (std::string const & header) {
256
262
std::shared_ptr<VPackBuilder> headerBuilder =
257
263
parseJson (StringUtils::decodeBase64U (header), " jwt header" );
258
- if (headerBuilder. get () == nullptr ) {
264
+ if (headerBuilder == nullptr ) {
259
265
return false ;
260
266
}
261
267
@@ -267,20 +273,15 @@ bool auth::TokenCache::validateJwtHeader(std::string const& header) {
267
273
VPackSlice const algSlice = headerSlice.get (" alg" );
268
274
VPackSlice const typSlice = headerSlice.get (" typ" );
269
275
270
- if (!algSlice.isString ()) {
271
- return false ;
272
- }
273
-
274
- if (!typSlice.isString ()) {
276
+ if (!algSlice.isString () || !typSlice.isString ()) {
275
277
return false ;
276
278
}
277
279
278
- if (algSlice.copyString () != " HS256 " ) {
280
+ if (! algSlice.isEqualString (::hs256String) ) {
279
281
return false ;
280
282
}
281
-
282
- std::string typ = typSlice.copyString ();
283
- if (typ != " JWT" ) {
283
+
284
+ if (!typSlice.isEqualString (::jwtString)) {
284
285
return false ;
285
286
}
286
287
@@ -290,7 +291,7 @@ bool auth::TokenCache::validateJwtHeader(std::string const& header) {
290
291
auth::TokenCache::Entry auth::TokenCache::validateJwtBody (std::string const & body) {
291
292
std::shared_ptr<VPackBuilder> bodyBuilder =
292
293
parseJson (StringUtils::decodeBase64U (body), " jwt body" );
293
- if (bodyBuilder. get () == nullptr ) {
294
+ if (bodyBuilder == nullptr ) {
294
295
LOG_TOPIC (" 99524" , TRACE, Logger::AUTHENTICATION) << " invalid JWT body" ;
295
296
return auth::TokenCache::Entry::Unauthenticated ();
296
297
}
@@ -307,14 +308,14 @@ auth::TokenCache::Entry auth::TokenCache::validateJwtBody(std::string const& bod
307
308
return auth::TokenCache::Entry::Unauthenticated ();
308
309
}
309
310
310
- if (issSlice.copyString () != " arangodb" ) {
311
+ if (! issSlice.isEqualString ( velocypack::StringRef ( " arangodb" )) ) {
311
312
LOG_TOPIC (" 2547e" , TRACE, arangodb::Logger::AUTHENTICATION) << " invalid iss value" ;
312
313
return auth::TokenCache::Entry::Unauthenticated ();
313
314
}
314
315
315
316
auth::TokenCache::Entry authResult (" " , false , 0 );
316
- if ( bodySlice.hasKey (" preferred_username" )) {
317
- VPackSlice const usernameSlice = bodySlice. get ( " preferred_username " );
317
+ VPackSlice const usernameSlice = bodySlice.get (" preferred_username" );
318
+ if (! usernameSlice. isNone ()) {
318
319
if (!usernameSlice.isString () || usernameSlice.getStringLength () == 0 ) {
319
320
return auth::TokenCache::Entry::Unauthenticated ();
320
321
}
@@ -330,8 +331,8 @@ auth::TokenCache::Entry auth::TokenCache::validateJwtBody(std::string const& bod
330
331
return auth::TokenCache::Entry::Unauthenticated ();
331
332
}
332
333
333
- if ( bodySlice.hasKey (" allowed_paths" )) {
334
- VPackSlice const paths = bodySlice. get ( " allowed_paths " );
334
+ VPackSlice const paths = bodySlice.get (" allowed_paths" );
335
+ if (! paths. isNone ()) {
335
336
if (!paths.isArray ()) {
336
337
LOG_TOPIC (" 89898" , TRACE, arangodb::Logger::AUTHENTICATION)
337
338
<< " allowed_paths must be an array" ;
@@ -353,8 +354,8 @@ auth::TokenCache::Entry auth::TokenCache::validateJwtBody(std::string const& bod
353
354
}
354
355
355
356
// mop: optional exp (cluster currently uses non expiring jwts)
356
- if ( bodySlice.hasKey (" exp" )) {
357
- VPackSlice const expSlice = bodySlice. get ( " exp " );
357
+ VPackSlice const expSlice = bodySlice.get (" exp" );
358
+ if (! expSlice. isNone ()) {
358
359
if (!expSlice.isNumber ()) {
359
360
LOG_TOPIC (" 74735" , TRACE, Logger::AUTHENTICATION) << " invalid exp value" ;
360
361
return authResult; // unauthenticated
@@ -417,22 +418,22 @@ std::string auth::TokenCache::generateJwt(VPackSlice const& payload) const {
417
418
bool hasIat = payload.hasKey (" iat" );
418
419
if (hasIss && hasIat) {
419
420
return generateRawJwt (payload);
420
- } else {
421
- VPackBuilder bodyBuilder;
422
- {
423
- VPackObjectBuilder p (&bodyBuilder);
424
- if (!hasIss) {
425
- bodyBuilder. add ( " iss " , VPackValue ( " arangodb " ));
426
- }
427
- if (!hasIat) {
428
- bodyBuilder. add ( " iat " , VPackValue ( TRI_microtime () / 1000 ));
429
- }
430
- for ( auto const & obj : VPackObjectIterator (payload)) {
431
- bodyBuilder. add (obj. key . copyString (), obj. value );
432
- }
421
+ }
422
+
423
+ VPackBuilder bodyBuilder;
424
+ {
425
+ VPackObjectBuilder p (&bodyBuilder);
426
+ if (!hasIss) {
427
+ bodyBuilder. add ( " iss " , VPackValue ( " arangodb " ));
428
+ }
429
+ if (!hasIat) {
430
+ bodyBuilder. add ( " iat " , VPackValue ( TRI_microtime () / 1000 ));
431
+ }
432
+ for ( auto const & obj : VPackObjectIterator (payload)) {
433
+ bodyBuilder. add (obj. key . copyString (), obj. value );
433
434
}
434
- return generateRawJwt (bodyBuilder.slice ());
435
435
}
436
+ return generateRawJwt (bodyBuilder.slice ());
436
437
}
437
438
438
439
// / generate a JWT token for internal cluster communication
0 commit comments