-
Notifications
You must be signed in to change notification settings - Fork 27
/
HawkHelper.swift
140 lines (126 loc) · 5.29 KB
/
HawkHelper.swift
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
import FxA
import Shared
public class HawkHelper {
private let NonceLengthInBytes: UInt = 8
let id: String
let key: NSData
public init(id: String, key: NSData) {
self.id = id
self.key = key
}
// Produce a HAWK value suitable for an "Authorization: value" header, timestamped now.
public func getAuthorizationValueFor(request: NSURLRequest) -> String {
let timestampInSeconds: Int64 = Int64(NSDate().timeIntervalSince1970)
return getAuthorizationValueFor(request, at: timestampInSeconds)
}
// Produce a HAWK value suitable for an "Authorization: value" header.
func getAuthorizationValueFor(request: NSURLRequest, at timestampInSeconds: Int64) -> String {
let nonce = NSData.randomOfLength(NonceLengthInBytes)!.base64EncodedString
let extra = ""
return getAuthorizationValueFor(request, at: timestampInSeconds, nonce: nonce, extra: extra)
}
func getAuthorizationValueFor(request: NSURLRequest, at timestampInSeconds: Int64, nonce: String, extra: String) -> String {
let timestampString = String(timestampInSeconds)
let hashString = HawkHelper.getPayloadHashFor(request)
let requestString = HawkHelper.getRequestStringFor(request, timestampString: timestampString, nonce: nonce, hash: hashString, extra: extra)
let macString = HawkHelper.getSignatureFor(requestString.utf8EncodedData!, key: self.key)
let s = NSMutableString(string: "Hawk ")
func append(key: String, value: String) -> Void {
s.appendString(key)
s.appendString("=\"")
s.appendString(value)
s.appendString("\", ")
}
append("id", value: id)
append("ts", value: timestampString)
append("nonce", value: nonce)
if !hashString.isEmpty {
append("hash", value: hashString)
}
if !extra.isEmpty {
append("ext", value: HawkHelper.escapeExtraHeaderAttribute(extra))
}
append("mac", value: macString)
// Drop the trailing "\",".
return s.substringToIndex(s.length - 2)
}
class func getSignatureFor(input: NSData, key: NSData) -> String {
return input.hmacSha256WithKey(key).base64EncodedString
}
class func getRequestStringFor(request: NSURLRequest, timestampString: String, nonce: String, hash: String, extra: String) -> String {
let s = NSMutableString(string: "hawk.1.header\n")
func append(line: String) -> Void {
s.appendString(line)
s.appendString("\n")
}
append(timestampString)
append(nonce)
append(request.HTTPMethod?.uppercaseString ?? "GET")
let url = request.URL!
s.appendString(url.path!)
if let query = url.query {
s.appendString("?")
s.appendString(query)
}
if let fragment = url.fragment {
s.appendString("#")
s.appendString(fragment)
}
s.appendString("\n")
append(url.host!)
if let port = url.port {
append(port.stringValue)
} else {
if url.scheme.lowercaseString == "https" {
append("443")
} else {
append("80")
}
}
append(hash)
if !extra.isEmpty {
append(HawkHelper.escapeExtraString(extra))
} else {
append("")
}
return s as String
}
class func getPayloadHashFor(request: NSURLRequest) -> String {
if let body = request.HTTPBody {
let d = NSMutableData()
func append(s: String) {
let data = s.utf8EncodedData!
d.appendBytes(data.bytes, length: data.length)
}
append("hawk.1.payload\n")
append(getBaseContentTypeFor(request.valueForHTTPHeaderField("Content-Type")))
append("\n") // Trailing newline is specified by Hawk.
d.appendBytes(body.bytes, length: body.length)
append("\n") // Trailing newline is specified by Hawk.
return d.sha256.base64EncodedString
} else {
return ""
}
}
class func getBaseContentTypeFor(contentType: String?) -> String {
if let contentType = contentType {
if let index = contentType.characters.indexOf(";") {
return contentType.substringToIndex(index).stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
} else {
return contentType.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
}
} else {
return "text/plain"
}
}
class func escapeExtraHeaderAttribute(extra: String) -> String {
return extra.stringByReplacingOccurrencesOfString("\\", withString: "\\\\").stringByReplacingOccurrencesOfString("\"", withString: "\\\"")
}
class func escapeExtraString(extra: String) -> String {
return extra.stringByReplacingOccurrencesOfString("\\", withString: "\\\\").stringByReplacingOccurrencesOfString("\n", withString: "\\n")
}
}