Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions examples/international_address_autocomplete.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import SmartySDK from "smartystreets-javascript-sdk";
const SmartyCore = SmartySDK.core;
const Lookup = SmartySDK.internationalAddressAutocomplete.Lookup;

// for Server-to-server requests, use this code:
// let authId = process.env.SMARTY_AUTH_ID;
// let authToken = process.env.SMARTY_AUTH_TOKEN;
// const credentials = new SmartyCore.StaticCredentials(authId, authToken);

// for client-side requests (browser/mobile), use this code:
const key = process.env.SMARTY_EMBEDDED_KEY;
const credentials = new SmartyCore.SharedCredentials(key);
// const key = process.env.SMARTY_EMBEDDED_KEY;
// const credentials = new SmartyCore.SharedCredentials(key);

// for Server-to-server requests, use this code:
const authId = process.env.SMARTY_AUTH_ID;
const authToken = process.env.SMARTY_AUTH_TOKEN;
const credentials = new SmartyCore.BasicAuthCredentials(authId, authToken);

// The appropriate license values to be used for your subscriptions
// can be found on the Subscription page of the account dashboard.
// https://www.smarty.com/docs/cloud/licensing
const clientBuilder = new SmartyCore.ClientBuilder(credentials).withLicenses(["international-autocomplete-v2-cloud"])
// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API
const clientBuilder = new SmartyCore.ClientBuilder(credentials);
// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API

const client = clientBuilder.buildInternationalAddressAutocompleteClient();

Expand Down
14 changes: 7 additions & 7 deletions examples/international_postal_code.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import SmartySDK from "smartystreets-javascript-sdk";
const SmartyCore = SmartySDK.core;
const Lookup = SmartySDK.internationalPostalCode.Lookup;

// for Server-to-server requests, use this code:
// let authId = process.env.SMARTY_AUTH_ID;
// let authToken = process.env.SMARTY_AUTH_TOKEN;
// const credentials = new SmartyCore.StaticCredentials(authId, authToken);

// for client-side requests (browser/mobile), use this code:
let key = process.env.SMARTY_EMBEDDED_KEY;
const credentials = new SmartyCore.SharedCredentials(key);
// let key = process.env.SMARTY_EMBEDDED_KEY;
// const credentials = new SmartyCore.SharedCredentials(key);

// for Server-to-server requests, use this code:
let authId = process.env.SMARTY_AUTH_ID;
let authToken = process.env.SMARTY_AUTH_TOKEN;
const credentials = new SmartyCore.BasicAuthCredentials(authId, authToken);

// The appropriate license values to be used for your subscriptions
// can be found on the Subscription page of the account dashboard.
Expand Down
18 changes: 9 additions & 9 deletions examples/international_street.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import SmartySDK from "smartystreets-javascript-sdk";
const SmartyCore = SmartySDK.core;
const Lookup = SmartySDK.internationalStreet.Lookup;

// for Server-to-server requests, use this code:
// let authId = process.env.SMARTY_AUTH_ID;
// let authToken = process.env.SMARTY_AUTH_TOKEN;
// const credentials = new SmartyCore.StaticCredentials(authId, authToken);

// for client-side requests (browser/mobile), use this code:
let key = process.env.SMARTY_EMBEDDED_KEY;
const credentials = new SmartyCore.SharedCredentials(key);
// let key = process.env.SMARTY_EMBEDDED_KEY;
// const credentials = new SmartyCore.SharedCredentials(key);

// for Server-to-server requests, use this code:
let authId = process.env.SMARTY_AUTH_ID;
let authToken = process.env.SMARTY_AUTH_TOKEN;
const credentials = new SmartyCore.BasicAuthCredentials(authId, authToken);

// The appropriate license values to be used for your subscriptions
// can be found on the Subscription page of the account dashboard.
// https://www.smarty.com/docs/cloud/licensing
let clientBuilder = new SmartyCore.ClientBuilder(credentials).withLicenses(["international-global-plus-cloud"]);
// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API
let clientBuilder = new SmartyCore.ClientBuilder(credentials);
// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API

let client = clientBuilder.buildInternationalStreetClient();

Expand Down
18 changes: 9 additions & 9 deletions examples/us_autocomplete_pro.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import SmartySDK from "smartystreets-javascript-sdk";
const SmartyCore = SmartySDK.core;
const Lookup = SmartySDK.usAutocompletePro.Lookup;

// for Server-to-server requests, use this code:
// let authId = process.env.SMARTY_AUTH_ID;
// let authToken = process.env.SMARTY_AUTH_TOKEN;
// const credentials = new SmartyCore.StaticCredentials(authId, authToken);

// for client-side requests (browser/mobile), use this code:
let key = process.env.SMARTY_EMBEDDED_KEY;
const credentials = new SmartyCore.SharedCredentials(key);
// let key = process.env.SMARTY_EMBEDDED_KEY;
// const credentials = new SmartyCore.SharedCredentials(key);

// for Server-to-server requests, use this code:
let authId = process.env.SMARTY_AUTH_ID;
let authToken = process.env.SMARTY_AUTH_TOKEN;
const credentials = new SmartyCore.BasicAuthCredentials(authId, authToken);

// The appropriate license values to be used for your subscriptions
// can be found on the Subscription page of the account dashboard.
// https://www.smarty.com/docs/cloud/licensing
let clientBuilder = new SmartyCore.ClientBuilder(credentials).withLicenses(["us-autocomplete-pro-cloud"]);
// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API
let clientBuilder = new SmartyCore.ClientBuilder(credentials);
// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API

let client = clientBuilder.buildUsAutocompleteProClient();

Expand Down
18 changes: 9 additions & 9 deletions examples/us_enrichment.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import SmartySDK from "smartystreets-javascript-sdk";
const SmartyCore = SmartySDK.core;
const Lookup = SmartySDK.usEnrichment.Lookup;

// for Server-to-server requests, use this code:
// let authId = process.env.SMARTY_AUTH_ID;
// let authToken = process.env.SMARTY_AUTH_TOKEN;
// const credentials = new SmartyCore.StaticCredentials(authId, authToken);

// for client-side requests (browser/mobile), use this code:
let key = process.env.SMARTY_EMBEDDED_KEY;
const credentials = new SmartyCore.SharedCredentials(key);
// let key = process.env.SMARTY_EMBEDDED_KEY;
// const credentials = new SmartyCore.SharedCredentials(key);

// for Server-to-server requests, use this code:
let authId = process.env.SMARTY_AUTH_ID;
let authToken = process.env.SMARTY_AUTH_TOKEN;
const credentials = new SmartyCore.BasicAuthCredentials(authId, authToken);

// The appropriate license values to be used for your subscriptions
// can be found on the Subscription page of the account dashboard.
// https://www.smarty.com/docs/cloud/licensing
let clientBuilder = new SmartyCore.ClientBuilder(credentials).withLicenses(["us-property-data-principal-cloud"]);
// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API
let clientBuilder = new SmartyCore.ClientBuilder(credentials);
// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API

let client = clientBuilder.buildUsEnrichmentClient();

Expand Down
14 changes: 7 additions & 7 deletions examples/us_extract.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import SmartySDK from "smartystreets-javascript-sdk";
const SmartyCore = SmartySDK.core;
const Lookup = SmartySDK.usExtract.Lookup;

// for Server-to-server requests, use this code:
// let authId = process.env.SMARTY_AUTH_ID;
// let authToken = process.env.SMARTY_AUTH_TOKEN;
// const credentials = new SmartyCore.StaticCredentials(authId, authToken);

// for client-side requests (browser/mobile), use this code:
let key = process.env.SMARTY_EMBEDDED_KEY;
const credentials = new SmartyCore.SharedCredentials(key);
// let key = process.env.SMARTY_EMBEDDED_KEY;
// const credentials = new SmartyCore.SharedCredentials(key);

// for Server-to-server requests, use this code:
let authId = process.env.SMARTY_AUTH_ID;
let authToken = process.env.SMARTY_AUTH_TOKEN;
const credentials = new SmartyCore.BasicAuthCredentials(authId, authToken);

let clientBuilder = new SmartyCore.ClientBuilder(credentials);
// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API
Expand Down
18 changes: 9 additions & 9 deletions examples/us_reverse_geo.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import SmartySDK from "smartystreets-javascript-sdk";
const SmartyCore = SmartySDK.core;
const Lookup = SmartySDK.usReverseGeo.Lookup;

// for Server-to-server requests, use this code:
// let authId = process.env.SMARTY_AUTH_ID;
// let authToken = process.env.SMARTY_AUTH_TOKEN;
// const credentials = new SmartyCore.StaticCredentials(authId, authToken);

