juice-shop

Форк
0
/
two-factor-auth-enter.component.ts 
62 строки · 2.2 Кб
1
/*
2
 * Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
3
 * SPDX-License-Identifier: MIT
4
 */
5

6
import { Component, NgZone } from '@angular/core'
7
import { UntypedFormControl, UntypedFormGroup, Validators } from '@angular/forms'
8
import { TwoFactorAuthService } from '../Services/two-factor-auth-service'
9
import { CookieService } from 'ngx-cookie'
10
import { UserService } from '../Services/user.service'
11
import { Router } from '@angular/router'
12
import { library } from '@fortawesome/fontawesome-svg-core'
13
import { faUnlockAlt } from '@fortawesome/free-solid-svg-icons'
14

15
library.add(faUnlockAlt)
16

17
interface TokenEnterFormFields {
18
  token: string
19
}
20

21
@Component({
22
  selector: 'app-two-factor-auth-enter',
23
  templateUrl: './two-factor-auth-enter.component.html',
24
  styleUrls: ['./two-factor-auth-enter.component.scss']
25
})
26
export class TwoFactorAuthEnterComponent {
27
  public twoFactorForm: UntypedFormGroup = new UntypedFormGroup({
28
    token: new UntypedFormControl('', [Validators.minLength(6), Validators.maxLength(6), Validators.required, Validators.pattern('^[\\d]{6}$')])
29
  })
30

31
  public errored: boolean = false
32

33
  constructor (
34
    private readonly twoFactorAuthService: TwoFactorAuthService,
35
    private readonly cookieService: CookieService,
36
    private readonly userService: UserService,
37
    private readonly router: Router,
38
    private readonly ngZone: NgZone
39
  ) { }
40

41
  verify () {
42
    const fields: TokenEnterFormFields = this.twoFactorForm.value
43

44
    this.twoFactorAuthService.verify(fields.token).subscribe((authentication) => {
45
      localStorage.setItem('token', authentication.token)
46
      const expires = new Date()
47
      expires.setHours(expires.getHours() + 8)
48
      this.cookieService.put('token', authentication.token, { expires })
49
      sessionStorage.setItem('bid', authentication.bid?.toString())
50
      /* Use userService to notifiy if user has logged in */
51
      /* this.userService.isLoggedIn = true; */
52
      this.userService.isLoggedIn.next(true)
53
      this.ngZone.run(async () => await this.router.navigate(['/search']))
54
    }, (error) => {
55
      this.errored = true
56
      setTimeout(() => {
57
        this.errored = false
58
      }, 5 * 1000)
59
      return error
60
    })
61
  }
62
}
63

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.