20 changed files with 932 additions and 27 deletions
@ -0,0 +1,104 @@ |
|||
<!-- |
|||
|
|||
Copyright © 2016-2022 The Thingsboard Authors |
|||
|
|||
Licensed under the Apache License, Version 2.0 (the "License"); |
|||
you may not use this file except in compliance with the License. |
|||
You may obtain a copy of the License at |
|||
|
|||
http://www.apache.org/licenses/LICENSE-2.0 |
|||
|
|||
Unless required by applicable law or agreed to in writing, software |
|||
distributed under the License is distributed on an "AS IS" BASIS, |
|||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
See the License for the specific language governing permissions and |
|||
limitations under the License. |
|||
|
|||
--> |
|||
<mat-toolbar fxLayout="row" color="primary"> |
|||
<h2>Enable email authenticator</h2> |
|||
<span fxFlex></span> |
|||
<button mat-icon-button |
|||
(click)="closeDialog()" |
|||
type="button"> |
|||
<mat-icon class="material-icons">close</mat-icon> |
|||
</button> |
|||
</mat-toolbar> |
|||
<mat-progress-bar color="warn" mode="indeterminate" *ngIf="isLoading$ | async"> |
|||
</mat-progress-bar> |
|||
<div style="height: 4px;" *ngIf="!(isLoading$ | async)"></div> |
|||
<div mat-dialog-content style="padding: 0"> |
|||
<mat-stepper labelPosition="bottom" linear #stepper> |
|||
<ng-template matStepperIcon="edit"> |
|||
<mat-icon>done</mat-icon> |
|||
</ng-template> |
|||
<mat-step [stepControl]="emailConfigForm"> |
|||
<ng-template matStepLabel>Add email</ng-template> |
|||
<form [formGroup]="emailConfigForm" style="display: block"> |
|||
<mat-form-field class="mat-block" appearance="fill" style="max-width: 250px;"> |
|||
<mat-label translate>user.email</mat-label> |
|||
<input matInput formControlName="email" required/> |
|||
<mat-error *ngIf="emailConfigForm.get('email').hasError('required')"> |
|||
{{ 'user.email-required' | translate }} |
|||
</mat-error> |
|||
<mat-error *ngIf="emailConfigForm.get('email').hasError('email')"> |
|||
{{ 'user.invalid-email-format' | translate }} |
|||
</mat-error> |
|||
</mat-form-field> |
|||
<div fxLayout="row" fxLayoutAlign="end center"> |
|||
<button mat-button |
|||
type="button" |
|||
[mat-dialog-close]="false"> |
|||
{{ 'action.cancel' | translate }} |
|||
</button> |
|||
<button mat-raised-button |
|||
type="button" |
|||
color="primary" |
|||
[disabled]="(isLoading$ | async) || emailConfigForm.invalid" |
|||
(click)="nextStep()"> |
|||
Send code |
|||
</button> |
|||
</div> |
|||
</form> |
|||
</mat-step> |
|||
<mat-step [stepControl]="emailVerificationForm"> |
|||
<ng-template matStepLabel>Authentication verification</ng-template> |
|||
<form [formGroup]="emailVerificationForm" style="display: block"> |
|||
<p class="mat-body-strong">Enter the 6-digit code here</p> |
|||
<p class="mat-body" style="max-width: 480px;">Enter the 6 digit verification code we just sent to {{ emailConfigForm.get('email').value }}.</p> |
|||
<mat-form-field class="mat-block" appearance="fill" style="max-width: 200px;"> |
|||
<mat-label>6-digit code</mat-label> |
|||
<input matInput formControlName="verificationCode" required maxlength="6" type="text" pattern="\d*"> |
|||
</mat-form-field> |
|||
<div fxLayout="row" fxLayoutAlign="end center"> |
|||
<button mat-button |
|||
type="button" |
|||
[mat-dialog-close]="false"> |
|||
{{ 'action.cancel' | translate }} |
|||
</button> |
|||
<button mat-raised-button |
|||
type="button" |
|||
color="primary" |
|||
[disabled]="(isLoading$ | async) || emailVerificationForm.invalid" |
|||
(click)="nextStep()"> |
|||
Activate |
|||
</button> |
|||
</div> |
|||
</form> |
|||
</mat-step> |
|||
<mat-step> |
|||
<ng-template matStepLabel matStepperIcon="done">Two-factor authentication activated</ng-template> |
|||
<div> |
|||
<h2 class="mat-h2">Email authentication enabled</h2> |
|||
<p class="mat-body" style="max-width: 480px;">If we notice an attempted login from a device or browser we don’t recognize, you will be prompted to enter the security code that will be sent to your email address.</p> |
|||
<div fxLayout="row" fxLayoutAlign="end center"> |
|||
<button mat-button |
|||
type="button" |
|||
[mat-dialog-close]="true"> |
|||
Done |
|||
</button> |
|||
</div> |
|||
</div> |
|||
</mat-step> |
|||
</mat-stepper> |
|||
</div> |
|||
@ -0,0 +1,15 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0 |
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
@ -0,0 +1,93 @@ |
|||
///
|
|||
/// Copyright © 2016-2022 The Thingsboard Authors
|
|||
///
|
|||
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|||
/// you may not use this file except in compliance with the License.
|
|||
/// You may obtain a copy of the License at
|
|||
///
|
|||
/// http://www.apache.org/licenses/LICENSE-2.0
|
|||
///
|
|||
/// Unless required by applicable law or agreed to in writing, software
|
|||
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|||
/// See the License for the specific language governing permissions and
|
|||
/// limitations under the License.
|
|||
///
|
|||
|
|||
import { Component, Inject, ViewChild } from '@angular/core'; |
|||
import { DialogComponent } from '@shared/components/dialog.component'; |
|||
import { Store } from '@ngrx/store'; |
|||
import { AppState } from '@core/core.state'; |
|||
import { Router } from '@angular/router'; |
|||
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; |
|||
import { FormBuilder, FormGroup, Validators } from '@angular/forms'; |
|||
import { TwoFactorAuthenticationService } from '@core/http/two-factor-authentication.service'; |
|||
import { TwoFactorAuthAccountConfig, TwoFactorAuthProviderType } from '@shared/models/two-factor-auth.models'; |
|||
import { MatStepper } from '@angular/material/stepper'; |
|||
|
|||
export interface EmailAuthDialogData { |
|||
email: string; |
|||
} |
|||
|
|||
@Component({ |
|||
selector: 'tb-email-auth-dialog', |
|||
templateUrl: './email-auth-dialog.component.html', |
|||
styleUrls: ['./email-auth-dialog.component.scss'] |
|||
}) |
|||
export class EmailAuthDialogComponent extends DialogComponent<EmailAuthDialogComponent> { |
|||
|
|||
private authAccountConfig: TwoFactorAuthAccountConfig; |
|||
|
|||
emailConfigForm: FormGroup; |
|||
emailVerificationForm: FormGroup; |
|||
|
|||
@ViewChild('stepper', {static: false}) stepper: MatStepper; |
|||
|
|||
constructor(protected store: Store<AppState>, |
|||
protected router: Router, |
|||
private twoFaService: TwoFactorAuthenticationService, |
|||
@Inject(MAT_DIALOG_DATA) public data: EmailAuthDialogData, |
|||
public dialogRef: MatDialogRef<EmailAuthDialogComponent>, |
|||
public fb: FormBuilder) { |
|||
super(store, router, dialogRef); |
|||
|
|||
this.emailConfigForm = this.fb.group({ |
|||
email: [this.data.email, [Validators.required, Validators.email]] |
|||
}); |
|||
|
|||
this.emailVerificationForm = this.fb.group({ |
|||
verificationCode: ['', [ |
|||
Validators.required, |
|||
Validators.minLength(6), |
|||
Validators.maxLength(6), |
|||
Validators.pattern(/^\d*$/) |
|||
]] |
|||
}); |
|||
} |
|||
|
|||
nextStep() { |
|||
switch (this.stepper.selectedIndex) { |
|||
case 0: |
|||
this.authAccountConfig = { |
|||
providerType: TwoFactorAuthProviderType.EMAIL, |
|||
useByDefault: true, |
|||
email: this.emailConfigForm.get('email').value as string |
|||
}; |
|||
this.twoFaService.submitTwoFaAccountConfig(this.authAccountConfig).subscribe(() => { |
|||
this.stepper.next(); |
|||
}); |
|||
break; |
|||
case 1: |
|||
this.twoFaService.verifyAndSaveTwoFaAccountConfig(this.authAccountConfig, |
|||
this.emailVerificationForm.get('verificationCode').value).subscribe(() => { |
|||
this.stepper.next(); |
|||
}); |
|||
break; |
|||
} |
|||
} |
|||
|
|||
closeDialog() { |
|||
return this.dialogRef.close(this.stepper.selectedIndex > 1); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,105 @@ |
|||
<!-- |
|||
|
|||
Copyright © 2016-2022 The Thingsboard Authors |
|||
|
|||
Licensed under the Apache License, Version 2.0 (the "License"); |
|||
you may not use this file except in compliance with the License. |
|||
You may obtain a copy of the License at |
|||
|
|||
http://www.apache.org/licenses/LICENSE-2.0 |
|||
|
|||
Unless required by applicable law or agreed to in writing, software |
|||
distributed under the License is distributed on an "AS IS" BASIS, |
|||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
See the License for the specific language governing permissions and |
|||
limitations under the License. |
|||
|
|||
--> |
|||
<mat-toolbar fxLayout="row" color="primary"> |
|||
<h2>Enable SMS authenticator</h2> |
|||
<span fxFlex></span> |
|||
<button mat-icon-button |
|||
(click)="closeDialog()" |
|||
type="button"> |
|||
<mat-icon class="material-icons">close</mat-icon> |
|||
</button> |
|||
</mat-toolbar> |
|||
<mat-progress-bar color="warn" mode="indeterminate" *ngIf="isLoading$ | async"> |
|||
</mat-progress-bar> |
|||
<div style="height: 4px;" *ngIf="!(isLoading$ | async)"></div> |
|||
<div mat-dialog-content style="padding: 0"> |
|||
<mat-stepper labelPosition="bottom" linear #stepper> |
|||
<ng-template matStepperIcon="edit"> |
|||
<mat-icon>done</mat-icon> |
|||
</ng-template> |
|||
<mat-step [stepControl]="smsConfigForm"> |
|||
<ng-template matStepLabel>Add phone number</ng-template> |
|||
<form [formGroup]="smsConfigForm" style="display: block"> |
|||
<p class="mat-body-1">Enter the phone number including the area code.</p> |
|||
<mat-form-field class="mat-block" appearance="fill" style="max-width: 250px;"> |
|||
<mat-label>Phone number</mat-label> |
|||
<input type="tel" required [pattern]="phoneNumberPattern" matInput formControlName="phone"> |
|||
<mat-error *ngIf="smsConfigForm.get('phone').hasError('required')"> |
|||
{{ 'admin.number-to-required' | translate }} |
|||
</mat-error> |
|||
<mat-error *ngIf="smsConfigForm.get('phone').hasError('pattern')"> |
|||
{{ 'admin.phone-number-pattern' | translate }} |
|||
</mat-error> |
|||
</mat-form-field> |
|||
<div fxLayout="row" fxLayoutAlign="end center"> |
|||
<button mat-button |
|||
type="button" |
|||
[mat-dialog-close]="false"> |
|||
{{ 'action.cancel' | translate }} |
|||
</button> |
|||
<button mat-raised-button |
|||
type="button" |
|||
color="primary" |
|||
[disabled]="(isLoading$ | async) || smsConfigForm.invalid" |
|||
(click)="nextStep()"> |
|||
Send code |
|||
</button> |
|||
</div> |
|||
</form> |
|||
</mat-step> |
|||
<mat-step [stepControl]="smsVerificationForm"> |
|||
<ng-template matStepLabel>Authentication verification</ng-template> |
|||
<form [formGroup]="smsVerificationForm" style="display: block"> |
|||
<p class="mat-body-strong">Enter the 6-digit code here</p> |
|||
<p class="mat-body" style="max-width: 480px;">Enter the 6 digit verification code we just sent to {{ smsConfigForm.get('phone').value }}.</p> |
|||
<mat-form-field class="mat-block" appearance="fill" style="max-width: 200px;"> |
|||
<mat-label>6-digit code</mat-label> |
|||
<input matInput formControlName="verificationCode" required maxlength="6" type="text" pattern="\d*"> |
|||
</mat-form-field> |
|||
<div fxLayout="row" fxLayoutAlign="end center"> |
|||
<button mat-button |
|||
type="button" |
|||
[mat-dialog-close]="false"> |
|||
{{ 'action.cancel' | translate }} |
|||
</button> |
|||
<button mat-raised-button |
|||
type="button" |
|||
color="primary" |
|||
[disabled]="(isLoading$ | async) || smsVerificationForm.invalid" |
|||
(click)="nextStep()"> |
|||
Activate |
|||
</button> |
|||
</div> |
|||
</form> |
|||
</mat-step> |
|||
<mat-step> |
|||
<ng-template matStepLabel matStepperIcon="done">Two-factor authentication activated</ng-template> |
|||
<div> |
|||
<h2 class="mat-h2">SMS authentication enabled</h2> |
|||
<p class="mat-body" style="max-width: 480px;">If we notice an attempted login from a device or browser we don’t recognize, you will be prompted to enter the security code that will be sent to the phone number.</p> |
|||
<div fxLayout="row" fxLayoutAlign="end center"> |
|||
<button mat-button |
|||
type="button" |
|||
[mat-dialog-close]="true"> |
|||
Done |
|||
</button> |
|||
</div> |
|||
</div> |
|||
</mat-step> |
|||
</mat-stepper> |
|||
</div> |
|||
@ -0,0 +1,15 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0 |
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
@ -0,0 +1,91 @@ |
|||
///
|
|||
/// Copyright © 2016-2022 The Thingsboard Authors
|
|||
///
|
|||
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|||
/// you may not use this file except in compliance with the License.
|
|||
/// You may obtain a copy of the License at
|
|||
///
|
|||
/// http://www.apache.org/licenses/LICENSE-2.0
|
|||
///
|
|||
/// Unless required by applicable law or agreed to in writing, software
|
|||
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|||
/// See the License for the specific language governing permissions and
|
|||
/// limitations under the License.
|
|||
///
|
|||
|
|||
import { Component, ViewChild } from '@angular/core'; |
|||
import { DialogComponent } from '@shared/components/dialog.component'; |
|||
import { Store } from '@ngrx/store'; |
|||
import { AppState } from '@core/core.state'; |
|||
import { Router } from '@angular/router'; |
|||
import { MatDialogRef } from '@angular/material/dialog'; |
|||
import { FormBuilder, FormGroup, Validators } from '@angular/forms'; |
|||
import { TwoFactorAuthenticationService } from '@core/http/two-factor-authentication.service'; |
|||
import { TwoFactorAuthAccountConfig, TwoFactorAuthProviderType } from '@shared/models/two-factor-auth.models'; |
|||
import { phoneNumberPattern } from '@shared/models/settings.models'; |
|||
import { MatStepper } from '@angular/material/stepper'; |
|||
|
|||
@Component({ |
|||
selector: 'tb-sms-auth-dialog', |
|||
templateUrl: './sms-auth-dialog.component.html', |
|||
styleUrls: ['./sms-auth-dialog.component.scss'] |
|||
}) |
|||
export class SMSAuthDialogComponent extends DialogComponent<SMSAuthDialogComponent> { |
|||
|
|||
private authAccountConfig: TwoFactorAuthAccountConfig; |
|||
|
|||
phoneNumberPattern = phoneNumberPattern; |
|||
|
|||
smsConfigForm: FormGroup; |
|||
smsVerificationForm: FormGroup; |
|||
|
|||
@ViewChild('stepper', {static: false}) stepper: MatStepper; |
|||
|
|||
constructor(protected store: Store<AppState>, |
|||
protected router: Router, |
|||
private twoFaService: TwoFactorAuthenticationService, |
|||
public dialogRef: MatDialogRef<SMSAuthDialogComponent>, |
|||
public fb: FormBuilder) { |
|||
super(store, router, dialogRef); |
|||
|
|||
this.smsConfigForm = this.fb.group({ |
|||
phone: ['', [Validators.required, Validators.pattern(phoneNumberPattern)]] |
|||
}); |
|||
|
|||
this.smsVerificationForm = this.fb.group({ |
|||
verificationCode: ['', [ |
|||
Validators.required, |
|||
Validators.minLength(6), |
|||
Validators.maxLength(6), |
|||
Validators.pattern(/^\d*$/) |
|||
]] |
|||
}); |
|||
} |
|||
|
|||
nextStep() { |
|||
switch (this.stepper.selectedIndex) { |
|||
case 0: |
|||
this.authAccountConfig = { |
|||
providerType: TwoFactorAuthProviderType.SMS, |
|||
useByDefault: true, |
|||
phoneNumber: this.smsConfigForm.get('phone').value as string |
|||
}; |
|||
this.twoFaService.submitTwoFaAccountConfig(this.authAccountConfig).subscribe(() => { |
|||
this.stepper.next(); |
|||
}); |
|||
break; |
|||
case 1: |
|||
this.twoFaService.verifyAndSaveTwoFaAccountConfig(this.authAccountConfig, |
|||
this.smsVerificationForm.get('verificationCode').value).subscribe(() => { |
|||
this.stepper.next(); |
|||
}); |
|||
break; |
|||
} |
|||
} |
|||
|
|||
closeDialog() { |
|||
return this.dialogRef.close(this.stepper.selectedIndex > 1); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,97 @@ |
|||
<!-- |
|||
|
|||
Copyright © 2016-2022 The Thingsboard Authors |
|||
|
|||
Licensed under the Apache License, Version 2.0 (the "License"); |
|||
you may not use this file except in compliance with the License. |
|||
You may obtain a copy of the License at |
|||
|
|||
http://www.apache.org/licenses/LICENSE-2.0 |
|||
|
|||
Unless required by applicable law or agreed to in writing, software |
|||
distributed under the License is distributed on an "AS IS" BASIS, |
|||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
See the License for the specific language governing permissions and |
|||
limitations under the License. |
|||
|
|||
--> |
|||
<mat-toolbar fxLayout="row" color="primary"> |
|||
<h2>Enable authenticator app</h2> |
|||
<span fxFlex></span> |
|||
<button mat-icon-button |
|||
(click)="closeDialog()" |
|||
type="button"> |
|||
<mat-icon class="material-icons">close</mat-icon> |
|||
</button> |
|||
</mat-toolbar> |
|||
<mat-progress-bar color="warn" mode="indeterminate" *ngIf="isLoading$ | async"> |
|||
</mat-progress-bar> |
|||
<div style="height: 4px;" *ngIf="!(isLoading$ | async)"></div> |
|||
<div mat-dialog-content style="padding: 0"> |
|||
<mat-stepper labelPosition="bottom" linear #stepper> |
|||
<ng-template matStepperIcon="edit"> |
|||
<mat-icon>done</mat-icon> |
|||
</ng-template> |
|||
<mat-step> |
|||
<ng-template matStepLabel>Get app</ng-template> |
|||
<div> |
|||
<p class="mat-body-1">You'll need to use a verification app such as Google Authenticator, Authy, or Duo. Install from your app store</p> |
|||
<div fxLayout="row" fxLayoutAlign="end center"> |
|||
<button mat-button |
|||
type="button" |
|||
[mat-dialog-close]="false"> |
|||
{{ 'action.cancel' | translate }} |
|||
</button> |
|||
<button mat-raised-button |
|||
type="button" |
|||
color="primary" |
|||
matStepperNext> |
|||
Next |
|||
</button> |
|||
</div> |
|||
</div> |
|||
</mat-step> |
|||
<mat-step [stepControl]="totpConfigForm"> |
|||
<ng-template matStepLabel>Authentication verification</ng-template> |
|||
<form [formGroup]="totpConfigForm" style="display: block"> |
|||
<p class="mat-body-strong">1. Scan this QR code with your verification app</p> |
|||
<p class="mat-body" style="margin-bottom: 8px">Once your app reads the QR code, you'll get a 6-digit code.</p> |
|||
<canvas fxFlex #canvas [ngStyle]="{display: totpAuthURL ? 'block' : 'none'}"></canvas> |
|||
<p class="mat-body-strong">2. Enter the 6-digit code here</p> |
|||
<p class="mat-body" style="max-width: 480px;">Enter the code from the app below. Once connected, we'll remember your phone so you can use it each time you log in.</p> |
|||
<mat-form-field class="mat-block" appearance="fill" style="max-width: 200px;"> |
|||
<mat-label>6-digit code</mat-label> |
|||
<input matInput formControlName="verificationCode" required maxlength="6" type="text" pattern="\d*"> |
|||
</mat-form-field> |
|||
<div fxLayout="row" fxLayoutAlign="end center"> |
|||
<button mat-button |
|||
type="button" |
|||
[mat-dialog-close]="false"> |
|||
{{ 'action.cancel' | translate }} |
|||
</button> |
|||
<button mat-raised-button |
|||
type="button" |
|||
color="primary" |
|||
[disabled]="(isLoading$ | async) || totpConfigForm.invalid" |
|||
(click)="onSaveConfig()"> |
|||
Next |
|||
</button> |
|||
</div> |
|||
</form> |
|||
</mat-step> |
|||
<mat-step> |
|||
<ng-template matStepLabel matStepperIcon="done">Two-factor authentication activated</ng-template> |
|||
<div> |
|||
<h2 class="mat-h2">Success</h2> |
|||
<p class="mat-body" style="max-width: 480px;">The next time you login from an unrecognized browser or device, you will need to provide a two-factor authentication code.</p> |
|||
<div fxLayout="row" fxLayoutAlign="end center"> |
|||
<button mat-button |
|||
type="button" |
|||
[mat-dialog-close]="true"> |
|||
Done |
|||
</button> |
|||
</div> |
|||
</div> |
|||
</mat-step> |
|||
</mat-stepper> |
|||
</div> |
|||
@ -0,0 +1,15 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0 |
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
@ -0,0 +1,80 @@ |
|||
///
|
|||
/// Copyright © 2016-2022 The Thingsboard Authors
|
|||
///
|
|||
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|||
/// you may not use this file except in compliance with the License.
|
|||
/// You may obtain a copy of the License at
|
|||
///
|
|||
/// http://www.apache.org/licenses/LICENSE-2.0
|
|||
///
|
|||
/// Unless required by applicable law or agreed to in writing, software
|
|||
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|||
/// See the License for the specific language governing permissions and
|
|||
/// limitations under the License.
|
|||
///
|
|||
|
|||
import { Component, ElementRef, ViewChild } from '@angular/core'; |
|||
import { DialogComponent } from '@shared/components/dialog.component'; |
|||
import { Store } from '@ngrx/store'; |
|||
import { AppState } from '@core/core.state'; |
|||
import { Router } from '@angular/router'; |
|||
import { MatDialogRef } from '@angular/material/dialog'; |
|||
import { FormBuilder, FormGroup, Validators } from '@angular/forms'; |
|||
import { TwoFactorAuthenticationService } from '@core/http/two-factor-authentication.service'; |
|||
import { TotpTwoFactorAuthAccountConfig, TwoFactorAuthProviderType } from '@shared/models/two-factor-auth.models'; |
|||
import { MatStepper } from '@angular/material/stepper'; |
|||
|
|||
@Component({ |
|||
selector: 'tb-totp-auth-dialog', |
|||
templateUrl: './totp-auth-dialog.component.html', |
|||
styleUrls: ['./totp-auth-dialog.component.scss'] |
|||
}) |
|||
export class TotpAuthDialogComponent extends DialogComponent<TotpAuthDialogComponent> { |
|||
|
|||
private authAccountConfig: TotpTwoFactorAuthAccountConfig; |
|||
|
|||
totpConfigForm: FormGroup; |
|||
totpAuthURL: string; |
|||
|
|||
@ViewChild('stepper', {static: false}) stepper: MatStepper; |
|||
@ViewChild('canvas', {static: false}) canvasRef: ElementRef<HTMLCanvasElement>; |
|||
|
|||
constructor(protected store: Store<AppState>, |
|||
protected router: Router, |
|||
private twoFaService: TwoFactorAuthenticationService, |
|||
public dialogRef: MatDialogRef<TotpAuthDialogComponent>, |
|||
public fb: FormBuilder) { |
|||
super(store, router, dialogRef); |
|||
this.twoFaService.generateTwoFaAccountConfig(TwoFactorAuthProviderType.TOTP).subscribe(accountConfig => { |
|||
this.authAccountConfig = accountConfig as TotpTwoFactorAuthAccountConfig; |
|||
this.totpAuthURL = this.authAccountConfig.authUrl; |
|||
this.authAccountConfig.useByDefault = true; |
|||
import('qrcode').then((QRCode) => { |
|||
QRCode.toCanvas(this.canvasRef.nativeElement, this.totpAuthURL); |
|||
this.canvasRef.nativeElement.style.width = 'auto'; |
|||
this.canvasRef.nativeElement.style.height = 'auto'; |
|||
}); |
|||
}); |
|||
this.totpConfigForm = this.fb.group({ |
|||
verificationCode: ['', [ |
|||
Validators.required, |
|||
Validators.minLength(6), |
|||
Validators.maxLength(6), |
|||
Validators.pattern(/^\d*$/) |
|||
]] |
|||
}); |
|||
} |
|||
|
|||
onSaveConfig() { |
|||
this.twoFaService.verifyAndSaveTwoFaAccountConfig(this.authAccountConfig, |
|||
this.totpConfigForm.get('verificationCode').value).subscribe((res) => { |
|||
this.stepper.next(); |
|||
}); |
|||
} |
|||
|
|||
closeDialog() { |
|||
return this.dialogRef.close(this.stepper.selectedIndex > 1); |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue