Skip to content

Commit

Permalink
feat: use @googlemaps/url-signature (#690)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoehnelt authored Oct 25, 2021
1 parent 80a15ec commit ac9c5ba
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 27 deletions.
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
"test:all": "jest"
},
"dependencies": {
"@googlemaps/url-signature": "^1.0.2",
"agentkeepalive": "^4.1.0",
"axios": "^0.21.1",
"crypto-js": "4.1.1",
"query-string": "^7.0.1",
"retry-axios": "^2.2.1"
},
Expand Down
8 changes: 0 additions & 8 deletions src/serialize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import { LatLng, LatLngLiteral } from "./common";
import {
createPremiumPlanQueryString,
createPremiumPlanSignature,
latLngArrayToStringMaybeEncoded,
latLngBoundsToString,
latLngToString,
Expand Down Expand Up @@ -161,10 +160,3 @@ test("createPremiumPlanQueryString", () => {
expect(createPremiumPlanQueryString(serializedParams, queryStringOptions, baseUrl))
.toEqual('avoid=ferries&client=testClient&destination=38.8977%2C-77.0365&mode=driving&origin=33.8121%2C-117.9190&units=imperial&signature=YRJoTd6ohbpsR14WkWv3S7H6MqU=');
});

test("createPremiumPlanSignature", () => {
const unsignedUrl = "https://test.url/maps/api/directions/json?avoid=ferries&client=testClient&destination=38.8977%2C-77.0365&mode=driving&origin=33.8121%2C-117.9190&units=imperial";
const clientSecret = "testClientSecret";

expect(createPremiumPlanSignature(unsignedUrl, clientSecret)).toEqual("YRJoTd6ohbpsR14WkWv3S7H6MqU=");
});
19 changes: 2 additions & 17 deletions src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
import { LatLng, LatLngBounds, LatLngLiteral } from "./common";

import { encodePath } from "./util";
import * as CryptoJS from "crypto-js";
import { createSignature} from "@googlemaps/url-signature";
import { stringify as qs } from "query-string";
import { URL } from "url";

const separator = "|";

Expand Down Expand Up @@ -148,22 +147,8 @@ export function createPremiumPlanQueryString(

const partialQueryString = qs(serializedParams, queryStringOptions);
const unsignedUrl = `${baseUrl}?${partialQueryString}`;
const signature = createPremiumPlanSignature(unsignedUrl, clientSecret);
const signature = createSignature(unsignedUrl, clientSecret);

// The signature must come last
return `${partialQueryString}&signature=${signature}`;
}

export function createPremiumPlanSignature(unsignedUrl: string, clientSecret: string): string {
// Strip off the protocol, scheme, and host portions of the URL, leaving only the path and the query
const fullUrl = new URL(unsignedUrl);
const pathAndQuery = `${fullUrl.pathname}${fullUrl.search}`;
// Convert from 'web safe' base64 to true base64
const unsafeClientSecret = clientSecret.replace(/-/g, "+").replace(/_/g, "/");
// Base64 decode the secret
const decodedSecret = CryptoJS.enc.Base64.parse(unsafeClientSecret)
// Sign the url with the decoded secret
const unsafeSignature = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA1, decodedSecret).update(pathAndQuery).finalize().toString(CryptoJS.enc.Base64)
// Convert from true base64 to 'web safe' base64
return unsafeSignature.replace(/\+/g, "-").replace(/\//g, "_");
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"declaration": true,
"declarationDir": "./dist",
"resolveJsonModule": true,
"lib": []
"lib": ["ESNext"]
},
"include": [
"src/**/*.ts"
Expand Down

0 comments on commit ac9c5ba

Please sign in to comment.