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

map-specific access token #8364

Merged
merged 19 commits into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
35 changes: 35 additions & 0 deletions debug/token.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html>
<head>
<title>Mapbox GL JS debug page</title>
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<link rel='stylesheet' href='/dist/mapbox-gl.css' />
<style>
body { margin: 0; padding: 0; }
html, body, #map { height: 100%; }
</style>
</head>

<body>
<div id='map'></div>

<script src='/dist/mapbox-gl-dev.js'></script>
<script src='/debug/access_token_generated.js'></script>
peterqliu marked this conversation as resolved.
Show resolved Hide resolved
<script>

var mapToken = mapboxgl.accessToken;
mapboxgl.accessToken = 'notAToken';

var map = window.map = new mapboxgl.Map({
container: 'map',
zoom: 12.5,
center: [-77.01866, 38.888],
style: 'mapbox://styles/mapbox/streets-v10',
hash: true,
accessToken: mapToken
});

</script>
</body>
</html>
1 change: 0 additions & 1 deletion src/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ class Style extends Evented {

url = this.map._requestManager.normalizeStyleURL(url, options.accessToken);
const request = this.map._requestManager.transformRequest(url, ResourceType.Style);

this._request = getJSON(request, (error: ?Error, json: ?Object) => {
this._request = null;
if (error) {
Expand Down
7 changes: 5 additions & 2 deletions src/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ type MapOptions = {
pitch?: number,
renderWorldCopies?: boolean,
maxTileCacheSize?: number,
transformRequest?: RequestTransformFunction
transformRequest?: RequestTransformFunction,
accessToken: string
};

const defaultMinZoom = 0;
Expand Down Expand Up @@ -211,6 +212,8 @@ const defaultOptions = {
* @param {boolean} [options.collectResourceTiming=false] If `true`, Resource Timing API information will be collected for requests made by GeoJSON and Vector Tile web workers (this information is normally inaccessible from the main Javascript thread). Information will be returned in a `resourceTiming` property of relevant `data` events.
* @param {number} [options.fadeDuration=300] Controls the duration of the fade-in/fade-out animation for label collisions, in milliseconds. This setting affects all symbol layers. This setting does not affect the duration of runtime styling transitions or raster tile cross-fading.
* @param {boolean} [options.crossSourceCollisions=true] If `true`, symbols from multiple sources can collide with each other during collision detection. If `false`, collision detection is run separately for the symbols in each source.
* @param {string} [options.accessToken=null] If specified, map will use this token instead of the one defined in mapboxgl.accessToken.

* @example
* var map = new mapboxgl.Map({
* container: 'map',
Expand Down Expand Up @@ -331,7 +334,7 @@ class Map extends Camera {
this._renderTaskQueue = new TaskQueue();
this._controls = [];
this._mapId = uniqueId();
this._requestManager = new RequestManager(options.transformRequest);
this._requestManager = new RequestManager(options.transformRequest, options.accessToken);

if (typeof options.container === 'string') {
this._container = window.document.getElementById(options.container);
Expand Down
4 changes: 3 additions & 1 deletion src/util/mapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ export class RequestManager {
_skuTokenExpiresAt: number;
_transformRequestFn: ?RequestTransformFunction;

constructor(transformRequestFn?: RequestTransformFunction) {
constructor(transformRequestFn?: RequestTransformFunction, customAccessToken?: string) {
this._transformRequestFn = transformRequestFn;
if (customAccessToken) config.ACCESS_TOKEN = customAccessToken;
peterqliu marked this conversation as resolved.
Show resolved Hide resolved

this._createSkuToken();
}

Expand Down
13 changes: 13 additions & 0 deletions test/unit/ui/map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ test('Map', (t) => {
t.end();
});

// t.test('map-specific token', (t) => {\
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can we instantiate a map with a bad token?

Would like to add a test for the positive case, where it starts with mapboxgl.accessToken = badtoken but gets overwritten by a valid token inside the map options.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can we instantiate a map with a bad token?
The unit tests don't exercise any access token specific logic.

Map options are set-only with no ability to read back to options that were used to instantiate a map object. Since the access token is passed through to the RequestManager unit tests are probably suited to the behavior of that class.

// t.end();
// });

t.test('bad map-specific token breaks map', (t) => {
const container = window.document.createElement('div');
Object.defineProperty(container, 'offsetWidth', {value: 512});
Object.defineProperty(container, 'offsetHeight', {value: 512});
createMap(t, {accessToken:'notAToken'});
t.error();
t.end();
});

t.test('initial bounds in constructor options', (t) => {
const container = window.document.createElement('div');
Object.defineProperty(container, 'offsetWidth', {value: 512});
Expand Down