Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. import * as AccessManagerApiClient from '@mulesoft/access-management-api-client';
  2. import { AppConfig } from '@mulesoft/anypoint-client-core';
  3.  
  4. export default class Authentication {
  5. private _username: string;
  6. private _password: string;
  7. private _baseUrl: string;
  8. private _accessManagerToken: string;
  9. private _apiManagerToken: string;
  10. private appConfig = AppConfig.setup();
  11.  
  12. constructor(baseUrl: string, username: string, password: string) {
  13. this._baseUrl = baseUrl;
  14. this._username = username;
  15. this._password = password;
  16. }
  17.  
  18. public async getApiManagerOptions(uri: string): Promise<any> {
  19. if (!this._apiManagerToken) {
  20. const authenticationResponse: any = (await this.apiManagerAuthentication());
  21. this._apiManagerToken = authenticationResponse;
  22. }
  23.  
  24. const options: any = {
  25. baseUri: `${this._baseUrl}${uri}`,
  26. headers: {
  27. 'Authorization': `bearer ${this._apiManagerToken}`,
  28. 'Content-Type': 'application/json'
  29. }
  30. };
  31.  
  32. return options;
  33. }
  34.  
  35. public async getAccessManagerOptions(uri: string): Promise<any> {
  36. if (!this._accessManagerToken) {
  37. const authenticationResponse = await this.accessManagerAuthentication();
  38. this._accessManagerToken = JSON.parse(authenticationResponse.body).access_token;
  39. }
  40.  
  41. const options: any = {
  42. baseUri: `${this._baseUrl}${uri}`,
  43. headers: {
  44. 'Authorization': `bearer ${this._accessManagerToken}`,
  45. 'Content-Type': 'application/json'
  46. }
  47. };
  48.  
  49. return options;
  50. }
  51.  
  52. private async accessManagerAuthentication(): Promise<any> {
  53. const options: any = {
  54. baseUri: `${this._baseUrl}${this.appConfig.uris.coreServices}`
  55. };
  56.  
  57. const body: any = {
  58. password: this._password,
  59. username: this._username
  60. };
  61.  
  62. const authenticationClient = new AccessManagerApiClient(options);
  63. return await authenticationClient.login.post(body);
  64. }
  65.  
  66. private async apiManagerAuthentication(): Promise<any> {
  67. const authenticationResponse = await this.accessManagerAuthentication();
  68. this._accessManagerToken = JSON.parse(authenticationResponse.body).access_token;
  69.  
  70. const options: any = {
  71. baseUri: `${this._baseUrl}${this.appConfig.uris.apiPlatform}/session`,
  72. headers: {
  73. 'Content-Type': 'application/json'
  74. }
  75. };
  76.  
  77. const body: any = {
  78. token: this._accessManagerToken
  79. };
  80.  
  81. const authenticationClient = new AccessManagerApiClient(options);
  82. // tslint:disable
  83. console.log('=======> OPTS: ', options);
  84. console.log('=======> BODY: ', body);
  85. return authenticationClient.api.session.post(body);
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement