juice-shop
103 строки · 3.5 Кб
1/*
2* Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
3* SPDX-License-Identifier: MIT
4*/
5
6import { Component } from '@angular/core'
7import { UntypedFormControl, UntypedFormGroup, Validators } from '@angular/forms'
8
9import { TwoFactorAuthService } from '../Services/two-factor-auth-service'
10import { ConfigurationService } from '../Services/configuration.service'
11
12import { library } from '@fortawesome/fontawesome-svg-core'
13import { faSave, faUnlockAlt } from '@fortawesome/free-solid-svg-icons'
14
15import { forkJoin } from 'rxjs'
16import { TranslateService } from '@ngx-translate/core'
17import { MatSnackBar } from '@angular/material/snack-bar'
18import { SnackBarHelperService } from '../Services/snack-bar-helper.service'
19
20library.add(faUnlockAlt, faSave)
21
22@Component({
23selector: 'app-two-factor-auth',
24templateUrl: './two-factor-auth.component.html',
25styleUrls: ['./two-factor-auth.component.scss']
26})
27export class TwoFactorAuthComponent {
28public data?: string
29
30public twoFactorSetupForm: UntypedFormGroup = new UntypedFormGroup({
31passwordControl: new UntypedFormControl('', [Validators.required]),
32initalTokenControl: new UntypedFormControl('', [Validators.required, Validators.pattern('^[\\d]{6}$')])
33})
34
35public twoFactorDisableForm: UntypedFormGroup = new UntypedFormGroup({
36passwordControl: new UntypedFormControl('', [Validators.required])
37})
38
39public setupStatus: boolean | null = null
40public errored: boolean | null = null
41
42public totpUrl?: string
43public totpSecret?: string
44private setupToken?: string
45
46private appName = 'OWASP Juice Shop'
47
48constructor (private readonly twoFactorAuthService: TwoFactorAuthService, private readonly configurationService: ConfigurationService, private readonly snackBar: MatSnackBar, private readonly translateService: TranslateService, private readonly snackBarHelperService: SnackBarHelperService) {}
49
50ngOnInit () {
51this.updateStatus()
52}
53
54updateStatus () {
55const status = this.twoFactorAuthService.status()
56const config = this.configurationService.getApplicationConfiguration()
57
58forkJoin([status, config]).subscribe(([{ setup, email, secret, setupToken }, config]) => {
59this.setupStatus = setup
60this.appName = config.application.name
61if (!setup) {
62const encodedAppName = encodeURIComponent(this.appName)
63this.totpUrl = `otpauth://totp/${encodedAppName}:${email}?secret=${secret}&issuer=${encodedAppName}`
64this.totpSecret = secret
65this.setupToken = setupToken
66}
67}, () => {
68console.log('Failed to fetch 2fa status')
69})
70return status
71}
72
73setup () {
74this.twoFactorAuthService.setup(
75this.twoFactorSetupForm.get('passwordControl')?.value,
76this.twoFactorSetupForm.get('initalTokenControl')?.value,
77this.setupToken
78).subscribe(() => {
79this.setupStatus = true
80this.snackBarHelperService.open('CONFIRM_2FA_SETUP')
81}, () => {
82this.twoFactorSetupForm.get('passwordControl')?.markAsPristine()
83this.twoFactorSetupForm.get('initalTokenControl')?.markAsPristine()
84this.errored = true
85})
86}
87
88disable () {
89this.twoFactorAuthService.disable(
90this.twoFactorDisableForm.get('passwordControl')?.value
91).subscribe(() => {
92this.updateStatus().subscribe(
93() => {
94this.setupStatus = false
95}
96)
97this.snackBarHelperService.open('CONFIRM_2FA_DISABLE')
98}, () => {
99this.twoFactorDisableForm.get('passwordControl')?.markAsPristine()
100this.errored = true
101})
102}
103}
104