Skip to content

Commit 0b756f7

Browse files
authored
Nonce computation changes (#1118)
1 parent 2b2f0db commit 0b756f7

File tree

2 files changed

+97
-50
lines changed

2 files changed

+97
-50
lines changed

Auth0/Auth0WebAuth.swift

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ final class Auth0WebAuth: WebAuth {
2929
private(set) var ephemeralSession = false
3030
private(set) var issuer: String
3131
private(set) var leeway: Int = 60 * 1000 // Default leeway is 60 seconds
32-
private(set) var nonce: String?
3332
private(set) var maxAge: Int?
3433
private(set) var organization: String?
3534
private(set) var invitationURL: URL?
@@ -38,7 +37,11 @@ final class Auth0WebAuth: WebAuth {
3837
private(set) var onCloseCallback: (() -> Void)?
3938

4039
var state: String {
41-
return self.parameters["state"] ?? self.generateDefaultState()
40+
return parameters["state"] ?? generateRandomString()
41+
}
42+
43+
var nonce: String {
44+
return parameters["nonce"] ?? generateRandomString()
4245
}
4346

4447
lazy var redirectURL: URL? = {
@@ -91,6 +94,11 @@ final class Auth0WebAuth: WebAuth {
9194
return self
9295
}
9396

97+
func nonce(_ nonce: String) -> Self {
98+
self.parameters["nonce"] = nonce
99+
return self
100+
}
101+
94102
func state(_ state: String) -> Self {
95103
self.parameters["state"] = state
96104
return self
@@ -117,11 +125,6 @@ final class Auth0WebAuth: WebAuth {
117125
return self
118126
}
119127

120-
func nonce(_ nonce: String) -> Self {
121-
self.nonce = nonce
122-
return self
123-
}
124-
125128
func audience(_ audience: String) -> Self {
126129
self.parameters["audience"] = audience
127130
return self
@@ -181,13 +184,15 @@ final class Auth0WebAuth: WebAuth {
181184
return callback(.failure(WebAuthError(code: .noBundleIdentifier)))
182185
}
183186

184-
let handler = self.handler(redirectURL)
185-
let state = self.state
187+
let nonce = nonce
188+
let state = state
189+
let handler = self.handler(redirectURL, nonce: nonce)
186190

187191
let authorizeURL: URL
188192
do {
189193
authorizeURL = try self.buildAuthorizeURL(withRedirectURL: redirectURL,
190194
defaults: handler.defaults,
195+
nonce: nonce,
191196
state: state)
192197
} catch {
193198
return callback(.failure(error))
@@ -249,7 +254,8 @@ final class Auth0WebAuth: WebAuth {
249254

250255
func buildAuthorizeURL(withRedirectURL redirectURL: URL,
251256
defaults: [String: String],
252-
state: String?) throws(WebAuthError) -> URL {
257+
nonce: String,
258+
state: String) throws(WebAuthError) -> URL {
253259
guard let authorize = self.overrideAuthorizeURL ?? URL(string: "authorize", relativeTo: self.url),
254260
var components = URLComponents(url: authorize, resolvingAgainstBaseURL: true) else {
255261
let message = "Unable to build authorize URL with base URL: \(self.url.absoluteString)."
@@ -264,7 +270,7 @@ final class Auth0WebAuth: WebAuth {
264270
entries["response_type"] = self.responseType
265271
entries["redirect_uri"] = redirectURL.absoluteString
266272
entries["state"] = state
267-
entries["nonce"] = self.nonce
273+
entries["nonce"] = nonce
268274
entries["organization"] = self.organization
269275

270276
if let invitationURL = self.invitationURL {
@@ -297,21 +303,18 @@ final class Auth0WebAuth: WebAuth {
297303
return components.url!
298304
}
299305

300-
func generateDefaultState() -> String {
306+
func generateRandomString() -> String {
301307
let data = Data(count: 32)
302308
var tempData = data
303-
304309
let result = tempData.withUnsafeMutableBytes {
305310
SecRandomCopyBytes(kSecRandomDefault, data.count, $0.baseAddress!)
306311
}
307-
308-
guard result == 0, let state = tempData.a0_encodeBase64URLSafe()
312+
guard result == errSecSuccess, let randomString = tempData.a0_encodeBase64URLSafe()
309313
else { return UUID().uuidString.replacingOccurrences(of: "-", with: "") }
310-
311-
return state
314+
return randomString
312315
}
313316

314-
private func handler(_ redirectURL: URL) -> OAuth2Grant {
317+
private func handler(_ redirectURL: URL, nonce: String) -> OAuth2Grant {
315318
var authentication = Auth0Authentication(clientId: self.clientId,
316319
url: self.url,
317320
session: self.session,
@@ -323,7 +326,7 @@ final class Auth0WebAuth: WebAuth {
323326
issuer: self.issuer,
324327
leeway: self.leeway,
325328
maxAge: self.maxAge,
326-
nonce: self.nonce,
329+
nonce: nonce,
327330
organization: self.organization)
328331
}
329332

0 commit comments

Comments
 (0)