|
|
@ -1,11 +1,6 @@ |
|
|
|
import { observable } from "mobx"; |
|
|
|
import { observable } from "mobx"; |
|
|
|
|
|
|
|
|
|
|
|
import HttpApi, { ApiError } from "@client/state/HttpApi"; |
|
|
|
|
|
|
|
import { Token } from "@client/state/Token"; |
|
|
|
import { Token } from "@client/state/Token"; |
|
|
|
import { TokenGrantPasswordRequest, TokenGrantRefreshRequest, TokenGrantResponse } from "@common/httpApi"; |
|
|
|
|
|
|
|
import logger from "@common/logger"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const log = logger.child({ source: "TokenStore"}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const LOCAL_STORAGE_KEY = "TokenStore"; |
|
|
|
const LOCAL_STORAGE_KEY = "TokenStore"; |
|
|
|
|
|
|
|
|
|
|
@ -13,12 +8,6 @@ export class TokenStore { |
|
|
|
@observable accessToken: Token = new Token(); |
|
|
|
@observable accessToken: Token = new Token(); |
|
|
|
@observable refreshToken: Token = new Token(); |
|
|
|
@observable refreshToken: Token = new Token(); |
|
|
|
|
|
|
|
|
|
|
|
private api: HttpApi; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor(api: HttpApi) { |
|
|
|
|
|
|
|
this.api = api; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
clear() { |
|
|
|
clear() { |
|
|
|
this.accessToken.token = null; |
|
|
|
this.accessToken.token = null; |
|
|
|
this.refreshToken.token = null; |
|
|
|
this.refreshToken.token = null; |
|
|
@ -37,35 +26,6 @@ export class TokenStore { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
this.saveLocalStorage(); |
|
|
|
|
|
|
|
log.debug({ aud: this.accessToken.claims!.aud }, "got password grant tokens"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async grantRefresh() { |
|
|
|
|
|
|
|
if (!this.refreshToken.isValid) { |
|
|
|
|
|
|
|
throw new ApiError("can not grant refresh with invalid refresh_token"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
this.saveLocalStorage(); |
|
|
|
|
|
|
|
log.debug({ aud: this.accessToken.claims!.aud }, "got refresh grant tokens"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
toJSON() { |
|
|
|
toJSON() { |
|
|
|
return { accessToken: this.accessToken.toJSON(), refreshToken: this.refreshToken.toJSON() }; |
|
|
|
return { accessToken: this.accessToken.toJSON(), refreshToken: this.refreshToken.toJSON() }; |
|
|
|
} |
|
|
|
} |
|
|
|