Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for EdgeKV access tokens using reference #201

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 12 additions & 6 deletions edgekv/lib/edgekv.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
(c) Copyright 2020 Akamai Technologies, Inc. Licensed under Apache 2 license.
Version: 0.6.1
Version: 0.6.2
Purpose: Provide a helper class to simplify the interaction with EdgeKV in an EdgeWorker.
Repo: https://github.com/akamai/edgeworkers-examples/tree/master/edgekv/lib
*/
Expand Down Expand Up @@ -122,15 +122,21 @@ export class EdgeKV {
}
}

getNamespaceToken(namespace) {
getNamespaceTokenHeader(namespace) {
if (this.#token_override) {
return this.#token_override;
}
let name = "namespace-" + namespace;
if (!(name in edgekv_access_tokens)) {
throw "MISSING ACCESS TOKEN. No EdgeKV Access Token defined for namespace '" + namespace + "'.";
}
return edgekv_access_tokens[name]["value"];
if ("value" in edgekv_access_tokens[name]) {
return { 'X-Akamai-EdgeDB-Auth': [edgekv_access_tokens[name]["value"]]};
} else if ("reference" in edgekv_access_tokens[name]) {
return { 'X-Akamai-EdgeDB-Auth-Ref': [edgekv_access_tokens[name]["reference"]]};
} else {
throw "MISSING ACCESS TOKEN. No EdgeKV Access Token value or reference defined for namespace '" + namespace + "'.";
}
}

addTimeout(options, timeout) {
Expand Down Expand Up @@ -176,7 +182,7 @@ export class EdgeKV {
return httpRequest(this.addSandboxId(uri), this.addTimeout({
method: "PUT",
body: typeof value === "object" ? JSON.stringify(value) : value,
headers: { "X-Akamai-EdgeDB-Auth": [this.getNamespaceToken(namespace)] }
headers: { ...this.getNamespaceTokenHeader(namespace) }
}, timeout));
}

Expand Down Expand Up @@ -272,7 +278,7 @@ export class EdgeKV {
let uri = this.#edgekv_uri + "/api/v1/namespaces/" + namespace + "/groups/" + group + "/items/" + item;
return httpRequest(this.addSandboxId(uri), this.addTimeout({
method: "GET",
headers: { "X-Akamai-EdgeDB-Auth": [this.getNamespaceToken(namespace)] }
headers: { ...this.getNamespaceTokenHeader(namespace) }
}, timeout));
}

Expand Down Expand Up @@ -332,7 +338,7 @@ export class EdgeKV {
let uri = this.#edgekv_uri + "/api/v1/namespaces/" + namespace + "/groups/" + group + "/items/" + item;
return httpRequest(this.addSandboxId(uri), this.addTimeout({
method: "DELETE",
headers: { "X-Akamai-EdgeDB-Auth": [this.getNamespaceToken(namespace)] }
headers: { ...this.getNamespaceTokenHeader(namespace) }
}, timeout));
}

Expand Down