Skip to content

Commit

Permalink
Made IAuthenticator interface asynchronous. (#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
azaslonov authored Dec 17, 2019
1 parent 6f65100 commit e101ebe
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 41 deletions.
8 changes: 4 additions & 4 deletions src/authentication/IAuthenticator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ export interface IAuthenticator {
/**
* Returns access token for current session.
*/
getAccessToken(): string;
getAccessToken(): Promise<string>;

/**
* 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<void>;

/**
* Parses specified access token.
Expand All @@ -21,10 +21,10 @@ export interface IAuthenticator {
/**
* Clears access token from current session.
*/
clearAccessToken(): void;
clearAccessToken(): Promise<void>;

/**
* Checks if current user is signed in.
*/
isAuthenticated(): boolean;
isAuthenticated(): Promise<boolean>;
}
4 changes: 2 additions & 2 deletions src/components/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand All @@ -47,7 +47,7 @@ export class App {
return;
}

this.authenticator.setAccessToken(accessToken.value);
await this.authenticator.setAccessToken(accessToken.value);
}
}
catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/content/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
10 changes: 5 additions & 5 deletions src/components/defaultAuthenticator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
return sessionStorage.getItem("accessToken");
}

public setAccessToken(accessToken: string): void {
public async setAccessToken(accessToken: string): Promise<void> {
sessionStorage.setItem("accessToken", accessToken);
}

public clearAccessToken(): void {
public async clearAccessToken(): Promise<void> {
sessionStorage.removeItem("accessToken");
}

public isAuthenticated(): boolean {
const accessToken = this.getAccessToken();
public async isAuthenticated(): Promise<boolean> {
const accessToken = await this.getAccessToken();

if (!accessToken) {
return false;
Expand Down
11 changes: 6 additions & 5 deletions src/components/staticAuthenticator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ import { IAuthenticator } from "../authentication";
export class StaticAuthenticator implements IAuthenticator {
private accessToken: string;

public getAccessToken(): string {
public async getAccessToken(): Promise<string> {
return this.accessToken;
}

public setAccessToken(token: string): void {
public async setAccessToken(token: string): Promise<void> {
this.accessToken = token;
}

public clearAccessToken(): void {
public async clearAccessToken(): Promise<void> {
this.accessToken = undefined;
}

public isAuthenticated(): boolean {
return !!this.getAccessToken();
public async isAuthenticated(): Promise<boolean> {
const accessToken = await this.getAccessToken();
return !!accessToken;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/routing/signOutRouteGuard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class SignOutRouteGuard implements RouteGuard {

public async canActivate(route: Route): Promise<boolean> {
if (route.hash === hashSignOut) {
this.authenticator.clearAccessToken();
await this.authenticator.clearAccessToken();
location.assign(pageUrlHome);
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/services/aadService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/services/backendService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class BackendService {
}

public async sendChangePassword(changePasswordRequest: ChangePasswordRequest): Promise<void> {
const authToken = this.authenticator.getAccessToken();
const authToken = await this.authenticator.getAccessToken();

if (!authToken) {
throw Error("Auth token not found");
Expand Down Expand Up @@ -107,7 +107,7 @@ export class BackendService {
}

public async getDelegationUrl(action: DelegationAction, delegationParameters: {}): Promise<string> {
const authToken = this.authenticator.getAccessToken();
const authToken = await this.authenticator.getAccessToken();

if (!authToken) {
throw Error("Auth token not found");
Expand Down
6 changes: 2 additions & 4 deletions src/services/mapiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`);
Expand Down Expand Up @@ -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}` });
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions src/services/provisioningService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`);
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/services/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class StaticUserService implements UserService {
* Returns current user's role keys.
*/
public async getUserRoles(): Promise<string[]> {
const authenticated = this.authenticator.isAuthenticated();
const authenticated = await this.authenticator.isAuthenticated();

if (authenticated) {
return [BuiltInRoles.authenticated.key];
Expand Down
24 changes: 12 additions & 12 deletions src/services/usersService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class UsersService {
return userId;
}
else {
this.authenticator.clearAccessToken();
await this.authenticator.clearAccessToken();
return undefined;
}
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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<void> {
await this.authenticator.clearAccessToken();

if (withRedirect) {
this.navigateToSignin();
Expand Down Expand Up @@ -115,8 +115,8 @@ export class UsersService {
/**
* Checks if current user is authenticated.
*/
public isUserSignedIn(): boolean {
return this.authenticator.isAuthenticated();
public async isUserSignedIn(): Promise<boolean> {
return await this.authenticator.isAuthenticated();
}

/**
Expand Down Expand Up @@ -166,7 +166,7 @@ export class UsersService {
}
}



/**
* Deletes specified user.
Expand All @@ -183,7 +183,7 @@ export class UsersService {

await this.mapiClient.delete<string>(query, [header]);

this.signOut();
await this.signOut();
}
catch (error) {
this.navigateToSignin();
Expand Down Expand Up @@ -221,22 +221,22 @@ export class UsersService {
}

public async createResetPasswordRequest(email: string): Promise<void> {
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<void> {
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);
}
}

0 comments on commit e101ebe

Please sign in to comment.