apiKeyCredentials.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License. See License.txt in the project root for license information.
  3. import { HttpHeaders } from "../httpHeaders";
  4. import { WebResource } from "../webResource";
  5. import { ServiceClientCredentials } from "./serviceClientCredentials";
  6. /**
  7. * @interface ApiKeyCredentialOptions
  8. * Describes the options to be provided while creating an instance of ApiKeyCredentials
  9. */
  10. export interface ApiKeyCredentialOptions {
  11. /**
  12. * A key value pair of the header parameters that need to be applied to the request.
  13. */
  14. inHeader?: { [x: string]: any };
  15. /**
  16. * A key value pair of the query parameters that need to be applied to the request.
  17. */
  18. inQuery?: { [x: string]: any };
  19. }
  20. /**
  21. * Authenticates to a service using an API key.
  22. */
  23. export class ApiKeyCredentials implements ServiceClientCredentials {
  24. /**
  25. * A key value pair of the header parameters that need to be applied to the request.
  26. */
  27. private readonly inHeader?: { [x: string]: any };
  28. /**
  29. * A key value pair of the query parameters that need to be applied to the request.
  30. */
  31. private readonly inQuery?: { [x: string]: any };
  32. /**
  33. * @constructor
  34. * @param {object} options Specifies the options to be provided for auth. Either header or query needs to be provided.
  35. */
  36. constructor(options: ApiKeyCredentialOptions) {
  37. if (!options || (options && !options.inHeader && !options.inQuery)) {
  38. throw new Error(`options cannot be null or undefined. Either "inHeader" or "inQuery" property of the options object needs to be provided.`);
  39. }
  40. this.inHeader = options.inHeader;
  41. this.inQuery = options.inQuery;
  42. }
  43. /**
  44. * Signs a request with the values provided in the inHeader and inQuery parameter.
  45. *
  46. * @param {WebResource} webResource The WebResource to be signed.
  47. * @returns {Promise<WebResource>} The signed request object.
  48. */
  49. signRequest(webResource: WebResource): Promise<WebResource> {
  50. if (!webResource) {
  51. return Promise.reject(new Error(`webResource cannot be null or undefined and must be of type "object".`));
  52. }
  53. if (this.inHeader) {
  54. if (!webResource.headers) {
  55. webResource.headers = new HttpHeaders();
  56. }
  57. for (const headerName in this.inHeader) {
  58. webResource.headers.set(headerName, this.inHeader[headerName]);
  59. }
  60. }
  61. if (this.inQuery) {
  62. if (!webResource.url) {
  63. return Promise.reject(new Error(`url cannot be null in the request object.`));
  64. }
  65. if (webResource.url.indexOf("?") < 0) {
  66. webResource.url += "?";
  67. }
  68. for (const key in this.inQuery) {
  69. if (!webResource.url.endsWith("?")) {
  70. webResource.url += "&";
  71. }
  72. webResource.url += `${key}=${this.inQuery[key]}`;
  73. }
  74. }
  75. return Promise.resolve(webResource);
  76. }
  77. }