Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import * as AccessManagerApiClient from '@mulesoft/access-management-api-client';
- import { AppConfig } from '@mulesoft/anypoint-client-core';
- export default class Authentication {
- private _username: string;
- private _password: string;
- private _baseUrl: string;
- private _accessManagerToken: string;
- private _apiManagerToken: string;
- private appConfig = AppConfig.setup();
- constructor(baseUrl: string, username: string, password: string) {
- this._baseUrl = baseUrl;
- this._username = username;
- this._password = password;
- }
- public async getApiManagerOptions(uri: string): Promise<any> {
- if (!this._apiManagerToken) {
- const authenticationResponse: any = (await this.apiManagerAuthentication());
- this._apiManagerToken = authenticationResponse;
- }
- const options: any = {
- baseUri: `${this._baseUrl}${uri}`,
- headers: {
- 'Authorization': `bearer ${this._apiManagerToken}`,
- 'Content-Type': 'application/json'
- }
- };
- return options;
- }
- public async getAccessManagerOptions(uri: string): Promise<any> {
- if (!this._accessManagerToken) {
- const authenticationResponse = await this.accessManagerAuthentication();
- this._accessManagerToken = JSON.parse(authenticationResponse.body).access_token;
- }
- const options: any = {
- baseUri: `${this._baseUrl}${uri}`,
- headers: {
- 'Authorization': `bearer ${this._accessManagerToken}`,
- 'Content-Type': 'application/json'
- }
- };
- return options;
- }
- private async accessManagerAuthentication(): Promise<any> {
- const options: any = {
- baseUri: `${this._baseUrl}${this.appConfig.uris.coreServices}`
- };
- const body: any = {
- password: this._password,
- username: this._username
- };
- const authenticationClient = new AccessManagerApiClient(options);
- return await authenticationClient.login.post(body);
- }
- private async apiManagerAuthentication(): Promise<any> {
- const authenticationResponse = await this.accessManagerAuthentication();
- this._accessManagerToken = JSON.parse(authenticationResponse.body).access_token;
- const options: any = {
- baseUri: `${this._baseUrl}${this.appConfig.uris.apiPlatform}/session`,
- headers: {
- 'Content-Type': 'application/json'
- }
- };
- const body: any = {
- token: this._accessManagerToken
- };
- const authenticationClient = new AccessManagerApiClient(options);
- // tslint:disable
- console.log('=======> OPTS: ', options);
- console.log('=======> BODY: ', body);
- return authenticationClient.api.session.post(body);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement