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