-
Notifications
You must be signed in to change notification settings - Fork 0
/
sigv4.ts
124 lines (108 loc) · 3.22 KB
/
sigv4.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { Parameters, Headers } from "mappersmith"
import { hmac, sha256, Hash } from "./crypto"
const algo = "AWS4-HMAC-SHA256"
function builCanonicalRequest(
method: string,
path: string,
params: Parameters,
headers: Headers,
body: string | object
) {
return [
method.toUpperCase(),
encodeURI(path),
buildCanonicalQueryString(params),
buildCanonicalHeaders(headers),
buildCanonicalSignedHeaders(headers),
sha256(body.toString()),
].join("\n")
}
function buildCanonicalQueryString(params: Parameters): string {
return Object.keys(params)
.sort()
.map((key) => [key, fixedEncodeURIComponent(String(params[key]))].join("="))
.join("&")
}
function fixedEncodeURIComponent(str: string): string {
return encodeURIComponent(str).replace(/[!'()*]/g, (c) => "%" + c.charCodeAt(0).toString(16))
}
function buildCanonicalHeaders(headers: Headers): string {
return (
Object.keys(headers)
.sort()
.map((key) => [key.toLowerCase(), headers[key]].join(":"))
.join("\n") + "\n"
)
}
function buildCanonicalSignedHeaders(headers: Headers): string {
return Object.keys(headers).sort().join(";")
}
function buildStringToSign(
timestamp: string,
credentialScope: string,
hashedCanonicalRequest: Hash
): string {
return [algo, timestamp, credentialScope, hashedCanonicalRequest].join("\n")
}
function buildCredentialScope(date: string, region: string, service: string): string {
return [date, region, service, "aws4_request"].join("/")
}
function calculateSigningKey(
secretKey: string,
date: string,
region: string,
service: string
): Hash {
return hmac("aws4_request", hmac(service, hmac(region, hmac(date, "AWS4" + secretKey))))
}
function buildAuthorizationHeader(
accessKey: string,
credentialScope: string,
headers: Headers,
signature: Hash
): string {
const credential = [accessKey, credentialScope].join("/")
const signedHeaders = buildCanonicalSignedHeaders(headers)
return `${algo} Credential=${credential}, SignedHeaders=${signedHeaders}, Signature=${signature}`
}
export async function sigv4({
systemClockOffset = 0,
region,
host,
params,
body,
headers,
secretKey,
accessKey,
method,
path,
service,
now = new Date(),
}: {
systemClockOffset?: number
region: string
host: string
params: Parameters
body: string | object
headers: Headers
secretKey: string
accessKey: string
method: string
path: string
service: string
now?: Date
}) {
headers = { ...headers, host }
const timestamp = new Date(now.getTime() + systemClockOffset)
.toISOString()
.replace(/[:\-]|\.\d{3}/g, "")
const date = timestamp.substr(0, 8)
const canonicalRequest = builCanonicalRequest(method, path, params, headers, body)
const hashedCanonicalRequest = sha256(canonicalRequest)
const credentialScope = buildCredentialScope(date, region, service)
const stringToSign = buildStringToSign(timestamp, credentialScope, hashedCanonicalRequest)
const signingKey = calculateSigningKey(secretKey, date, region, service)
const signature = hmac(stringToSign, signingKey)
const authorization = buildAuthorizationHeader(accessKey, credentialScope, headers, signature)
return { authorization, timestamp, info: { canonicalRequest, stringToSign } }
}