|
1 | | -// Type definitions for aws4 1.5.0 |
| 1 | +// Type definitions for aws4 1.11 |
2 | 2 | // Project: https://github.com/mhart/aws4 |
3 | 3 | // Definitions by: Andrew Crites <https://github.com/ajcrites> |
| 4 | +// Alexandre Szymocha <https://github.com/Aksamyt> |
4 | 5 | // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped |
| 6 | +// Minimum TypeScript Version: 4.0 |
| 7 | + |
| 8 | +/// <reference types="node" /> |
| 9 | + |
| 10 | +import { OutgoingHttpHeaders, RequestOptions } from 'http'; |
| 11 | + |
| 12 | +export interface Request extends RequestOptions { |
| 13 | + /** Defaults to {@link RequestSigner.createHost}() if possible. */ |
| 14 | + host?: string; |
| 15 | + /** Equivalent to {@link host} */ |
| 16 | + hostname?: string; |
| 17 | + /** Defaults to `"GET"`, or `"POST"` if there is a {@link body}. */ |
| 18 | + method?: string; |
| 19 | + /** Defaults to `/`. */ |
| 20 | + path?: string; |
| 21 | + /** Defaults to `""`. */ |
| 22 | + body?: string; |
| 23 | + /** Defaults to {@link RequestSigner.matchHost}()[0], or `""`. */ |
| 24 | + service?: string; |
| 25 | + /** Defaults to {@link RequestSigner.matchHost}()[1], or `"us-east-1"`. */ |
| 26 | + region?: string; |
| 27 | + /** To sign the query instead of adding an Authorization header, defaults to false. */ |
| 28 | + signQuery?: boolean; |
| 29 | + /** |
| 30 | + * Some headers have a default value: |
| 31 | + * - `Host`: Defaults to {@link hostname}, {@link host}, or |
| 32 | + * {@link RequestSigner.createHost}() |
| 33 | + * - `Content-Type`: defaults to |
| 34 | + * `"application/x-www-form-urlencoded; charset=utf-8"` |
| 35 | + * if there is a {@link body} |
| 36 | + * - `Date`: used to calculate the signature date if given, defaults to |
| 37 | + * `new Date()` |
| 38 | + */ |
| 39 | + headers?: OutgoingHttpHeaders; |
| 40 | + /** If true, signing the Request won’t mutate the headers. */ |
| 41 | + doNotModifyHeaders?: boolean; |
| 42 | + /** If true, {@link path} won’t be encoded in the signature. */ |
| 43 | + doNotEncodePath?: boolean; |
| 44 | +} |
| 45 | + |
| 46 | +export interface Credentials { |
| 47 | + /** Equivalent to the env vars `AWS_ACCESS_KEY_ID` and `AWS_ACCESS_KEY`. */ |
| 48 | + accessKeyId?: string; |
| 49 | + /** Equivalent to the env vars `AWS_SECRET_ACCESS_KEY` and `AWS_SECRET_KEY`. */ |
| 50 | + secretAccessKey?: string; |
| 51 | + /** Equivalent to the env var `AWS_SESSION_TOKEN`. */ |
| 52 | + sessionToken?: string; |
| 53 | +} |
5 | 54 |
|
6 | 55 | export class RequestSigner { |
7 | | - constructor(request?: any, credentials?: any); |
8 | | - request: any; |
9 | | - credentials: any; |
10 | | - service: any; |
11 | | - region: any; |
12 | | - isCodeCommitGit: any; |
13 | | - |
14 | | - matchHost(host?: string): string[]; |
| 56 | + constructor(requestOptions: string | Request, credentials?: Credentials); |
| 57 | + |
| 58 | + request: Request; |
| 59 | + credentials: Credentials; |
| 60 | + service: string; |
| 61 | + region: string; |
| 62 | + |
| 63 | + /** |
| 64 | + * `true` if {@link service} is `"codecommit"` and {@link request}.method |
| 65 | + * is `"GIT"`. |
| 66 | + */ |
| 67 | + isCodeCommitGit: boolean; |
| 68 | + |
| 69 | + /** |
| 70 | + * Set by: |
| 71 | + * - {@link prepareRequest} |
| 72 | + * - {@link sign} |
| 73 | + * - {@link canonicalString} |
| 74 | + * - {@link parsePath} |
| 75 | + */ |
| 76 | + parsedPath?: { |
| 77 | + path: string; |
| 78 | + query: Record<string, string | string[]>; |
| 79 | + }; |
| 80 | + |
| 81 | + /** |
| 82 | + * Used as a cache by {@link getDateTime} and {@link getDate}. |
| 83 | + * Setting it will shortcut those functions. |
| 84 | + * |
| 85 | + * Set by: |
| 86 | + * - {@link prepareRequest} |
| 87 | + * - {@link getDateTime} |
| 88 | + * - {@link sign} |
| 89 | + */ |
| 90 | + datetime?: string; |
| 91 | + |
| 92 | + /** |
| 93 | + * Extract the service code and region code from a Host name. |
| 94 | + */ |
| 95 | + matchHost(host: string): [service: string, region: string]; |
| 96 | + |
| 97 | + /** |
| 98 | + * https://docs.aws.amazon.com/general/latest/gr/rande.html |
| 99 | + */ |
15 | 100 | isSingleRegion(): boolean; |
| 101 | + |
| 102 | + /** |
| 103 | + * Compute the Host name from the {@link service} and the {@link region}. |
| 104 | + */ |
16 | 105 | createHost(): string; |
17 | | - prepareRequest(): void; |
18 | | - sign(): any; |
19 | | - getDateTime(): string; |
20 | | - getDate(): string; |
| 106 | + |
| 107 | + /** |
| 108 | + * Generate the Authorization header value. |
| 109 | + */ |
21 | 110 | authHeader(): string; |
22 | | - signature(): string; |
23 | | - stringToSign(): string; |
24 | | - canonicalString(): string; |
| 111 | + |
| 112 | + /** |
| 113 | + * Generate a string representation of {@link request}.headers: |
| 114 | + * - one header per line, formatted as `<key>:<value>` |
| 115 | + * - lines are sorted. |
| 116 | + * - `<key>` is in lowercase and is trimmed. |
| 117 | + * - `<value>` is trimmed, and contiguous whitespaces are reduced to a |
| 118 | + * single space character ` `. |
| 119 | + * |
| 120 | + * These headers are filtered out: |
| 121 | + * - `authorization` |
| 122 | + * - `connection` |
| 123 | + * - `x-amzn-trace-id` |
| 124 | + * - `user-agent` |
| 125 | + * - `expect` |
| 126 | + * - `presigned-expires` |
| 127 | + * - `range` |
| 128 | + */ |
25 | 129 | canonicalHeaders(): string; |
| 130 | + |
| 131 | + /** |
| 132 | + * Generates a list of the header names of {@link request}.headers separated |
| 133 | + * by `;`. |
| 134 | + * |
| 135 | + * These headers are filtered out: |
| 136 | + * - `authorization` |
| 137 | + * - `connection` |
| 138 | + * - `x-amzn-trace-id` |
| 139 | + * - `user-agent` |
| 140 | + * - `expect` |
| 141 | + * - `presigned-expires` |
| 142 | + * - `range` |
| 143 | + */ |
26 | 144 | signedHeaders(): string; |
| 145 | + |
| 146 | + /** |
| 147 | + * Generate the “credential scope” part of the signature. |
| 148 | + */ |
27 | 149 | credentialString(): string; |
28 | | - defaultCredentials(): any; |
29 | | - parsePath(): any; |
| 150 | + |
| 151 | + /** |
| 152 | + * Extract credentials from the environment, if found. |
| 153 | + */ |
| 154 | + defaultCredentials(): Credentials; |
| 155 | + |
| 156 | + /** |
| 157 | + * Parse {@link request}.path and store the results in {@link parsedPath}. |
| 158 | + */ |
| 159 | + parsePath(): void; |
| 160 | + |
| 161 | + /** |
| 162 | + * **Will throw if {@link parsedPath} is undefined!** |
| 163 | + * |
| 164 | + * Rebuild the path from {@link parsedPath}. |
| 165 | + */ |
30 | 166 | formatPath(): string; |
| 167 | + |
| 168 | + /** |
| 169 | + * Sign the Request. The Request is returned for convenience. |
| 170 | + */ |
| 171 | + sign(): Request; |
| 172 | + |
| 173 | + /** |
| 174 | + * Calls {@link parsePath}. |
| 175 | + * Prepare the Request by setting the required headers, |
| 176 | + * or query parameters if {@link request}.signQuery is true. |
| 177 | + */ |
| 178 | + prepareRequest(): void; |
| 179 | + |
| 180 | + /** |
| 181 | + * If {@link datetime} is set, it is returned without further processing. |
| 182 | + * |
| 183 | + * Else, parse the `Date` header from {@link request}.headers, |
| 184 | + * or get the current date, and format it as (ISO 8601): |
| 185 | + * ``` |
| 186 | + * YYYYMMDDThhmmssZ |
| 187 | + * ``` |
| 188 | + * If {@link isCodeCommitGit} is `true`, then the trailing `Z` is removed. |
| 189 | + * |
| 190 | + * The result is stored to {@link datetime} before being returned. |
| 191 | + */ |
| 192 | + getDateTime(): string; |
| 193 | + |
| 194 | + /** |
| 195 | + * If {@link datetime} is set, its first 8 characters are returned. |
| 196 | + * |
| 197 | + * Else, call {@link getDateTime} and return the first 8 characters. |
| 198 | + */ |
| 199 | + getDate(): string; |
| 200 | + |
| 201 | + /** |
| 202 | + * Generate a string representation of the Request. |
| 203 | + * https://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html |
| 204 | + */ |
| 205 | + canonicalString(): string; |
| 206 | + |
| 207 | + /** |
| 208 | + * Compute the Request HMAC signature. |
| 209 | + */ |
| 210 | + signature(): string; |
| 211 | + |
| 212 | + /** |
| 213 | + * Generate the raw data to be signed. |
| 214 | + * https://docs.aws.amazon.com/general/latest/gr/sigv4-create-string-to-sign.html |
| 215 | + */ |
| 216 | + stringToSign(): string; |
31 | 217 | } |
32 | 218 |
|
33 | | -export function sign(options?: any, credentials?: any): any; |
| 219 | +/** |
| 220 | + * Calculates and populates any necessary AWS headers and/or request options on |
| 221 | + * `requestOptions`. Returns `requestOptions` as a convenience for chaining. |
| 222 | + */ |
| 223 | +export function sign(requestOptions: string | Request, credentials?: Credentials): Request; |
0 commit comments