You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.5 KiB
78 lines
2.5 KiB
7 years ago
|
import { observable } from "mobx";
|
||
|
|
||
7 years ago
|
import HttpApi, { ApiError } from "@client/state/HttpApi";
|
||
|
import { Token } from "@client/state/Token";
|
||
7 years ago
|
import { TokenGrantPasswordRequest, TokenGrantRefreshRequest, TokenGrantResponse } from "@common/httpApi";
|
||
7 years ago
|
import logger from "@common/logger";
|
||
|
|
||
7 years ago
|
const log = logger.child({ source: "TokenStore"});
|
||
|
|
||
7 years ago
|
const LOCAL_STORAGE_KEY = "TokenStore";
|
||
|
|
||
7 years ago
|
export class TokenStore {
|
||
|
@observable accessToken: Token = new Token();
|
||
|
@observable refreshToken: Token = new Token();
|
||
|
|
||
|
private api: HttpApi;
|
||
|
|
||
|
constructor(api: HttpApi) {
|
||
|
this.api = api;
|
||
|
}
|
||
|
|
||
7 years ago
|
clear() {
|
||
|
this.accessToken.token = null;
|
||
|
this.refreshToken.token = null;
|
||
|
this.saveLocalStorage();
|
||
|
}
|
||
|
|
||
7 years ago
|
saveLocalStorage() {
|
||
|
window.localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(this.toJSON()));
|
||
|
}
|
||
|
|
||
|
loadLocalStorage() {
|
||
|
const data = window.localStorage.getItem(LOCAL_STORAGE_KEY);
|
||
|
if (data) {
|
||
|
const data2 = JSON.parse(data);
|
||
|
this.updateFromJson(data2);
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
async grantPassword(username: string, password: string) {
|
||
|
const request: TokenGrantPasswordRequest = {
|
||
|
grant_type: "password", username, password,
|
||
|
};
|
||
|
const response: TokenGrantResponse = await this.api.makeRequest("/token/grant", {
|
||
|
method: "POST",
|
||
|
}, request);
|
||
|
this.accessToken.token = response.access_token;
|
||
|
this.refreshToken.token = response.refresh_token;
|
||
7 years ago
|
this.saveLocalStorage();
|
||
7 years ago
|
log.debug({ aud: this.accessToken.claims!.aud }, "got password grant tokens");
|
||
7 years ago
|
}
|
||
|
|
||
|
async grantRefresh() {
|
||
|
if (!this.refreshToken.isValid) {
|
||
7 years ago
|
throw new ApiError("can not grant refresh with invalid refresh_token");
|
||
7 years ago
|
}
|
||
|
const request: TokenGrantRefreshRequest = {
|
||
|
grant_type: "refresh", refresh_token: this.refreshToken.token!,
|
||
|
};
|
||
|
const response: TokenGrantResponse = await this.api.makeRequest("/token/grant", {
|
||
|
method: "POST",
|
||
|
}, request);
|
||
|
this.accessToken.token = response.access_token;
|
||
|
this.refreshToken.token = response.refresh_token;
|
||
7 years ago
|
this.saveLocalStorage();
|
||
7 years ago
|
log.debug({ aud: this.accessToken.claims!.aud }, "got refresh grant tokens");
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
|
toJSON() {
|
||
7 years ago
|
return { accessToken: this.accessToken.toJSON(), refreshToken: this.refreshToken.toJSON() };
|
||
7 years ago
|
}
|
||
|
|
||
|
updateFromJson(json: any) {
|
||
|
this.accessToken.token = json.accessToken;
|
||
|
this.refreshToken.token = json.refreshToken;
|
||
|
}
|
||
7 years ago
|
}
|