2019-03-07 17:18:45 +01:00
|
|
|
import { Component } from '@angular/core';
|
|
|
|
|
|
|
|
import { ToasterService } from 'angular2-toaster';
|
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { CryptoService } from 'jslib-common/abstractions/crypto.service';
|
|
|
|
import { I18nService } from 'jslib-common/abstractions/i18n.service';
|
2021-10-20 18:30:04 +02:00
|
|
|
import { LogService } from 'jslib-common/abstractions/log.service';
|
2019-03-07 17:18:45 +01:00
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { PasswordVerificationRequest } from 'jslib-common/models/request/passwordVerificationRequest';
|
2019-03-07 17:18:45 +01:00
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { ApiKeyResponse } from 'jslib-common/models/response/apiKeyResponse';
|
2019-03-07 17:18:45 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-api-key',
|
|
|
|
templateUrl: 'api-key.component.html',
|
|
|
|
})
|
|
|
|
export class ApiKeyComponent {
|
2020-11-10 22:13:42 +01:00
|
|
|
keyType: string;
|
|
|
|
isRotation: boolean;
|
|
|
|
postKey: (entityId: string, request: PasswordVerificationRequest) => Promise<ApiKeyResponse>;
|
|
|
|
entityId: string;
|
|
|
|
scope: string;
|
|
|
|
grantType: string;
|
|
|
|
apiKeyTitle: string;
|
|
|
|
apiKeyWarning: string;
|
|
|
|
apiKeyDescription: string;
|
2019-03-07 17:18:45 +01:00
|
|
|
|
|
|
|
masterPassword: string;
|
|
|
|
formPromise: Promise<ApiKeyResponse>;
|
|
|
|
clientId: string;
|
|
|
|
clientSecret: string;
|
|
|
|
|
2021-04-14 23:43:40 +02:00
|
|
|
constructor(private i18nService: I18nService, private toasterService: ToasterService,
|
2021-10-20 18:30:04 +02:00
|
|
|
private cryptoService: CryptoService, private logService: LogService) { }
|
2019-03-07 17:18:45 +01:00
|
|
|
|
|
|
|
async submit() {
|
|
|
|
if (this.masterPassword == null || this.masterPassword === '') {
|
|
|
|
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
|
|
|
|
this.i18nService.t('masterPassRequired'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const request = new PasswordVerificationRequest();
|
|
|
|
request.masterPasswordHash = await this.cryptoService.hashPassword(this.masterPassword, null);
|
|
|
|
try {
|
2020-11-10 22:13:42 +01:00
|
|
|
this.formPromise = this.postKey(this.entityId, request);
|
2019-03-07 17:18:45 +01:00
|
|
|
const response = await this.formPromise;
|
|
|
|
this.clientSecret = response.apiKey;
|
2020-11-10 22:13:42 +01:00
|
|
|
this.clientId = `${this.keyType}.${this.entityId}`;
|
2021-10-20 18:30:04 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
|
|
|
}
|
2019-03-07 17:18:45 +01:00
|
|
|
}
|
|
|
|
}
|