-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
208 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { LoginAction } from 'src/models/LoginAction' | ||
import { AuthenticationResponse } from 'src/models/AuthenticationResponse' | ||
import { AuthenticationMfaResponse } from 'src/models/AuthenticationMfaResponse' | ||
|
||
import { tokenHelper } from './tokenHelper' | ||
|
||
const apiBaseUrl = process.env.NODE_ENV === 'development' ? '/api/v1/' : '/auth/api/v1/' | ||
|
||
export class AuthenticationHelper { | ||
mfaIdentifier: string | undefined | ||
|
||
constructor () { | ||
this.mfaIdentifier = undefined | ||
} | ||
|
||
public async tokenLogin (token : string) : Promise<LoginAction> { | ||
const response = await fetch(`${apiBaseUrl}Authentication/Token`, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ | ||
mfaIdentifier: this.mfaIdentifier, | ||
token | ||
}) | ||
}) | ||
|
||
if (response.status === 200) { | ||
const authenticationResponse = await response.json() as AuthenticationResponse | ||
tokenHelper.setToken(authenticationResponse) | ||
|
||
this.mfaIdentifier = undefined | ||
|
||
return LoginAction.Forward | ||
} | ||
|
||
return LoginAction.ClearPassword | ||
} | ||
|
||
public async login (emailAddress : string, password : string) : Promise<LoginAction> { | ||
const response = await fetch(`${apiBaseUrl}Authentication`, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ | ||
emailAddress, | ||
password | ||
}) | ||
}) | ||
|
||
if (response.status === 200) { | ||
const authenticationResponse = await response.json() as AuthenticationResponse | ||
tokenHelper.setToken(authenticationResponse) | ||
|
||
return LoginAction.Forward | ||
} | ||
|
||
if (response.status === 401) { | ||
const authenticationMfaResponse = await response.json() as AuthenticationMfaResponse | ||
this.mfaIdentifier = authenticationMfaResponse.mfaIdentifier | ||
|
||
return LoginAction.TimeBasedOneTimePasswordRequired | ||
} | ||
|
||
if (response.status === 404) { | ||
return LoginAction.Failure | ||
} | ||
|
||
if (response.status === 504) { | ||
return LoginAction.Failure | ||
} | ||
|
||
return LoginAction.ClearPassword | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface AuthenticationMfaResponse { | ||
mfaIdentifier: string | ||
mfaType: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export enum LoginAction { | ||
Forward, | ||
ClearPassword, | ||
Failure | ||
Failure, | ||
TimeBasedOneTimePasswordRequired | ||
} |
Oops, something went wrong.