Skip to content

Commit

Permalink
Remove parameterless tokenExchange() method (#573)
Browse files Browse the repository at this point in the history
* Remove parameterless `tokenExchange()` method

* Address review feedback
  • Loading branch information
Widcket authored Dec 7, 2021
1 parent 30776df commit aee55ac
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 40 deletions.
36 changes: 19 additions & 17 deletions Auth0/Auth0Authentication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -270,22 +270,8 @@ struct Auth0Authentication: Authentication {
telemetry: self.telemetry)
}

func tokenExchange() -> Request<Credentials, AuthenticationError> {
let payload: [String: Any] = [
"client_id": self.clientId
]
let token = URL(string: "oauth/token", relativeTo: self.url)!
return Request(session: session,
url: token,
method: "POST",
handle: codable,
parameters: payload,
logger: self.logger,
telemetry: self.telemetry)
}

func tokenExchange(withCode code: String, codeVerifier: String, redirectURI: String) -> Request<Credentials, AuthenticationError> {
return self.tokenExchange().parameters([
func codeExchange(withCode code: String, codeVerifier: String, redirectURI: String) -> Request<Credentials, AuthenticationError> {
return self.token().parameters([
"code": code,
"code_verifier": codeVerifier,
"redirect_uri": redirectURI,
Expand Down Expand Up @@ -340,6 +326,7 @@ struct Auth0Authentication: Authentication {
// MARK: - Private Methods

private extension Auth0Authentication {

func login(username: String, otp: String, realm: String, audience: String?, scope: String) -> Request<Credentials, AuthenticationError> {
let url = URL(string: "oauth/token", relativeTo: self.url)!
var payload: [String: Any] = [
Expand All @@ -360,13 +347,28 @@ private extension Auth0Authentication {
telemetry: self.telemetry)
}

func token() -> Request<Credentials, AuthenticationError> {
let payload: [String: Any] = [
"client_id": self.clientId
]
let token = URL(string: "oauth/token", relativeTo: self.url)!
return Request(session: session,
url: token,
method: "POST",
handle: codable,
parameters: payload,
logger: self.logger,
telemetry: self.telemetry)
}

func tokenExchange(subjectToken: String, subjectTokenType: String, scope: String, audience: String?, parameters: [String: Any]?) -> Request<Credentials, AuthenticationError> {
var parameters: [String: Any] = parameters ?? [:]
parameters["grant_type"] = "urn:ietf:params:oauth:grant-type:token-exchange"
parameters["subject_token"] = subjectToken
parameters["subject_token_type"] = subjectTokenType
parameters["audience"] = audience
parameters["scope"] = scope
return self.tokenExchange().parameters(parameters)
return self.token().parameters(parameters)
}

}
20 changes: 2 additions & 18 deletions Auth0/Authentication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -410,22 +410,6 @@ public protocol Authentication: Trackable, Loggable {
*/
func userInfo(withAccessToken accessToken: String) -> Request<UserInfo, AuthenticationError>

/**
Perform a OAuth2 token request against Auth0.

```
Auth0
.authentication(clientId: clientId, domain: "samples.auth0.com")
.tokenExchange()
.parameters(["key": "value"])
.start { print($0) }
```

- returns: a request that will yield Auth0 user's credentials
- seeAlso: exchangeCode(codeVerifier:, redirectURI:) for PKCE
*/
func tokenExchange() -> Request<Credentials, AuthenticationError>

/**
Performs the last step of Proof Key for Code Exchange [RFC 7636](https://tools.ietf.org/html/rfc7636).

Expand All @@ -434,7 +418,7 @@ public protocol Authentication: Trackable, Loggable {
```
Auth0
.authentication(clientId: clientId, domain: "samples.auth0.com")
.tokenExchange(withCode: "a code", codeVerifier: "code verifier", redirectURI: "https://samples.auth0.com/callback")
.codeExchange(withCode: "a code", codeVerifier: "code verifier", redirectURI: "https://samples.auth0.com/callback")
.start { print($0) }
```

Expand All @@ -445,7 +429,7 @@ public protocol Authentication: Trackable, Loggable {
- returns: a request that will yield Auth0 user's credentials
- seeAlso: https://tools.ietf.org/html/rfc7636
*/
func tokenExchange(withCode code: String, codeVerifier: String, redirectURI: String) -> Request<Credentials, AuthenticationError>
func codeExchange(withCode code: String, codeVerifier: String, redirectURI: String) -> Request<Credentials, AuthenticationError>

/**
Renew user's credentials with a refresh_token grant for `/oauth/token`
Expand Down
2 changes: 1 addition & 1 deletion Auth0/OAuth2Grant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ struct PKCE: OAuth2Grant {
nonce: self.defaults["nonce"],
organization: self.organization)
authentication
.tokenExchange(withCode: code, codeVerifier: verifier, redirectURI: redirectUrlString)
.codeExchange(withCode: code, codeVerifier: verifier, redirectURI: redirectUrlString)
.start { result in
switch result {
case .failure(let error) where error.localizedDescription == "Unauthorized":
Expand Down
4 changes: 2 additions & 2 deletions Auth0Tests/AuthenticationSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ class AuthenticationSpec: QuickSpec {
it("should exchange code for tokens") {
stub(condition: isToken(Domain) && hasAtLeast(["code": code, "code_verifier": codeVerifier, "grant_type": "authorization_code", "redirect_uri": redirectURI])) { _ in return authResponse(accessToken: AccessToken, idToken: IdToken) }.name = "Code Exchange Auth"
waitUntil(timeout: Timeout) { done in
auth.tokenExchange(withCode: code, codeVerifier: codeVerifier, redirectURI: redirectURI).start { result in
auth.codeExchange(withCode: code, codeVerifier: codeVerifier, redirectURI: redirectURI).start { result in
expect(result).to(haveCredentials(AccessToken, IdToken))
done()
}
Expand All @@ -1019,7 +1019,7 @@ class AuthenticationSpec: QuickSpec {
let description = "Invalid code"
let invalidCode = "return invalid code"
stub(condition: isToken(Domain) && hasAtLeast(["code": invalidCode])) { _ in return authFailure(code: code, description: description) }.name = "Invalid Code"
auth.tokenExchange(withCode: invalidCode, codeVerifier: codeVerifier, redirectURI: redirectURI).start { result in
auth.codeExchange(withCode: invalidCode, codeVerifier: codeVerifier, redirectURI: redirectURI).start { result in
expect(result).to(haveAuthenticationError(code: code, description: description))
done()
}
Expand Down
21 changes: 19 additions & 2 deletions V2_MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ Use `createUser(email:username:password:connection:userMetadata:rootAttributes:`

Use `userInfo(withAccessToken:)` instead.

#### `tokenExchange(withParameters:)`

Use `codeExchange(withCode:codeVerifier:redirectURI:)` instead. To pass custom parameters, use the `parameters(_:)` method from `Request`:

```swift
Auth0
.authentication()
.codeExchange(withCode: code, codeVerifier: codeVerifier, redirectURI: redirectURI)
.parameters(["key": "value"]) // 👈🏻
.start { result in
// ...
}
```

#### `tokenExchange(withAppleAuthorizationCode:scope:audience:fullName:)`

Use `login(appleAuthorizationCode:fullName:profile:audience:scope:)` instead.
Expand Down Expand Up @@ -298,21 +312,24 @@ These properties were removed:

The Authentication API client methods will now only yield errors of type `AuthenticationError`. The underlying error (if any) is available via the `cause: Error?` property of the `AuthenticationError`.

#### Renamed `tokenExchange(withCode:codeVerifier:redirectURI:)`

The method `tokenExchange(withCode:codeVerifier:redirectURI:)` was renamed to `codeExchange(withCode:codeVerifier:redirectURI:)`.

#### Removed `parameters` parameter

The following methods lost the `parameters` parameter:

- `login(phoneNumber:code:audience:scope:)`
- `login(usernameOrEmail:password:realm:audience:scope:)`
- `loginDefaultDirectory(withUsername:password:audience:scope:)`
- `tokenExchange()`

To pass custom parameters to those (or any) method in the Authentication client, use the `parameters(_:)` method from `Request`:

```swift
Auth0
.authentication()
.tokenExchange() // Returns a Request
.renew(withRefreshToken: refreshToken) // Returns a Request
.parameters(["key": "value"]) // 👈🏻
.start { result in
// ...
Expand Down

0 comments on commit aee55ac

Please sign in to comment.