// for client-side requests (browser/mobile), use this code:
let key = process.env.SMARTY_EMBEDDED_KEY;
const credentials = new SmartyCore.SharedCredentials(key);
// let key = process.env.SMARTY_EMBEDDED_KEY;
// const credentials = new SmartyCore.SharedCredentials(key);

// for Server-to-server requests, use this code:
let authId = process.env.SMARTY_AUTH_ID;
let authToken = process.env.SMARTY_AUTH_TOKEN;
const credentials = new SmartyCore.BasicAuthCredentials(authId, authToken);

// The appropriate license values to be used for your subscriptions
// can be found on the Subscription page of the account dashboard.
// https://www.smarty.com/docs/cloud/licensing
let clientBuilder = new SmartyCore.ClientBuilder(credentials).withLicenses(["us-reverse-geocoding-cloud"]);
// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API
let clientBuilder = new SmartyCore.ClientBuilder(credentials);
// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API
let client = clientBuilder.buildUsReverseGeoClient();

let lookup1 = new Lookup(40.27644, -111.65747);
Expand Down
18 changes: 8 additions & 10 deletions examples/us_street.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ import SmartySDK from "smartystreets-javascript-sdk";
const SmartyCore = SmartySDK.core;
const Lookup = SmartySDK.usStreet.Lookup;

// for Server-to-server requests, use this code:
// let authId = process.env.SMARTY_AUTH_ID;
// let authToken = process.env.SMARTY_AUTH_TOKEN;
// const credentials = new SmartyCore.StaticCredentials(authId, authToken);

// for client-side requests (browser/mobile), use this code:
let key = process.env.SMARTY_EMBEDDED_KEY;
const credentials = new SmartyCore.SharedCredentials(key);
// let key = process.env.SMARTY_EMBEDDED_KEY;
// const credentials = new SmartyCore.SharedCredentials(key);

// for Server-to-server requests, use this code:
let authId = process.env.SMARTY_AUTH_ID;
let authToken = process.env.SMARTY_AUTH_TOKEN;
const credentials = new SmartyCore.BasicAuthCredentials(authId, authToken);

// The appropriate license values to be used for your subscriptions
// can be found on the Subscription page of the account dashboard.
// https://www.smarty.com/docs/cloud/licensing
let clientBuilder = new SmartyCore.ClientBuilder(credentials).withLicenses([
"us-rooftop-geocoding-cloud",
]);
let clientBuilder = new SmartyCore.ClientBuilder(credentials);
// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API

let client = clientBuilder.buildUsStreetApiClient();
Expand Down
14 changes: 7 additions & 7 deletions examples/us_zipcode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import SmartySDK from "smartystreets-javascript-sdk";
const SmartyCore = SmartySDK.core;
const Lookup = SmartySDK.usZipcode.Lookup;

// for Server-to-server requests, use this code:
// let authId = process.env.SMARTY_AUTH_ID;
// let authToken = process.env.SMARTY_AUTH_TOKEN;
// const credentials = new SmartyCore.StaticCredentials(authId, authToken);

// for client-side requests (browser/mobile), use this code:
let key = process.env.SMARTY_EMBEDDED_KEY;
const credentials = new SmartyCore.SharedCredentials(key);
// let key = process.env.SMARTY_EMBEDDED_KEY;
// const credentials = new SmartyCore.SharedCredentials(key);

// for Server-to-server requests, use this code:
let authId = process.env.SMARTY_AUTH_ID;
let authToken = process.env.SMARTY_AUTH_TOKEN;
const credentials = new SmartyCore.BasicAuthCredentials(authId, authToken);

let clientBuilder = new SmartyCore.ClientBuilder(credentials);
// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API
Expand Down
2 changes: 2 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ClientBuilder from "./src/ClientBuilder.js";
import buildClient from "./src/util/buildClients.js";
import SharedCredentials from "./src/SharedCredentials.js";
import StaticCredentials from "./src/StaticCredentials.js";
import BasicAuthCredentials from "./src/BasicAuthCredentials.js";
import Errors from "./src/Errors.js";

import LookupUSStreet from "./src/us_street/Lookup.js";
Expand Down Expand Up @@ -41,6 +42,7 @@ export const core = {
buildClient,
SharedCredentials,
StaticCredentials,
BasicAuthCredentials,
Errors,
};

Expand Down
19 changes: 19 additions & 0 deletions src/BasicAuthCredentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Request } from "./types";

