Skip to content

Commit 885371a

Browse files
committed
Restore support for credentials in URLs
We're still supporting this in Foxx CLI and handling this in arangojs avoids having to teach Foxx CLI how to parse unix socket URLs. Additionally this makes the behavior more consistent.
1 parent 54a7bdd commit 885371a

File tree

5 files changed

+30
-21
lines changed

5 files changed

+30
-21
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- Restored support for credentials in URLs
13+
14+
If the server URL includes credentials, arangojs will now use them instead of
15+
the default username "root" and an empty password. Any credentials explicitly
16+
set using `useBasicAuth` or `useBearerAuth` will still override the default
17+
credentials as before.
18+
1019
## [6.8.0] - 2018-11-07
1120

1221
### Changed

docs/Drivers/JS/Reference/Database/README.md

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,20 @@ If _config_ is a string, it will be interpreted as _config.url_.
2626
When running ArangoDB on a unix socket, e.g. `/tmp/arangodb.sock` the
2727
following URL formats are supported for unix sockets:
2828

29-
* `unix:///tmp/arangodb.sock` (no SSL)
30-
* `http+unix:///tmp/arangodb.sock` (or `https+unix://` for SSL)
31-
* `http://unix:/tmp/arangodb.sock` (or `https://unix:` for SSL)
29+
- `unix:///tmp/arangodb.sock` (no SSL)
30+
- `http+unix:///tmp/arangodb.sock` (or `https+unix://` for SSL)
31+
- `http://unix:/tmp/arangodb.sock` (or `https://unix:` for SSL)
3232

3333
Additionally `ssl` and `tls` are treated as synonymous with `https` and
3434
`tcp` is treated as synonymous with `http`, so the following URLs are
3535
considered identical:
3636

37-
* `tcp://localhost:8529` and `http://localhost:8529`
38-
* `ssl://localhost:8529` and `https://localhost:8529`
39-
* `tcp+unix:///tmp/arangodb.sock` and `http+unix:///tmp/arangodb.sock`
40-
* `ssl+unix:///tmp/arangodb.sock` and `https+unix:///tmp/arangodb.sock`
41-
* `tcp://unix:/tmp/arangodb.sock` and `http://unix:/tmp/arangodb.sock`
42-
* `ssl://unix:/tmp/arangodb.sock` and `https://unix:/tmp/arangodb.sock`
43-
44-
{% hint 'warning' %}
45-
As of arangojs 6.0.0 it is no longer possible to pass
46-
the username or password from the URL. Use
47-
[`database.useBasicAuth`](#databaseusebasicauth) instead.
48-
{% endhint %}
37+
- `tcp://localhost:8529` and `http://localhost:8529`
38+
- `ssl://localhost:8529` and `https://localhost:8529`
39+
- `tcp+unix:///tmp/arangodb.sock` and `http+unix:///tmp/arangodb.sock`
40+
- `ssl+unix:///tmp/arangodb.sock` and `https+unix:///tmp/arangodb.sock`
41+
- `tcp://unix:/tmp/arangodb.sock` and `http://unix:/tmp/arangodb.sock`
42+
- `ssl://unix:/tmp/arangodb.sock` and `https://unix:/tmp/arangodb.sock`
4943

5044
If you want to use ArangoDB with authentication, see
5145
_useBasicAuth_ or _useBearerAuth_ methods.

src/database.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ export class Database {
8686

8787
constructor(config?: Config) {
8888
this._connection = new Connection(config);
89-
this.useBasicAuth();
9089
}
9190

9291
get name(): string | null {

src/util/request.node.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from "http";
88
import { Agent as HttpsAgent, request as httpsRequest } from "https";
99
import { parse as parseUrl, Url } from "url";
10+
import { btoa } from "./btoa";
1011
import { joinPath } from "./joinPath";
1112
import { Errback } from "./types";
1213

@@ -87,14 +88,18 @@ export function createRequest(
8788
if (body && !headers["content-length"]) {
8889
headers["content-length"] = String(Buffer.byteLength(body));
8990
}
91+
if (!headers["authorization"]) {
92+
headers["authorization"] = `Basic ${btoa(
93+
baseUrlParts.auth || "root:"
94+
)}`;
95+
}
9096
const options: ClientRequestArgs = { path, method, headers, agent };
9197
if (socketPath) {
9298
options.socketPath = socketPath;
9399
} else {
94100
options.host = baseUrlParts.hostname;
95101
options.port = baseUrlParts.port;
96102
}
97-
options.auth = baseUrlParts.auth;
98103
let called = false;
99104
try {
100105
const req = (isTls ? httpsRequest : httpRequest)(

src/util/request.web.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
import { format as formatUrl, parse as parseUrl } from "url";
2+
import { joinPath } from "./joinPath";
13
import {
24
ArangojsError,
35
ArangojsResponse,
46
RequestOptions
57
} from "./request.node";
6-
import { format as formatUrl, parse as parseUrl } from "url";
7-
88
import { Errback } from "./types";
9-
import { joinPath } from "./joinPath";
109
import xhr from "./xhr";
1110

1211
export const isBrowser = true;
@@ -21,7 +20,7 @@ function omit<T>(obj: T, keys: (keyof T)[]): T {
2120
}
2221

2322
export function createRequest(baseUrl: string, agentOptions: any) {
24-
const baseUrlParts = parseUrl(baseUrl);
23+
const { auth, ...baseUrlParts } = parseUrl(baseUrl);
2524
const options = omit(agentOptions, [
2625
"keepAlive",
2726
"keepAliveMsecs",
@@ -44,6 +43,9 @@ export function createRequest(baseUrl: string, agentOptions: any) {
4443
: url.search
4544
: baseUrlParts.search
4645
};
46+
if (!headers["authorization"]) {
47+
headers["authorization"] = `Basic ${btoa(auth || "root:")}`;
48+
}
4749

4850
let callback: Errback<ArangojsResponse> = (err, res) => {
4951
callback = () => undefined;

0 commit comments

Comments
 (0)