From 413e30954c5d11fec3df3b74a98fc6dc0f46ba76 Mon Sep 17 00:00:00 2001 From: Martin Walsh Date: Wed, 17 Jul 2019 11:32:37 +0100 Subject: [PATCH] Added support for root attributes when creating a new user (#287) * Added support for root attributes when creating a new user Added Tests * Review Tweaks --- Auth0/Auth0Authentication.swift | 7 ++++++- Auth0/Authentication.swift | 26 ++++++++++++++++++++++---- Auth0/SafariWebAuth.swift | 4 ++-- Auth0Tests/AuthenticationSpec.swift | 29 +++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 7 deletions(-) diff --git a/Auth0/Auth0Authentication.swift b/Auth0/Auth0Authentication.swift index fceb96b32..4ecc182d2 100644 --- a/Auth0/Auth0Authentication.swift +++ b/Auth0/Auth0Authentication.swift @@ -83,7 +83,7 @@ struct Auth0Authentication: Authentication { return Request(session: session, url: url, method: "POST", handle: authenticationObject, payload: payload, logger: self.logger, telemetry: self.telemetry) } - func createUser(email: String, username: String? = nil, password: String, connection: String, userMetadata: [String: Any]? = nil) -> Request { + func createUser(email: String, username: String? = nil, password: String, connection: String, userMetadata: [String: Any]? = nil, rootAttributes: [String: Any]? = nil) -> Request { var payload: [String: Any] = [ "email": email, "password": password, @@ -92,6 +92,11 @@ struct Auth0Authentication: Authentication { ] payload["username"] = username payload["user_metadata"] = userMetadata + if let rootAttributes = rootAttributes { + rootAttributes.forEach { (key, value) in + if payload[key] == nil { payload[key] = value } + } + } let createUser = URL(string: "/dbconnections/signup", relativeTo: self.url)! return Request(session: session, url: createUser, method: "POST", handle: databaseUser, payload: payload, logger: self.logger, telemetry: self.telemetry) diff --git a/Auth0/Authentication.swift b/Auth0/Authentication.swift index e2cebc241..eb612a428 100644 --- a/Auth0/Authentication.swift +++ b/Auth0/Authentication.swift @@ -155,7 +155,7 @@ public protocol Authentication: Trackable, Loggable { .start { print($0) } ``` - you can also add additional attributes when creating the user + you can also add additional metadata when creating the user ``` Auth0 @@ -178,10 +178,12 @@ public protocol Authentication: Trackable, Loggable { - parameter password: password for the new user - parameter connection: name where the user will be created (Database connection) - parameter userMetadata: additional userMetadata parameters that will be added to the newly created user. - + - parameter rootAttributes: root attributes that will be added to the newly created user. See https://auth0.com/docs/api/authentication#signup for supported attributes. Will not overwrite existing parameters. + - returns: request that will yield a created database user (just email, username and email verified flag) */ - func createUser(email: String, username: String?, password: String, connection: String, userMetadata: [String: Any]?) -> Request + // swiftlint:disable:next function_parameter_count + func createUser(email: String, username: String?, password: String, connection: String, userMetadata: [String: Any]?, rootAttributes: [String: Any]?) -> Request /** Resets a Database user password @@ -632,11 +634,27 @@ public extension Authentication { - parameter password: password for the new user - parameter connection: name where the user will be created (Database connection) - parameter userMetadata: additional userMetadata parameters that will be added to the newly created user. + - parameter rootAttributes: root attributes that will be added to the newly created user. See https://auth0.com/docs/api/authentication#signup for supported attributes. Will not overwrite existing parameters. + + - returns: request that will yield a created database user (just email, username and email verified flag) + */ + func createUser(email: String, username: String? = nil, password: String, connection: String, userMetadata: [String: Any]? = nil, rootAttributes: [String: Any]? = nil) -> Request { + return self.createUser(email: email, username: username, password: password, connection: connection, userMetadata: userMetadata, rootAttributes: rootAttributes) + } + /** + Creates a user in a Database connection + + - parameter email: email of the user to create + - parameter username: username of the user if the connection requires username. By default is 'nil' + - parameter password: password for the new user + - parameter connection: name where the user will be created (Database connection) + - parameter userMetadata: additional userMetadata parameters that will be added to the newly created user. + - returns: request that will yield a created database user (just email, username and email verified flag) */ func createUser(email: String, username: String? = nil, password: String, connection: String, userMetadata: [String: Any]? = nil) -> Request { - return self.createUser(email: email, username: username, password: password, connection: connection, userMetadata: userMetadata) + return self.createUser(email: email, username: username, password: password, connection: connection, userMetadata: userMetadata, rootAttributes: nil) } /** diff --git a/Auth0/SafariWebAuth.swift b/Auth0/SafariWebAuth.swift index d25bd0925..8b9829ac3 100644 --- a/Auth0/SafariWebAuth.swift +++ b/Auth0/SafariWebAuth.swift @@ -239,8 +239,8 @@ private func generateDefaultState() -> String? { var data = Data(count: 32) var tempData = data - let result = tempData.withUnsafeMutableBytes { (bytes: UnsafeMutablePointer) -> Int in - return Int(SecRandomCopyBytes(kSecRandomDefault, data.count, bytes)) + let result = tempData.withUnsafeMutableBytes { + SecRandomCopyBytes(kSecRandomDefault, data.count, $0.baseAddress!) } guard result == 0 else { return nil } diff --git a/Auth0Tests/AuthenticationSpec.swift b/Auth0Tests/AuthenticationSpec.swift index d9278f826..cee5c4862 100644 --- a/Auth0Tests/AuthenticationSpec.swift +++ b/Auth0Tests/AuthenticationSpec.swift @@ -415,6 +415,35 @@ class AuthenticationSpec: QuickSpec { } } } + + context("root attributes") { + + it("should send root attributes") { + let attributes = ["family_name": "Doe", + "nickname" : "Johnny"] + stub(condition: isSignUp(Domain) && hasAtLeast(attributes)) { _ in return createdUser(email: SupportAtAuth0) }.name = "User w/root attributes" + waitUntil(timeout: Timeout) { done in + auth.createUser(email: SupportAtAuth0, password: ValidPassword, connection: ConnectionName, rootAttributes: attributes).start { result in + expect(result).to(haveCreatedUser(SupportAtAuth0)) + done() + } + } + } + + it("should send root attributes but not overwrite existing email") { + let attributes = ["family_name": "Doe", + "nickname" : "Johnny", + "email" : "root@email.com"] + stub(condition: isSignUp(Domain) && !hasAtLeast(attributes)) { _ in return createdUser(email: SupportAtAuth0) }.name = "User w/root attributes" + waitUntil(timeout: Timeout) { done in + auth.createUser(email: SupportAtAuth0, password: ValidPassword, connection: ConnectionName, rootAttributes: attributes).start { result in + expect(result).to(haveCreatedUser(SupportAtAuth0)) + done() + } + } + } + + } }