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> {
#running = false;
startRefreshLoop() {
// TODO: don't allow more than one
this.refreshLoop();
if (this.#running) {
console.error('GcpApi startRefreshLoop: already running');
} else {
this.#running = true;
this.refreshLoop();
}
}
private refreshLoop() {
console.log("refreshing GCP token");
console.log("GcpApi refreshLoop");
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(() => {
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() {
const token = await this.spider('noun', 'gcp-token', 'get-gcp-token', {});
return token['gcp-token'];

View File

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

View File

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