Skip to content

Commit

Permalink
Add CesiumIonAuthPlugin (#638)
Browse files Browse the repository at this point in the history
* Add ion auth plugin

* Add Cesium ion auth plugin

* Provide option for url or passing into the cesium ion plugin

* Updates
  • Loading branch information
gkjohnson committed Jul 23, 2024
1 parent 6e79f65 commit daf84e8
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 69 deletions.
4 changes: 2 additions & 2 deletions src/base/TilesRendererBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class TilesRendererBase {

}

constructor( url ) {
constructor( url = null ) {

// state
this.tileSets = {};
Expand Down Expand Up @@ -167,7 +167,7 @@ export class TilesRendererBase {
const rootTileSet = tileSets[ this.rootURL ];
if ( ! ( this.rootURL in tileSets ) ) {

this.loadRootTileSet( this.rootURL );
this.invokeOnePlugin( plugin => plugin.loadRootTileSet && plugin.loadRootTileSet( this.rootURL ) );
return;

} else if ( ! rootTileSet || ! rootTileSet.root ) {
Expand Down
99 changes: 99 additions & 0 deletions src/three/plugins/CesiumIonAuthPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const UNLOADED = 0;
const LOADING = 1;
const LOADED = 2;
const FAILED = 3;

export class CesiumIonAuthPlugin {

constructor( { apiToken, assetId = null } ) {

this.name = 'CESIUM_ION_AUTH_PLUGIN';
this.apiToken = apiToken;
this.assetId = assetId;
this.tiles = null;
this._tileSetVersion = - 1;
this._tokenState = UNLOADED;
this._loadPromise = null;

}

init( tiles ) {

this.tiles = tiles;

}

loadRootTileSet( rootUrl ) {

if ( this._tokenState === UNLOADED ) {

this._tokenState = LOADING;

// construct the url to fetch the endpoint
let url;
if ( this.assetId === null ) {

url = new URL( rootUrl );

} else {

url = new URL( `https://api.cesium.com/v1/assets/${ this.assetId }/endpoint` );

}

url.searchParams.append( 'access_token', this.apiToken );

// load the ion asset information
this._loadPromise = fetch( url, { mode: 'cors' } )
.then( res => {

if ( res.ok ) {

return res.json();

} else {

return Promise.reject( new Error( `${ res.status } : ${ res.statusText }` ) );

}

} ).then( json => {

this._tokenState = LOADED;

// retrieve the url version
const tiles = this.tiles;
const url = new URL( json.url );
this._tileSetVersion = url.searchParams.get( 'v' );
tiles.rootURL = url;
tiles.fetchOptions.headers = tiles.fetchOptions.headers || {};
tiles.fetchOptions.headers.Authorization = `Bearer ${ json.accessToken }`;

return tiles.loadRootTileSet( tiles.rootURL );

} ).catch( err => {

this._tokenState = FAILED;
return Promise.reject( err );

} );

}

return this._loadPromise;

}

preprocessURL( uri ) {

uri = new URL( uri );
if ( /^http/.test( uri.protocol ) && this._tileSetVersion != - 1 ) {

uri.searchParams.append( 'v', this._tileSetVersion );

}
return uri.toString();

}

}
69 changes: 2 additions & 67 deletions src/three/renderers/CesiumIonTilesRenderer.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,14 @@
import { TilesRenderer } from '../TilesRenderer.js';
import { DebugTilesRenderer } from '../DebugTilesRenderer.js';
import { CesiumIonAuthPlugin } from '../plugins/CesiumIonAuthPlugin.js';

const UNLOADED = 0;
const LOADING = 1;
const LOADED = 2;
const FAILED = 3;
const CesiumIonTilesRendererMixin = base => class extends base {

constructor( ionAssetId, ionAccessToken ) {

super();
this._tokenState = UNLOADED;
this._ionAccessToken = ionAccessToken;
this._ionAssetId = ionAssetId;
this._tileSetVersion = - 1;
this.preprocessURL = uri => {

uri = new URL( uri );
if ( /^http/.test( uri.protocol ) && this._tileSetVersion != - 1 ) {

uri.searchParams.append( 'v', this._tileSetVersion );

}
return uri.toString();

};

}

update() {

const state = this._tokenState;
if ( state === UNLOADED ) {

this._tokenState = LOADING;

const url = new URL( `https://api.cesium.com/v1/assets/${ this._ionAssetId }/endpoint` );
url.searchParams.append( 'access_token', this._ionAccessToken );

fetch( url, { mode: 'cors' } ).then( res => {

if ( res.ok ) {

return res.json();

} else {

return Promise.reject( `${ res.status } : ${ res.statusText }` );

}

} ).then( json => {

this._tokenState = LOADED;

// retrieve the url version
const url = new URL( json.url );
this._tileSetVersion = url.searchParams.get( 'v' );
this.rootURL = url;
this.fetchOptions.headers = this.fetchOptions.headers || {};
this.fetchOptions.headers.Authorization = `Bearer ${ json.accessToken }`;
// Actually load the tileset now that we got its url from the cesium ion server
super.update();

} ).catch( () => {

this._tokenState = FAILED;

} );

} else if ( state === LOADED ) {

super.update();

}
this.registerPlugin( new CesiumIonAuthPlugin( { apiToken: ionAccessToken, assetId: ionAssetId } ) );

}

Expand Down

0 comments on commit daf84e8

Please sign in to comment.