Skip to content

Commit 006ef8b

Browse files
miquelgallbtoron
andauthored
Add bearer token (#24)
* Add Bearer Token Management * 1.9.0 --------- Co-authored-by: Borja Toron-Antons <[email protected]>
1 parent a03d547 commit 006ef8b

File tree

7 files changed

+66
-24
lines changed

7 files changed

+66
-24
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
],
66
"name": "@ofs-users/proxy",
77
"type": "module",
8-
"version": "1.8.3",
8+
"version": "1.9.0",
99
"description": "A Javascript proxy to access Oracle Field Service via REST API",
1010
"main": "dist/ofs.es.js",
1111
"module": "dist/ofs.es.js",

src/OFS.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ export class OFS {
2828
public set credentials(v: OFSCredentials) {
2929
this._credentials = v;
3030
this._hash = OFS.authenticateUser(v);
31-
this._baseURL = new URL(
32-
`https://${this.instance}.${OFS.DEFAULT_DOMAIN}`
33-
);
31+
if ("baseURL" in v && v.baseURL != "") {
32+
this._baseURL = new URL(v.baseURL!);
33+
} else {
34+
this._baseURL = new URL(
35+
`https://${this.instance}.${OFS.DEFAULT_DOMAIN}`
36+
);
37+
}
3438
}
3539

3640
public get authorization(): string {
@@ -42,18 +46,24 @@ export class OFS {
4246
}
4347

4448
public get instance(): string {
45-
return this.credentials.instance;
49+
return this.credentials.instance || "";
50+
}
51+
public get baseURL(): string {
52+
return this.credentials.baseURL || "";
4653
}
47-
4854
private static authenticateUser(credentials: OFSCredentials): string {
49-
var token =
50-
credentials.clientId +
51-
"@" +
52-
credentials.instance +
53-
":" +
54-
credentials.clientSecret;
55-
var hash = Buffer.from(token).toString("base64");
56-
return "Basic " + hash;
55+
if ("token" in credentials && credentials.token != "") {
56+
return "Bearer " + credentials.token;
57+
} else {
58+
var token =
59+
credentials.clientId +
60+
"@" +
61+
credentials.instance +
62+
":" +
63+
credentials.clientSecret;
64+
var hash = Buffer.from(token).toString("base64");
65+
return "Basic " + hash;
66+
}
5767
}
5868

5969
private _get(

src/model.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import { off } from "process";
77

88
export type OFSCredentials = {
9-
instance: string;
10-
clientId: string;
11-
clientSecret: string;
9+
instance?: string;
10+
clientId?: string;
11+
clientSecret?: string;
12+
token?: string;
13+
baseURL?: string;
1214
};
1315

1416
export interface OFSResponseInterface {

test/general/base.test.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@
66
import { createReadStream, readFileSync } from "fs";
77
import { OFSCredentials } from "../../src/model";
88
import { OFS } from "../../src/OFS";
9-
import myCredentials from "../credentials_test.json";
9+
import myCredentials from "../credentials_test_app.json";
10+
import myOldCredentials from "../credentials_test.json";
1011
import { th } from "@faker-js/faker";
1112

1213
var myProxy: OFS;
1314

1415
// Setup info
1516
beforeAll(() => {
1617
myProxy = new OFS(myCredentials);
17-
expect(myProxy.instance).toBe(myCredentials.instance);
18+
if ("instance" in myCredentials) {
19+
expect(myProxy.instance).toBe(myCredentials.instance);
20+
} else {
21+
expect(myProxy.baseURL).toBe(myProxy.baseURL);
22+
}
1823
});
1924

2025
// Teardown info
@@ -28,7 +33,24 @@ afterAll(() => {
2833
test("Get Subscriptions", async () => {
2934
//const myProxy = new OFS(myCredentials);
3035
var result = await myProxy.getSubscriptions();
31-
expect(myProxy.instance).toBe(myCredentials.instance);
36+
try {
37+
expect(result.status).toBe(200);
38+
} catch (error) {
39+
console.error(result);
40+
throw error;
41+
}
42+
});
43+
44+
test("Get Subscriptions with old credentials style", async () => {
45+
//const myProxy = new OFS(myCredentials);
46+
var myProxyOld = new OFS(myOldCredentials);
47+
var result = await myProxyOld.getSubscriptions();
48+
try {
49+
expect(result.status).toBe(200);
50+
} catch (error) {
51+
console.error(result);
52+
throw error;
53+
}
3254
});
3355

3456
test("Update Plugin (path)", async () => {

test/general/core.activities.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ var myProxy: OFS;
1414
// Setup info
1515
beforeAll(() => {
1616
myProxy = new OFS(myCredentials);
17-
expect(myProxy.instance).toBe(myCredentials.instance);
17+
if ("instance" in myCredentials) {
18+
expect(myProxy.instance).toBe(myCredentials.instance);
19+
} else {
20+
expect(myProxy.baseURL).toBe(myProxy.baseURL);
21+
}
1822
});
1923

2024
// Teardown info

test/general/meta.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ TEST_CONFIG.set("23.11", {
2828
// Setup info
2929
beforeAll(() => {
3030
myProxy = new OFS(myCredentials);
31-
expect(myProxy.instance).toBe(myCredentials.instance);
31+
if ("instance" in myCredentials) {
32+
expect(myProxy.instance).toBe(myCredentials.instance);
33+
} else {
34+
expect(myProxy.baseURL).toBe(myProxy.baseURL);
35+
}
3236
var default_version: string = test_info.ofsVersion;
3337
testConfig = TEST_CONFIG.get(default_version); // TODO: Store in jest config
3438
});

0 commit comments

Comments
 (0)