mirror of
https://github.com/bitwarden/browser.git
synced 2024-11-24 12:06:15 +01:00
implement AuthServiceAbstraction
This commit is contained in:
parent
dff634d25e
commit
cd46f64993
@ -6,7 +6,7 @@ import { SymmetricCryptoKey } from '../models/domain/symmetricCryptoKey';
|
||||
export abstract class AuthService {
|
||||
email: string;
|
||||
masterPasswordHash: string;
|
||||
twoFactorProviders: Map<TwoFactorProviderType, { [key: string]: string; }>;
|
||||
twoFactorProvidersData: Map<TwoFactorProviderType, { [key: string]: string; }>;
|
||||
selectedTwoFactorProviderType: TwoFactorProviderType;
|
||||
|
||||
logIn: (email: string, masterPassword: string) => Promise<AuthResult>;
|
||||
|
@ -48,7 +48,7 @@ export class TwoFactorComponent implements OnInit, OnDestroy {
|
||||
|
||||
async ngOnInit() {
|
||||
if (this.authService.email == null || this.authService.masterPasswordHash == null ||
|
||||
this.authService.twoFactorProviders == null) {
|
||||
this.authService.twoFactorProvidersData == null) {
|
||||
this.router.navigate([this.loginRoute]);
|
||||
return;
|
||||
}
|
||||
@ -90,18 +90,18 @@ export class TwoFactorComponent implements OnInit, OnDestroy {
|
||||
|
||||
this.cleanupU2f();
|
||||
this.title = (TwoFactorProviders as any)[this.selectedProviderType].name;
|
||||
const params = this.authService.twoFactorProviders.get(this.selectedProviderType);
|
||||
const providerData = this.authService.twoFactorProvidersData.get(this.selectedProviderType);
|
||||
switch (this.selectedProviderType) {
|
||||
case TwoFactorProviderType.U2f:
|
||||
if (!this.u2fSupported || this.u2f == null) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (params.Challenge != null) {
|
||||
this.u2f.init(JSON.parse(params.Challenge));
|
||||
if (providerData.Challenge != null) {
|
||||
this.u2f.init(JSON.parse(providerData.Challenge));
|
||||
} else {
|
||||
// TODO: Deprecated. Remove in future version.
|
||||
const challenges = JSON.parse(params.Challenges);
|
||||
const challenges = JSON.parse(providerData.Challenges);
|
||||
if (challenges != null && challenges.length > 0) {
|
||||
this.u2f.init({
|
||||
appId: challenges[0].appId,
|
||||
@ -125,8 +125,8 @@ export class TwoFactorComponent implements OnInit, OnDestroy {
|
||||
setTimeout(() => {
|
||||
DuoWebSDK.init({
|
||||
iframe: undefined,
|
||||
host: params.Host,
|
||||
sig_request: params.Signature,
|
||||
host: providerData.Host,
|
||||
sig_request: providerData.Signature,
|
||||
submit_callback: async (f: HTMLFormElement) => {
|
||||
const sig = f.querySelector('input[name="sig_response"]') as HTMLInputElement;
|
||||
if (sig != null) {
|
||||
@ -138,8 +138,8 @@ export class TwoFactorComponent implements OnInit, OnDestroy {
|
||||
}, 0);
|
||||
break;
|
||||
case TwoFactorProviderType.Email:
|
||||
this.twoFactorEmail = params.Email;
|
||||
if (this.authService.twoFactorProviders.size > 1) {
|
||||
this.twoFactorEmail = providerData.Email;
|
||||
if (this.authService.twoFactorProvidersData.size > 1) {
|
||||
await this.sendEmail(false);
|
||||
}
|
||||
break;
|
||||
|
@ -15,6 +15,7 @@ import { IdentityTwoFactorResponse } from '../models/response/identityTwoFactorR
|
||||
|
||||
import { ApiService } from '../abstractions/api.service';
|
||||
import { AppIdService } from '../abstractions/appId.service';
|
||||
import { AuthService as AuthServiceAbstraction } from '../abstractions/auth.service';
|
||||
import { CryptoService } from '../abstractions/crypto.service';
|
||||
import { I18nService } from '../abstractions/i18n.service';
|
||||
import { MessagingService } from '../abstractions/messaging.service';
|
||||
@ -73,10 +74,10 @@ export const TwoFactorProviders = {
|
||||
},
|
||||
};
|
||||
|
||||
export class AuthService {
|
||||
export class AuthService implements AuthServiceAbstraction {
|
||||
email: string;
|
||||
masterPasswordHash: string;
|
||||
twoFactorProviders: Map<TwoFactorProviderType, { [key: string]: string; }>;
|
||||
twoFactorProvidersData: Map<TwoFactorProviderType, { [key: string]: string; }>;
|
||||
selectedTwoFactorProviderType: TwoFactorProviderType = null;
|
||||
|
||||
private key: SymmetricCryptoKey;
|
||||
@ -139,32 +140,32 @@ export class AuthService {
|
||||
|
||||
getSupportedTwoFactorProviders(win: Window): any[] {
|
||||
const providers: any[] = [];
|
||||
if (this.twoFactorProviders == null) {
|
||||
if (this.twoFactorProvidersData == null) {
|
||||
return providers;
|
||||
}
|
||||
|
||||
if (this.twoFactorProviders.has(TwoFactorProviderType.OrganizationDuo) &&
|
||||
if (this.twoFactorProvidersData.has(TwoFactorProviderType.OrganizationDuo) &&
|
||||
this.platformUtilsService.supportsDuo()) {
|
||||
providers.push(TwoFactorProviders[TwoFactorProviderType.OrganizationDuo]);
|
||||
}
|
||||
|
||||
if (this.twoFactorProviders.has(TwoFactorProviderType.Authenticator)) {
|
||||
if (this.twoFactorProvidersData.has(TwoFactorProviderType.Authenticator)) {
|
||||
providers.push(TwoFactorProviders[TwoFactorProviderType.Authenticator]);
|
||||
}
|
||||
|
||||
if (this.twoFactorProviders.has(TwoFactorProviderType.Yubikey)) {
|
||||
if (this.twoFactorProvidersData.has(TwoFactorProviderType.Yubikey)) {
|
||||
providers.push(TwoFactorProviders[TwoFactorProviderType.Yubikey]);
|
||||
}
|
||||
|
||||
if (this.twoFactorProviders.has(TwoFactorProviderType.Duo) && this.platformUtilsService.supportsDuo()) {
|
||||
if (this.twoFactorProvidersData.has(TwoFactorProviderType.Duo) && this.platformUtilsService.supportsDuo()) {
|
||||
providers.push(TwoFactorProviders[TwoFactorProviderType.Duo]);
|
||||
}
|
||||
|
||||
if (this.twoFactorProviders.has(TwoFactorProviderType.U2f) && this.platformUtilsService.supportsU2f(win)) {
|
||||
if (this.twoFactorProvidersData.has(TwoFactorProviderType.U2f) && this.platformUtilsService.supportsU2f(win)) {
|
||||
providers.push(TwoFactorProviders[TwoFactorProviderType.U2f]);
|
||||
}
|
||||
|
||||
if (this.twoFactorProviders.has(TwoFactorProviderType.Email)) {
|
||||
if (this.twoFactorProvidersData.has(TwoFactorProviderType.Email)) {
|
||||
providers.push(TwoFactorProviders[TwoFactorProviderType.Email]);
|
||||
}
|
||||
|
||||
@ -172,18 +173,18 @@ export class AuthService {
|
||||
}
|
||||
|
||||
getDefaultTwoFactorProvider(u2fSupported: boolean): TwoFactorProviderType {
|
||||
if (this.twoFactorProviders == null) {
|
||||
if (this.twoFactorProvidersData == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (this.selectedTwoFactorProviderType != null &&
|
||||
this.twoFactorProviders.has(this.selectedTwoFactorProviderType)) {
|
||||
this.twoFactorProvidersData.has(this.selectedTwoFactorProviderType)) {
|
||||
return this.selectedTwoFactorProviderType;
|
||||
}
|
||||
|
||||
let providerType: TwoFactorProviderType = null;
|
||||
let providerPriority = -1;
|
||||
this.twoFactorProviders.forEach((value, type) => {
|
||||
this.twoFactorProvidersData.forEach((value, type) => {
|
||||
const provider = (TwoFactorProviders as any)[type];
|
||||
if (provider != null && provider.priority > providerPriority) {
|
||||
if (type === TwoFactorProviderType.U2f && !u2fSupported) {
|
||||
@ -245,7 +246,7 @@ export class AuthService {
|
||||
this.email = email;
|
||||
this.masterPasswordHash = hashedPassword;
|
||||
this.key = this.setCryptoKeys ? key : null;
|
||||
this.twoFactorProviders = twoFactorResponse.twoFactorProviders2;
|
||||
this.twoFactorProvidersData = twoFactorResponse.twoFactorProviders2;
|
||||
result.twoFactorProviders = twoFactorResponse.twoFactorProviders2;
|
||||
return result;
|
||||
}
|
||||
@ -285,7 +286,7 @@ export class AuthService {
|
||||
private clearState(): void {
|
||||
this.email = null;
|
||||
this.masterPasswordHash = null;
|
||||
this.twoFactorProviders = null;
|
||||
this.twoFactorProvidersData = null;
|
||||
this.selectedTwoFactorProviderType = null;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user