diff --git a/src/authentication/IAuthenticator.ts b/src/authentication/IAuthenticator.ts index ff5da4319..0a2eeace2 100644 --- a/src/authentication/IAuthenticator.ts +++ b/src/authentication/IAuthenticator.ts @@ -4,13 +4,13 @@ export interface IAuthenticator { /** * Returns access token for current session. */ - getAccessToken(): string; + getAccessToken(): Promise; /** * Sets new token for the session. * @param accessToken {string} Access token in SharedAccessSignature or Bearer token format. */ - setAccessToken(accessToken: string): void; + setAccessToken(accessToken: string): Promise; /** * Parses specified access token. @@ -21,10 +21,10 @@ export interface IAuthenticator { /** * Clears access token from current session. */ - clearAccessToken(): void; + clearAccessToken(): Promise; /** * Checks if current user is signed in. */ - isAuthenticated(): boolean; + isAuthenticated(): Promise; } \ No newline at end of file diff --git a/src/components/app/app.ts b/src/components/app/app.ts index b722c75a0..843ef0a7a 100644 --- a/src/components/app/app.ts +++ b/src/components/app/app.ts @@ -35,7 +35,7 @@ export class App { } try { - const token = this.authenticator.getAccessToken(); + const token = await this.authenticator.getAccessToken(); if (!token) { const managementApiAccessToken = settings["managementApiAccessToken"]; @@ -47,7 +47,7 @@ export class App { return; } - this.authenticator.setAccessToken(accessToken.value); + await this.authenticator.setAccessToken(accessToken.value); } } catch (error) { diff --git a/src/components/content/content.ts b/src/components/content/content.ts index c6200087d..ad734d77d 100644 --- a/src/components/content/content.ts +++ b/src/components/content/content.ts @@ -25,7 +25,7 @@ export class ContentWorkshop { try { this.logger.traceEvent("Click: Publish website"); - const accessToken = this.authenticator.getAccessToken(); + const accessToken = await this.authenticator.getAccessToken(); await this.httpClient.send({ url: "/publish", method: "POST", headers: [{ name: "Authorization", value: accessToken }] }); this.viewManager.notifySuccess("Operations", `The website is being published...`); this.viewManager.closeWorkshop("content-workshop"); diff --git a/src/components/defaultAuthenticator.ts b/src/components/defaultAuthenticator.ts index be145d8b3..2cf6b02a6 100644 --- a/src/components/defaultAuthenticator.ts +++ b/src/components/defaultAuthenticator.ts @@ -3,20 +3,20 @@ import { Utils } from "../utils"; import { IAuthenticator, AccessToken } from "./../authentication"; export class DefaultAuthenticator implements IAuthenticator { - public getAccessToken(): string { + public async getAccessToken(): Promise { return sessionStorage.getItem("accessToken"); } - public setAccessToken(accessToken: string): void { + public async setAccessToken(accessToken: string): Promise { sessionStorage.setItem("accessToken", accessToken); } - public clearAccessToken(): void { + public async clearAccessToken(): Promise { sessionStorage.removeItem("accessToken"); } - public isAuthenticated(): boolean { - const accessToken = this.getAccessToken(); + public async isAuthenticated(): Promise { + const accessToken = await this.getAccessToken(); if (!accessToken) { return false; diff --git a/src/components/staticAuthenticator.ts b/src/components/staticAuthenticator.ts index 45173f2ac..74ec40d20 100644 --- a/src/components/staticAuthenticator.ts +++ b/src/components/staticAuthenticator.ts @@ -3,19 +3,20 @@ import { IAuthenticator } from "../authentication"; export class StaticAuthenticator implements IAuthenticator { private accessToken: string; - public getAccessToken(): string { + public async getAccessToken(): Promise { return this.accessToken; } - public setAccessToken(token: string): void { + public async setAccessToken(token: string): Promise { this.accessToken = token; } - public clearAccessToken(): void { + public async clearAccessToken(): Promise { this.accessToken = undefined; } - public isAuthenticated(): boolean { - return !!this.getAccessToken(); + public async isAuthenticated(): Promise { + const accessToken = await this.getAccessToken(); + return !!accessToken; } } \ No newline at end of file diff --git a/src/components/users/user-signup-social/ko/runtime/user-signup-social.ts b/src/components/users/user-signup-social/ko/runtime/user-signup-social.ts index 3567465f8..100179b81 100644 --- a/src/components/users/user-signup-social/ko/runtime/user-signup-social.ts +++ b/src/components/users/user-signup-social/ko/runtime/user-signup-social.ts @@ -112,7 +112,7 @@ export class UserSignupSocial { } const sasToken = matches[1]; - this.authenticator.setAccessToken(`SharedAccessSignature ${sasToken}`); + await this.authenticator.setAccessToken(`SharedAccessSignature ${sasToken}`); this.router.navigateTo(Constants.pageUrlHome); } diff --git a/src/routing/signOutRouteGuard.ts b/src/routing/signOutRouteGuard.ts index 5e2397bd9..63dbddf72 100644 --- a/src/routing/signOutRouteGuard.ts +++ b/src/routing/signOutRouteGuard.ts @@ -8,7 +8,7 @@ export class SignOutRouteGuard implements RouteGuard { public async canActivate(route: Route): Promise { if (route.hash === hashSignOut) { - this.authenticator.clearAccessToken(); + await this.authenticator.clearAccessToken(); location.assign(pageUrlHome); } return true; diff --git a/src/services/aadService.ts b/src/services/aadService.ts index a5341e9a2..69f9ad96c 100644 --- a/src/services/aadService.ts +++ b/src/services/aadService.ts @@ -52,7 +52,7 @@ export class AadService { } const sasToken = matches[1]; - this.authenticator.setAccessToken(`SharedAccessSignature ${sasToken}`); + await this.authenticator.setAccessToken(`SharedAccessSignature ${sasToken}`); await this.router.navigateTo(Constants.pageUrlHome); } diff --git a/src/services/backendService.ts b/src/services/backendService.ts index 0fae8cf8e..d9cace56a 100644 --- a/src/services/backendService.ts +++ b/src/services/backendService.ts @@ -70,7 +70,7 @@ export class BackendService { } public async sendChangePassword(changePasswordRequest: ChangePasswordRequest): Promise { - const authToken = this.authenticator.getAccessToken(); + const authToken = await this.authenticator.getAccessToken(); if (!authToken) { throw Error("Auth token not found"); @@ -107,7 +107,7 @@ export class BackendService { } public async getDelegationUrl(action: DelegationAction, delegationParameters: {}): Promise { - const authToken = this.authenticator.getAccessToken(); + const authToken = await this.authenticator.getAccessToken(); if (!authToken) { throw Error("Auth token not found"); diff --git a/src/services/mapiClient.ts b/src/services/mapiClient.ts index 798d85f5e..c7fca46e4 100644 --- a/src/services/mapiClient.ts +++ b/src/services/mapiClient.ts @@ -65,7 +65,7 @@ export class MapiClient { const managementApiAccessToken = settings[Constants.SettingNames.managementApiAccessToken]; if (managementApiAccessToken) { - this.authenticator.setAccessToken(managementApiAccessToken); + await this.authenticator.setAccessToken(managementApiAccessToken); } else if (this.environment === "development") { console.warn(`Development mode: Please specify ${Constants.SettingNames.managementApiAccessToken}" in configuration file.`); @@ -126,7 +126,7 @@ export class MapiClient { const authHeader = httpRequest.headers.find(header => header.name === "Authorization"); if (!authHeader || !authHeader.value) { - const authToken = this.authenticator.getAccessToken(); + const authToken = await this.authenticator.getAccessToken(); if (authToken) { httpRequest.headers.push({ name: "Authorization", value: `${authToken}` }); @@ -175,8 +175,6 @@ export class MapiClient { } if (errorResponse.statusCode === 401) { - // this.authenticator.clear(); - const authHeader = errorResponse.headers.find(h => h.name.toLowerCase() === "www-authenticate"); if (authHeader && authHeader.value.indexOf("Basic") !== -1) { diff --git a/src/services/provisioningService.ts b/src/services/provisioningService.ts index 3afc174e6..b84a5fae7 100644 --- a/src/services/provisioningService.ts +++ b/src/services/provisioningService.ts @@ -50,7 +50,7 @@ export class ProvisionService { this.viewManager.removeShutter(); const dataObj = await this.fetchData(dataUrl); const keys = Object.keys(dataObj); - const accessToken = this.authenticator.getAccessToken(); + const accessToken = await this.authenticator.getAccessToken(); if (!accessToken) { this.viewManager.notifyError(`Unable to setup website`, `Management API access token is empty or invald.`); @@ -90,7 +90,8 @@ export class ProvisionService { const managementApiUrl = await this.getManagementUrl(); const managementApiVersion = await this.getManagementApiVersion(); const url = `${managementApiUrl}/contentTypes?api-version=${managementApiVersion}`; - const accessToken = this.authenticator.getAccessToken(); + const accessToken = await this.authenticator.getAccessToken(); + try { const request: HttpRequest = { url: url, diff --git a/src/services/userService.ts b/src/services/userService.ts index 052d8f8d7..c751c0404 100644 --- a/src/services/userService.ts +++ b/src/services/userService.ts @@ -12,7 +12,7 @@ export class StaticUserService implements UserService { * Returns current user's role keys. */ public async getUserRoles(): Promise { - const authenticated = this.authenticator.isAuthenticated(); + const authenticated = await this.authenticator.isAuthenticated(); if (authenticated) { return [BuiltInRoles.authenticated.key]; diff --git a/src/services/usersService.ts b/src/services/usersService.ts index da391962e..e0dc09652 100644 --- a/src/services/usersService.ts +++ b/src/services/usersService.ts @@ -34,7 +34,7 @@ export class UsersService { return userId; } else { - this.authenticator.clearAccessToken(); + await this.authenticator.clearAccessToken(); return undefined; } } @@ -63,7 +63,7 @@ export class UsersService { } const accessToken = match[1]; - this.authenticator.setAccessToken(`SharedAccessSignature ${accessToken}`); + await this.authenticator.setAccessToken(`SharedAccessSignature ${accessToken}`); } if (identity && identity.id) { @@ -79,8 +79,8 @@ export class UsersService { * Initiates signing-out with Basic identity provider. * @param withRedirect */ - public signOut(withRedirect: boolean = true): void { - this.authenticator.clearAccessToken(); + public async signOut(withRedirect: boolean = true): Promise { + await this.authenticator.clearAccessToken(); if (withRedirect) { this.navigateToSignin(); @@ -115,8 +115,8 @@ export class UsersService { /** * Checks if current user is authenticated. */ - public isUserSignedIn(): boolean { - return this.authenticator.isAuthenticated(); + public async isUserSignedIn(): Promise { + return await this.authenticator.isAuthenticated(); } /** @@ -166,7 +166,7 @@ export class UsersService { } } - + /** * Deletes specified user. @@ -183,7 +183,7 @@ export class UsersService { await this.mapiClient.delete(query, [header]); - this.signOut(); + await this.signOut(); } catch (error) { this.navigateToSignin(); @@ -221,22 +221,22 @@ export class UsersService { } public async createResetPasswordRequest(email: string): Promise { - const payload = {"to": email, "appType": "developerPortal"}; + const payload = { to: email, appType: "developerPortal" }; await this.mapiClient.post("/confirmations/password", null, payload); } public async changePassword(userId: string, newPassword: string): Promise { - const authToken = this.authenticator.getAccessToken(); + const authToken = await this.authenticator.getAccessToken(); if (!authToken) { throw Error("Auth token not found"); } - + const headers = [ { name: "Authorization", value: authToken }, { name: "If-Match", value: "*" } ]; - const payload = { "password": newPassword }; + const payload = { password: newPassword }; await this.mapiClient.patch(userId, headers, payload); } } \ No newline at end of file