Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for root attributes when creating a new user #287

Merged
merged 2 commits into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Auth0/Auth0Authentication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<DatabaseUser, AuthenticationError> {
func createUser(email: String, username: String? = nil, password: String, connection: String, userMetadata: [String: Any]? = nil, rootAttributes: [String: Any]? = nil) -> Request<DatabaseUser, AuthenticationError> {
var payload: [String: Any] = [
"email": email,
"password": password,
Expand All @@ -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 }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this behavior should be clarified in the method documentation

Copy link
Member Author

@cocojoe cocojoe Jul 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems fair, it was intended to stop abuse like overwriting of core params

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's no abuse IMHO. The user should be free to send whatever they want as "additional params". If they send something we already specify, that's their fault.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you in principal.

}
}

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)
Expand Down
26 changes: 22 additions & 4 deletions Auth0/Authentication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
lbalmaceda marked this conversation as resolved.
Show resolved Hide resolved

```
Auth0
Expand All @@ -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<DatabaseUser, AuthenticationError>
// swiftlint:disable:next function_parameter_count
func createUser(email: String, username: String?, password: String, connection: String, userMetadata: [String: Any]?, rootAttributes: [String: Any]?) -> Request<DatabaseUser, AuthenticationError>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protocols' methods can be changed without breaking users? this is a public one.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like an optional argument, and it's placed at the end of the parameter list so I'm not sure it would be a breaking change.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The argument is optional so in use you don't need to specify it. However, Lucho is correct any class that implements the protocol still has to support the behaviour. As the Protocol is Public then we should try to avoid breaking it.

I'll add support for this in the Public Extension so the original method will still work out of the box as the method in the Public Extension is a fallback.


/**
Resets a Database user password
Expand Down Expand Up @@ -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<DatabaseUser, AuthenticationError> {
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<DatabaseUser, AuthenticationError> {
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)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Auth0/SafariWebAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ private func generateDefaultState() -> String? {
var data = Data(count: 32)
var tempData = data

let result = tempData.withUnsafeMutableBytes { (bytes: UnsafeMutablePointer<UInt8>) -> Int in
return Int(SecRandomCopyBytes(kSecRandomDefault, data.count, bytes))
let result = tempData.withUnsafeMutableBytes {
SecRandomCopyBytes(kSecRandomDefault, data.count, $0.baseAddress!)
lbalmaceda marked this conversation as resolved.
Show resolved Hide resolved
}

guard result == 0 else { return nil }
Expand Down
29 changes: 29 additions & 0 deletions Auth0Tests/AuthenticationSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
lbalmaceda marked this conversation as resolved.
Show resolved Hide resolved
waitUntil(timeout: Timeout) { done in
auth.createUser(email: SupportAtAuth0, password: ValidPassword, connection: ConnectionName, rootAttributes: attributes).start { result in
expect(result).to(haveCreatedUser(SupportAtAuth0))
done()
}
}
}

}

}

Expand Down