Skip to content

Commit 3547cee

Browse files
authored
Adds an optional profile parameter to the login(appleAuthorizationCode) method (#392)
This enables passing more than just the name of the user
1 parent 749f980 commit 3547cee

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

Auth0/Auth0Authentication.swift

+14-8
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,22 @@ struct Auth0Authentication: Authentication {
133133
telemetry: self.telemetry)
134134
}
135135

136-
func login(appleAuthorizationCode authorizationCode: String, fullName: PersonNameComponents?, scope: String?, audience: String?) -> Request<Credentials, AuthenticationError> {
137-
var parameters: [String: String] = [:]
136+
func login(appleAuthorizationCode authorizationCode: String, fullName: PersonNameComponents?, profile: [String: Any]?, scope: String?, audience: String?) -> Request<Credentials, AuthenticationError> {
137+
var parameters: [String: Any] = [:]
138+
var profile: [String: Any] = profile ?? [:]
139+
138140
if let fullName = fullName {
139141
let name = ["firstName": fullName.givenName, "lastName": fullName.familyName].compactMapValues { $0 }
140-
if !name.isEmpty,
141-
let jsonData = try? JSONSerialization.data(withJSONObject: ["name": name], options: []),
142-
let json = String(data: jsonData, encoding: .utf8) {
143-
parameters["user_profile"] = json
142+
if !name.isEmpty {
143+
profile["name"] = name
144144
}
145145
}
146+
147+
if !profile.isEmpty, let jsonData = try? JSONSerialization.data(withJSONObject: profile, options: []),
148+
let json = String(data: jsonData, encoding: .utf8) {
149+
parameters["user_profile"] = json
150+
}
151+
146152
return self.tokenExchange(subjectToken: authorizationCode,
147153
subjectTokenType: "http://auth0.com/oauth/token-type/apple-authz-code",
148154
scope: scope,
@@ -412,8 +418,8 @@ private extension Auth0Authentication {
412418
return Request(session: session, url: url, method: "POST", handle: authenticationObject, payload: payload, logger: self.logger, telemetry: self.telemetry)
413419
}
414420

415-
func tokenExchange(subjectToken: String, subjectTokenType: String, scope: String?, audience: String?, parameters: [String: String]?) -> Request<Credentials, AuthenticationError> {
416-
var parameters: [String: String] = parameters ?? [:]
421+
func tokenExchange(subjectToken: String, subjectTokenType: String, scope: String?, audience: String?, parameters: [String: Any]?) -> Request<Credentials, AuthenticationError> {
422+
var parameters: [String: Any] = parameters ?? [:]
417423
parameters["grant_type"] = "urn:ietf:params:oauth:grant-type:token-exchange"
418424
parameters["subject_token"] = subjectToken
419425
parameters["subject_token_type"] = subjectTokenType

Auth0/Authentication.swift

+7-5
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,14 @@ public protocol Authentication: Trackable, Loggable {
256256
```
257257

258258
- parameter authCode: Authorization Code retrieved from Apple Authorization
259+
- parameter fullName: The full name property returned with the Apple ID Credentials
260+
- parameter profile: Additional user profile data returned with the Apple ID Credentials
259261
- parameter scope: Requested scope value when authenticating the user. By default is `openid profile offline_access`
260262
- parameter audience: API Identifier that the client is requesting access to
261-
- parameter fullName: The full name property returned with the Apple ID Credentials
262263

263264
- returns: a request that will yield Auth0 user's credentials
264265
*/
265-
func login(appleAuthorizationCode authorizationCode: String, fullName: PersonNameComponents?, scope: String?, audience: String?) -> Request<Credentials, AuthenticationError>
266+
func login(appleAuthorizationCode authorizationCode: String, fullName: PersonNameComponents?, profile: [String: Any]?, scope: String?, audience: String?) -> Request<Credentials, AuthenticationError>
266267

267268
/**
268269
Authenticate a user with their Facebook session info access token and profile data.
@@ -946,14 +947,15 @@ public extension Authentication {
946947
```
947948

948949
- parameter authCode: Authorization Code retrieved from Apple Authorization
950+
- parameter fullName: The full name property returned with the Apple ID Credentials
951+
- parameter profile: Additional user profile data returned with the Apple ID Credentials
949952
- parameter scope: Requested scope value when authenticating the user. By default is `openid profile offline_access`
950953
- parameter audience: API Identifier that the client is requesting access to
951-
- parameter fullName: The full name property returned with the Apple ID Credentials
952954

953955
- returns: a request that will yield Auth0 user's credentials
954956
*/
955-
func login(appleAuthorizationCode authorizationCode: String, fullName: PersonNameComponents? = nil, scope: String? = "openid profile offline_access", audience: String? = nil) -> Request<Credentials, AuthenticationError> {
956-
return self.login(appleAuthorizationCode: authorizationCode, fullName: fullName, scope: scope, audience: audience)
957+
func login(appleAuthorizationCode authorizationCode: String, fullName: PersonNameComponents? = nil, profile: [String: Any]? = nil, scope: String? = "openid profile offline_access", audience: String? = nil) -> Request<Credentials, AuthenticationError> {
958+
return self.login(appleAuthorizationCode: authorizationCode, fullName: fullName, profile: profile, scope: scope, audience: audience)
957959
}
958960

959961
/**

Auth0Tests/AuthenticationSpec.swift

+26
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,14 @@ class AuthenticationSpec: QuickSpec {
267267
"subject_token_type": "http://auth0.com/oauth/token-type/apple-authz-code"]) &&
268268
hasNoneOf(["user_profile"])
269269
) { _ in return authResponse(accessToken: AccessToken, idToken: IdToken) }.name = "Token Exchange Apple Success with missing user profile"
270+
271+
272+
stub(condition: isToken(Domain) && hasAtLeast([
273+
"grant_type": TokenExchangeGrantType,
274+
"subject_token": "VALIDNAMEANDPROFILECODE",
275+
"subject_token_type": "http://auth0.com/oauth/token-type/apple-authz-code"]) &&
276+
(hasAtLeast(["user_profile": "{\"name\":{\"firstName\":\"John\"},\"user_metadata\":{\"custom_key\":\"custom_value\"}}"]) || hasAtLeast(["user_profile": "{\"user_metadata\":{\"custom_key\":\"custom_value\"},\"name\":{\"firstName\":\"John\"}}"]))
277+
) { _ in return authResponse(accessToken: AccessToken, idToken: IdToken) }.name = "Token Exchange Apple Success with user profile"
270278
}
271279

272280
it("should exchange apple auth code for credentials") {
@@ -388,6 +396,24 @@ class AuthenticationSpec: QuickSpec {
388396
}
389397
}
390398
}
399+
400+
it("should exchange apple auth code for credentials with fullName and profile") {
401+
var fullName = PersonNameComponents()
402+
fullName.givenName = "John"
403+
fullName.familyName = nil
404+
fullName.middleName = "Ignored"
405+
let profile = ["user_metadata": ["custom_key": "custom_value"]]
406+
407+
waitUntil(timeout: Timeout) { done in
408+
auth.login(appleAuthorizationCode: "VALIDNAMEANDPROFILECODE",
409+
fullName: fullName,
410+
profile: profile)
411+
.start { result in
412+
expect(result).to(haveCredentials())
413+
done()
414+
}
415+
}
416+
}
391417
}
392418

393419
context("facebook") {

0 commit comments

Comments
 (0)