export default class BasicAuthCredentials {
private authId: string;
private authToken: string;

constructor(authId: string, authToken: string) {
if (!authId || !authToken) {
throw new Error("credentials (auth id, auth token) required");
}
this.authId = authId;
this.authToken = authToken;
}

sign(request: Request): void {
const encoded = Buffer.from(`${this.authId}:${this.authToken}`).toString("base64");
request.headers["Authorization"] = `Basic ${encoded}`;
}
}
3 changes: 2 additions & 1 deletion src/ClientBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const BaseUrlSender = require("./BaseUrlSender");
const AgentSender = require("./AgentSender");
const StaticCredentials = require("./StaticCredentials");
const SharedCredentials = require("./SharedCredentials");
const BasicAuthCredentials = require("./BasicAuthCredentials");
const CustomHeaderSender = require("./CustomHeaderSender");
const StatusCodeSender = require("./StatusCodeSender");
const LicenseSender = require("./LicenseSender");
Expand Down Expand Up @@ -54,7 +55,7 @@ class ClientBuilder {
this.customQueries = new Map();

function credentialsProvided() {
return signer instanceof StaticCredentials || signer instanceof SharedCredentials;
return signer instanceof StaticCredentials || signer instanceof SharedCredentials || signer instanceof BasicAuthCredentials;
}
}

Expand Down
100 changes: 100 additions & 0 deletions tests/test_BasicAuthCredentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { expect } from "chai";
import BasicAuthCredentials from "../src/BasicAuthCredentials.js";
import Request from "../src/Request.js";

describe("BasicAuthCredentials", function () {
it("creates credentials with valid authId and authToken", function () {
const credentials = new BasicAuthCredentials("testID", "testToken");

expect(credentials).to.not.be.null;
});

it("throws error when authId is empty", function () {
expect(() => new BasicAuthCredentials("", "testToken")).to.throw(
"credentials (auth id, auth token) required",
);
});

it("throws error when authToken is empty", function () {
expect(() => new BasicAuthCredentials("testID", "")).to.throw(
"credentials (auth id, auth token) required",
);
});

it("throws error when both authId and authToken are empty", function () {
expect(() => new BasicAuthCredentials("", "")).to.throw(
"credentials (auth id, auth token) required",
);
});

it("creates credentials with special characters", function () {
const credentials = new BasicAuthCredentials("test@id#123", "token!@#$%^&*()");

expect(credentials).to.not.be.null;
});

it("signs request with Authorization header", function () {
const credentials = new BasicAuthCredentials("myID", "myToken");
const request = new Request();

credentials.sign(request);

expect("Authorization" in request.headers).to.equal(true);
const authHeader = request.headers["Authorization"] as string;
expect(authHeader.startsWith("Basic ")).to.equal(true);

const encoded = authHeader.substring(6);
const decoded = Buffer.from(encoded, "base64").toString("utf-8");
expect(decoded).to.equal("myID:myToken");
});

it("signs request with password containing colon", function () {
const credentials = new BasicAuthCredentials("validUserID", "password:with:colons");
const request = new Request();

credentials.sign(request);

const authHeader = request.headers["Authorization"] as string;
const encoded = authHeader.substring(6);
const decoded = Buffer.from(encoded, "base64").toString("utf-8");
expect(decoded).to.equal("validUserID:password:with:colons");
});

it("signs request with special characters", function () {
const credentials = new BasicAuthCredentials("user@domain.com", "p@ssw0rd!");
const request = new Request();

credentials.sign(request);

const authHeader = request.headers["Authorization"] as string;
const encoded = authHeader.substring(6);
const decoded = Buffer.from(encoded, "base64").toString("utf-8");
expect(decoded).to.equal("user@domain.com:p@ssw0rd!");
});

it("signs request with unicode characters", function () {
const credentials = new BasicAuthCredentials("用户", "密码");
const request = new Request();

credentials.sign(request);

const authHeader = request.headers["Authorization"] as string;
const encoded = authHeader.substring(6);
const decoded = Buffer.from(encoded, "base64").toString("utf-8");
expect(decoded).to.equal("用户:密码");
});

it("overwrites existing Authorization header", function () {
const credentials = new BasicAuthCredentials("newID", "newToken");
const request = new Request();
request.headers["Authorization"] = "Bearer oldtoken";

credentials.sign(request);

const authHeader = request.headers["Authorization"] as string;
expect(authHeader.startsWith("Basic ")).to.equal(true);
const encoded = authHeader.substring(6);
const decoded = Buffer.from(encoded, "base64").toString("utf-8");
expect(decoded).to.equal("newID:newToken");
});
});
Loading