Skip to content

Commit 4c94fc3

Browse files
authored
Enforce openid scope on Web Auth [SDK-2924] (#535)
1 parent baac55c commit 4c94fc3

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

Auth0/Auth0WebAuth.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ final class Auth0WebAuth: WebAuth {
3939
private let platform = "ios"
4040
#endif
4141

42+
private let requiredScope = "openid"
4243
private(set) var parameters: [String: String] = [:]
4344
private(set) var issuer: String
4445
private(set) var leeway: Int = 60 * 1000 // Default leeway is 60 seconds
@@ -222,7 +223,7 @@ final class Auth0WebAuth: WebAuth {
222223
var entries = defaults
223224
entries["client_id"] = self.clientId
224225
entries["redirect_uri"] = redirectURL.absoluteString
225-
entries["scope"] = "openid"
226+
entries["scope"] = requiredScope // TODO: Change when setting the new default scope
226227
entries["state"] = state
227228
entries["response_type"] = self.responseType.map { $0.label! }.joined(separator: " ")
228229

@@ -241,6 +242,10 @@ final class Auth0WebAuth: WebAuth {
241242

242243
self.parameters.forEach { entries[$0] = $1 }
243244

245+
if let scope = entries["scope"]?.split(separator: " ").map(String.init), !scope.contains(requiredScope) {
246+
entries["scope"] = "\(requiredScope) \(entries["scope"]!)"
247+
}
248+
244249
entries.forEach { items.append(URLQueryItem(name: $0, value: $1)) }
245250
components.queryItems = self.telemetry.queryItemsWithTelemetry(queryItems: items)
246251
components.percentEncodedQuery = components.percentEncodedQuery?.replacingOccurrences(of: "+", with: "%2B")

Auth0Tests/WebAuthSpec.swift

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private func defaultQuery(withParameters parameters: [String: String] = [:]) ->
7777
"response_type": "code",
7878
"redirect_uri": RedirectURL.absoluteString,
7979
"scope": "openid",
80-
]
80+
]
8181
parameters.forEach { query[$0] = $1 }
8282
return query
8383
}
@@ -130,11 +130,24 @@ class WebAuthSpec: QuickSpec {
130130
]
131131
}
132132

133-
it("should override default values") {
134-
let url = newWebAuth()
135-
.parameters(["scope": "openid email phone"])
136-
.buildAuthorizeURL(withRedirectURL: RedirectURL, defaults: defaults, state: State, organization: nil, invitation: nil)
137-
expect(url.a0_components?.queryItems).toNot(containItem(withName: "scope", value: "openid"))
133+
itBehavesLike(ValidAuthorizeURLExample) {
134+
return [
135+
"url": newWebAuth()
136+
.parameters(["scope": "openid email phone"])
137+
.buildAuthorizeURL(withRedirectURL: RedirectURL, defaults: defaults, state: State, organization: nil, invitation: nil),
138+
"domain": Domain,
139+
"query": defaultQuery(withParameters: ["scope": "openid email phone"]),
140+
]
141+
}
142+
143+
itBehavesLike(ValidAuthorizeURLExample) {
144+
return [
145+
"url": newWebAuth()
146+
.parameters(["scope": "email phone"])
147+
.buildAuthorizeURL(withRedirectURL: RedirectURL, defaults: defaults, state: State, organization: nil, invitation: nil),
148+
"domain": Domain,
149+
"query": defaultQuery(withParameters: ["scope": "openid email phone"]),
150+
]
138151
}
139152

140153
itBehavesLike(ValidAuthorizeURLExample) {

V2_MIGRATION_GUIDE.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The deployment targets for each platform have been raised to:
1616

1717
The minimum supported Swift version is now 5.3.
1818

19-
## HS256 support
19+
## Supported JWT signature algorithms
2020

2121
ID Tokens signed with the HS256 algorithm are no longer allowed.
2222
This is because HS256 is a symmetric algorithm, which is not suitable for public clients like mobile apps.
@@ -37,7 +37,7 @@ Both have been subsumed in `AuthTransaction`.
3737

3838
## Type properties changed
3939

40-
### Credentials class
40+
### `Credentials` class
4141

4242
The following properties are no longer optional:
4343

@@ -57,7 +57,7 @@ The following methods lost the `parameters` parameter:
5757
- `loginDefaultDirectory(withUsername:password:audience:scope:)`
5858
- `tokenExchange()`
5959

60-
To pass custom parameters to those (or any) method, use the `parameters()` method from `Request`:
60+
To pass custom parameters to those (or any) method, use the `parameters(_:)` method from `Request`:
6161

6262
```swift
6363
Auth0
@@ -69,6 +69,21 @@ Auth0
6969
}
7070
```
7171

72+
## Behavior changes
73+
74+
### `openid` scope enforced on Web Auth
75+
76+
If the scopes passed via the Web Auth method `.scope(_:)` do not include the `openid` scope, it will be added automatically.
77+
78+
```swift
79+
Auth0
80+
.webAuth()
81+
.scope("profile email") // "openid profile email" will be used
82+
.start { result in
83+
print(result)
84+
}
85+
```
86+
7287
## Title of change
7388

7489
Description of change

0 commit comments

Comments
 (0)