interface: storing the gcp token

This commit is contained in:
J 2021-02-24 22:32:20 +00:00
parent 0a23c0f4a4
commit 3c2ce636e7
3 changed files with 26 additions and 7 deletions

View File

@ -4,21 +4,38 @@ import {GcpToken} from '../types/gcp-state';
export default class GcpApi extends BaseApi<StoreState> { export default class GcpApi extends BaseApi<StoreState> {
#running = false;
startRefreshLoop() { startRefreshLoop() {
// TODO: don't allow more than one if (this.#running) {
this.refreshLoop(); console.error('GcpApi startRefreshLoop: already running');
} else {
this.#running = true;
this.refreshLoop();
}
} }
private refreshLoop() { private refreshLoop() {
console.log("refreshing GCP token"); console.log("GcpApi refreshLoop");
this.refreshToken().then(({accessKey, expiresIn}: GcpToken) => { this.refreshToken().then(({accessKey, expiresIn}: GcpToken) => {
console.log("new token: ", accessKey); console.log("GcpApi new token");
// XX maybe bad?
this.store.state.gcp.accessKey = accessKey;
const timeout = this.refreshInterval(expiresIn);
console.log("GcpApi refreshing in", timeout);
setTimeout(() => { setTimeout(() => {
this.refreshLoop(); this.refreshLoop();
}, expiresIn); }, timeout);
}); });
} }
private refreshInterval(expiresIn: number) {
// Give ourselves 30 seconds for processing delays, but never refresh
// sooner than 10 minute from now. (The expiry window should be about an
// hour.)
return Math.max(600_000, expiresIn - 30_000);
}
private async refreshToken() { private async refreshToken() {
const token = await this.spider('noun', 'gcp-token', 'get-gcp-token', {}); const token = await this.spider('noun', 'gcp-token', 'get-gcp-token', {});
return token['gcp-token']; return token['gcp-token'];

View File

@ -71,7 +71,9 @@ export default class GlobalStore extends BaseStore<StoreState> {
}, },
weather: {}, weather: {},
userLocation: null, userLocation: null,
gcp: {}, gcp: {
accessKey: null
},
s3: { s3: {
configuration: { configuration: {
buckets: new Set(), buckets: new Set(),

View File

@ -4,5 +4,5 @@ export interface GcpToken {
}; };
export interface GcpState { export interface GcpState {
token: GcpToken; accessKey: string;
}; };