Skip to content

Commit 23d7a59

Browse files
committed
add JWT support
1 parent 13d2468 commit 23d7a59

File tree

4 files changed

+34
-28
lines changed

4 files changed

+34
-28
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
## Release notes for the ArangoDB-PHP driver 3.8.x
44

5+
The driver now supports connecting via JWT if the server's JWT secret is known.
6+
In order to use a JWT to connect, set the following value in ConnectionOptions:
7+
```
8+
$connectionOptions = [
9+
ArangoDBClient\ConnectionOptions::OPTION_DATABASE => '_system', // database name
10+
ArangoDBClient\ConnectionOptions::OPTION_ENDPOINT => 'tcp://127.0.0.1:8529', // endpoint to connect to
11+
ArangoDBClient\ConnectionOptions::OPTION_AUTH_TYPE => 'Bearer', // authentication via JWT!
12+
ArangoDBClient\ConnectionOptions::OPTION_AUTH_USER => 'root', // user name
13+
ArangoDBClient\ConnectionOptions::OPTION_AUTH_PASSWD => 'jwt-secret-value', // server's JWT secret value,
14+
];
15+
```
16+
517
The driver now supports the following options for document CRUD operations:
618
- "overwriteMode"
719
- "silent"

lib/ArangoDBClient/Connection.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -425,12 +425,28 @@ private function updateHttpHeader()
425425
}
426426

427427
if (isset($this->_options[ConnectionOptions::OPTION_AUTH_TYPE], $this->_options[ConnectionOptions::OPTION_AUTH_USER])) {
428-
// add authorization header
429-
$authorizationValue = base64_encode(
430-
$this->_options[ConnectionOptions::OPTION_AUTH_USER] . ':' .
431-
$this->_options[ConnectionOptions::OPTION_AUTH_PASSWD]
432-
);
428+
if ($this->_options[ConnectionOptions::OPTION_AUTH_TYPE] == 'Bearer') {
429+
// JWT
430+
$base = json_encode(['typ' => 'JWT', 'alg' => 'HS256']);
431+
$base64UrlHeader = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($base));
432+
$payload = json_encode([
433+
'preferred_username' => $this->_options[ConnectionOptions::OPTION_AUTH_USER],
434+
'iss' => 'arangodb',
435+
'iat' => (int) microtime(true),
436+
]);
437+
$base64UrlPayload = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($payload));
438+
$signature = hash_hmac('sha256', $base64UrlHeader . "." . $base64UrlPayload, $this->_options[ConnectionOptions::OPTION_AUTH_PASSWD], true);
439+
$base64UrlSignature = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($signature));
440+
$authorizationValue = $base64UrlHeader . '.' . $base64UrlPayload . '.' . $base64UrlSignature;
441+
} else {
442+
// HTTP basic authentication
443+
$authorizationValue = base64_encode(
444+
$this->_options[ConnectionOptions::OPTION_AUTH_USER] . ':' .
445+
$this->_options[ConnectionOptions::OPTION_AUTH_PASSWD]
446+
);
447+
}
433448

449+
// add authorization header
434450
$this->_httpHeader .= sprintf(
435451
'Authorization: %s %s%s',
436452
$this->_options[ConnectionOptions::OPTION_AUTH_TYPE],

lib/ArangoDBClient/ConnectionOptions.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,21 +165,11 @@ class ConnectionOptions implements \ArrayAccess
165165
*/
166166
const OPTION_BATCHSIZE = 'batchSize';
167167

168-
/**
169-
* Wait for sync index constant
170-
*/
171-
const OPTION_JOURNAL_SIZE = 'journalSize';
172-
173168
/**
174169
* Wait for sync index constant
175170
*/
176171
const OPTION_IS_SYSTEM = 'isSystem';
177172

178-
/**
179-
* Wait for sync index constant
180-
*/
181-
const OPTION_IS_VOLATILE = 'isVolatile';
182-
183173
/**
184174
* Authentication user name
185175
*/
@@ -456,9 +446,7 @@ private static function getDefaults()
456446
self::OPTION_REVISION => null,
457447
self::OPTION_WAIT_SYNC => DefaultValues::DEFAULT_WAIT_SYNC,
458448
self::OPTION_BATCHSIZE => null,
459-
self::OPTION_JOURNAL_SIZE => DefaultValues::DEFAULT_JOURNAL_SIZE,
460449
self::OPTION_IS_SYSTEM => false,
461-
self::OPTION_IS_VOLATILE => DefaultValues::DEFAULT_IS_VOLATILE,
462450
self::OPTION_CONNECTION => DefaultValues::DEFAULT_CONNECTION,
463451
self::OPTION_TRACE => null,
464452
self::OPTION_ENHANCED_TRACE => false,
@@ -486,7 +474,7 @@ private static function getDefaults()
486474
*/
487475
private static function getSupportedAuthTypes()
488476
{
489-
return ['Basic'];
477+
return ['Basic', 'Bearer'];
490478
}
491479

492480
/**

lib/ArangoDBClient/DefaultValues.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,6 @@ abstract class DefaultValues
5151
*/
5252
const DEFAULT_WAIT_SYNC = false;
5353

54-
/**
55-
* Default value for collection journal size
56-
*/
57-
const DEFAULT_JOURNAL_SIZE = 33554432;
58-
59-
/**
60-
* Default value for isVolatile
61-
*/
62-
const DEFAULT_IS_VOLATILE = false;
63-
6454
/**
6555
* Default value for createCollection (create the collection on the fly when the first document is added to an unknown collection)
6656
*/

0 commit comments

Comments
 (0)