signer.d.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import * as http from 'http';
  2. import { Storage } from './storage.js';
  3. import { GoogleAuth } from 'google-auth-library';
  4. type GoogleAuthLike = Pick<GoogleAuth, 'getCredentials' | 'sign'>;
  5. /**
  6. * @deprecated Use {@link GoogleAuth} instead
  7. */
  8. export interface AuthClient {
  9. sign(blobToSign: string): Promise<string>;
  10. getCredentials(): Promise<{
  11. client_email?: string;
  12. }>;
  13. }
  14. export interface BucketI {
  15. name: string;
  16. }
  17. export interface FileI {
  18. name: string;
  19. }
  20. export interface Query {
  21. [key: string]: string;
  22. }
  23. export interface GetSignedUrlConfigInternal {
  24. expiration: number;
  25. accessibleAt?: Date;
  26. method: string;
  27. extensionHeaders?: http.OutgoingHttpHeaders;
  28. queryParams?: Query;
  29. cname?: string;
  30. contentMd5?: string;
  31. contentType?: string;
  32. bucket: string;
  33. file?: string;
  34. /**
  35. * The host for the generated signed URL
  36. *
  37. * @example
  38. * 'https://localhost:8080/'
  39. */
  40. host?: string | URL;
  41. /**
  42. * An endpoint for generating the signed URL
  43. *
  44. * @example
  45. * 'https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/'
  46. */
  47. signingEndpoint?: string | URL;
  48. }
  49. export interface SignerGetSignedUrlConfig {
  50. method: 'GET' | 'PUT' | 'DELETE' | 'POST';
  51. expires: string | number | Date;
  52. accessibleAt?: string | number | Date;
  53. virtualHostedStyle?: boolean;
  54. version?: 'v2' | 'v4';
  55. cname?: string;
  56. extensionHeaders?: http.OutgoingHttpHeaders;
  57. queryParams?: Query;
  58. contentMd5?: string;
  59. contentType?: string;
  60. /**
  61. * The host for the generated signed URL
  62. *
  63. * @example
  64. * 'https://localhost:8080/'
  65. */
  66. host?: string | URL;
  67. /**
  68. * An endpoint for generating the signed URL
  69. *
  70. * @example
  71. * 'https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/'
  72. */
  73. signingEndpoint?: string | URL;
  74. }
  75. export type SignerGetSignedUrlResponse = string;
  76. export type GetSignedUrlResponse = [SignerGetSignedUrlResponse];
  77. export interface GetSignedUrlCallback {
  78. (err: Error | null, url?: string): void;
  79. }
  80. export declare enum SignerExceptionMessages {
  81. ACCESSIBLE_DATE_INVALID = "The accessible at date provided was invalid.",
  82. EXPIRATION_BEFORE_ACCESSIBLE_DATE = "An expiration date cannot be before accessible date.",
  83. X_GOOG_CONTENT_SHA256 = "The header X-Goog-Content-SHA256 must be a hexadecimal string."
  84. }
  85. /**
  86. * @const {string}
  87. * @deprecated - unused
  88. */
  89. export declare const PATH_STYLED_HOST = "https://storage.googleapis.com";
  90. export declare class URLSigner {
  91. private auth;
  92. private bucket;
  93. private file?;
  94. /**
  95. * A {@link Storage} object.
  96. *
  97. * @privateRemarks
  98. *
  99. * Technically this is a required field, however it would be a breaking change to
  100. * move it before optional properties. In the next major we should refactor the
  101. * constructor of this class to only accept a config object.
  102. */
  103. private storage;
  104. constructor(auth: AuthClient | GoogleAuthLike, bucket: BucketI, file?: FileI | undefined,
  105. /**
  106. * A {@link Storage} object.
  107. *
  108. * @privateRemarks
  109. *
  110. * Technically this is a required field, however it would be a breaking change to
  111. * move it before optional properties. In the next major we should refactor the
  112. * constructor of this class to only accept a config object.
  113. */
  114. storage?: Storage);
  115. getSignedUrl(cfg: SignerGetSignedUrlConfig): Promise<SignerGetSignedUrlResponse>;
  116. private getSignedUrlV2;
  117. private getSignedUrlV4;
  118. /**
  119. * Create canonical headers for signing v4 url.
  120. *
  121. * The canonical headers for v4-signing a request demands header names are
  122. * first lowercased, followed by sorting the header names.
  123. * Then, construct the canonical headers part of the request:
  124. * <lowercasedHeaderName> + ":" + Trim(<value>) + "\n"
  125. * ..
  126. * <lowercasedHeaderName> + ":" + Trim(<value>) + "\n"
  127. *
  128. * @param headers
  129. * @private
  130. */
  131. getCanonicalHeaders(headers: http.OutgoingHttpHeaders): string;
  132. getCanonicalRequest(method: string, path: string, query: string, headers: string, signedHeaders: string, contentSha256?: string): string;
  133. getCanonicalQueryParams(query: Query): string;
  134. getResourcePath(cname: boolean, bucket: string, file?: string): string;
  135. parseExpires(expires: string | number | Date, current?: Date): number;
  136. parseAccessibleAt(accessibleAt?: string | number | Date): number;
  137. }
  138. /**
  139. * Custom error type for errors related to getting signed errors and policies.
  140. *
  141. * @private
  142. */
  143. export declare class SigningError extends Error {
  144. name: string;
  145. }
  146. export {};