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

feat: add support for auth_referrer_policy #584

Merged
merged 3 commits into from
Apr 11, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ test.each([
{ client: "bar", channel: "foo" },
"https://maps.googleapis.com/maps/api/js?callback=__googleMapsCallback&channel=foo&client=bar",
],
[
{ authReferrerPolicy: "origin" },
"https://maps.googleapis.com/maps/api/js?callback=__googleMapsCallback&auth_referrer_policy=origin",
],
])("createUrl is correct", (options: LoaderOptions, expected: string) => {
const loader = new Loader(options);
expect(loader.createUrl()).toEqual(expected);
Expand Down
36 changes: 30 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@ export interface LoaderOptions {
* The number of script load retries.
*/
retries?: number;
/**
* Maps JS customers can configure HTTP Referrer Restrictions in the Cloud
* Console to limit which URLs are allowed to use a particular API Key. By
* default, these restrictions can be configured to allow only certain paths
* to use an API Key. If any URL on the same domain or origin may use the API
* Key, you can set `auth_referrer_policy=origin` to limit the amount of data
* sent when authorizing requests from the Maps JavaScript API. This is
* available starting in version 3.46. When this parameter is specified and
* HTTP Referrer Restrictions are enabled on Cloud Console, Maps JavaScript
* API will only be able to load if there is an HTTP Referrer Restriction that
* matches the current website's domain without a path specified.
*/
authReferrerPolicy?: "origin";
}

/**
Expand Down Expand Up @@ -249,6 +262,10 @@ export class Loader {
* See [[LoaderOptions.url]]
*/
public readonly url: string;
/**
* See [[LoaderOptions.authReferrerPolicy]]
*/
public readonly authReferrerPolicy: "origin";

private CALLBACK = "__googleMapsCallback";
private callbacks: ((e: ErrorEvent) => void)[] = [];
Expand All @@ -268,30 +285,32 @@ export class Loader {
*/
constructor({
apiKey,
authReferrerPolicy,
channel,
client,
id = DEFAULT_ID,
libraries = [],
language,
region,
version,
libraries = [],
mapIds,
nonce,
region,
retries = 3,
url = "https://maps.googleapis.com/maps/api/js",
version,
}: LoaderOptions) {
this.version = version;
this.apiKey = apiKey;
this.authReferrerPolicy = authReferrerPolicy;
this.channel = channel;
this.client = client;
this.id = id || DEFAULT_ID; // Do not allow empty string
this.libraries = libraries;
this.language = language;
this.region = region;
this.libraries = libraries;
this.mapIds = mapIds;
this.nonce = nonce;
this.region = region;
this.retries = retries;
this.url = url;
this.version = version;

if (Loader.instance) {
if (!isEqual(this.options, Loader.instance.options)) {
Expand Down Expand Up @@ -321,6 +340,7 @@ export class Loader {
mapIds: this.mapIds,
nonce: this.nonce,
url: this.url,
authReferrerPolicy: this.authReferrerPolicy,
};
}

Expand Down Expand Up @@ -383,6 +403,10 @@ export class Loader {
url += `&map_ids=${this.mapIds.join(",")}`;
}

if (this.authReferrerPolicy) {
url += `&auth_referrer_policy=${this.authReferrerPolicy}`;
}

return url;
}

Expand Down