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

Signup for NativeAuth #1949

Merged
merged 7 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion common
Submodule common updated 31 files
+66 −0 common/src/main/java/com/microsoft/identity/common/internal/commands/SignUpResendCodeCommand.kt
+66 −0 common/src/main/java/com/microsoft/identity/common/internal/commands/SignUpStartCommand.kt
+66 −0 common/src/main/java/com/microsoft/identity/common/internal/commands/SignUpSubmitCodeCommand.kt
+66 −0 common/src/main/java/com/microsoft/identity/common/internal/commands/SignUpSubmitPasswordCommand.kt
+66 −0 common/src/main/java/com/microsoft/identity/common/internal/commands/SignUpSubmitUserAttributesCommand.kt
+443 −0 common/src/main/java/com/microsoft/identity/common/internal/controllers/NativeAuthMsalController.kt
+54 −0 ...ain/com/microsoft/identity/common/java/commands/parameters/nativeauth/BaseSignUpStartCommandParameters.java
+39 −0 ...ain/com/microsoft/identity/common/java/commands/parameters/nativeauth/SignUpChallengeCommandParameters.java
+45 −0 ...main/com/microsoft/identity/common/java/commands/parameters/nativeauth/SignUpContinueCommandParameters.java
+45 −0 ...in/com/microsoft/identity/common/java/commands/parameters/nativeauth/SignUpResendCodeCommandParameters.java
+39 −0 ...rc/main/com/microsoft/identity/common/java/commands/parameters/nativeauth/SignUpStartCommandParameters.java
+45 −0 ...icrosoft/identity/common/java/commands/parameters/nativeauth/SignUpStartUsingPasswordCommandParameters.java
+45 −0 ...in/com/microsoft/identity/common/java/commands/parameters/nativeauth/SignUpSubmitCodeCommandParameters.java
+45 −0 ...om/microsoft/identity/common/java/commands/parameters/nativeauth/SignUpSubmitPasswordCommandParameters.java
+46 −0 ...rosoft/identity/common/java/commands/parameters/nativeauth/SignUpSubmitUserAttributesCommandParameters.java
+8 −3 common4j/src/main/com/microsoft/identity/common/java/controllers/results/INativeAuthCommandResult.kt
+109 −0 common4j/src/main/com/microsoft/identity/common/java/controllers/results/SignUpCommandResult.kt
+84 −0 common4j/src/main/com/microsoft/identity/common/java/providers/nativeauth/NativeAuthOAuth2Strategy.kt
+6 −0 common4j/src/main/com/microsoft/identity/common/java/providers/nativeauth/NativeAuthOAuth2StrategyFactory.kt
+112 −1 common4j/src/main/com/microsoft/identity/common/java/providers/nativeauth/NativeAuthRequestProvider.kt
+121 −0 common4j/src/main/com/microsoft/identity/common/java/providers/nativeauth/NativeAuthResponseHandler.kt
+163 −0 common4j/src/main/com/microsoft/identity/common/java/providers/nativeauth/interactors/SignUpInteractor.kt
+83 −0 .../src/main/com/microsoft/identity/common/java/providers/nativeauth/requests/signup/SignUpChallengeRequest.kt
+104 −0 ...j/src/main/com/microsoft/identity/common/java/providers/nativeauth/requests/signup/SignUpContinueRequest.kt
+91 −0 ...on4j/src/main/com/microsoft/identity/common/java/providers/nativeauth/requests/signup/SignUpStartRequest.kt
+168 −0 ...main/com/microsoft/identity/common/java/providers/nativeauth/responses/signup/SignUpChallengeApiResponse.kt
+75 −0 ...c/main/com/microsoft/identity/common/java/providers/nativeauth/responses/signup/SignUpChallengeApiResult.kt
+179 −0 .../main/com/microsoft/identity/common/java/providers/nativeauth/responses/signup/SignUpContinueApiResponse.kt
+108 −0 ...rc/main/com/microsoft/identity/common/java/providers/nativeauth/responses/signup/SignUpContinueApiResult.kt
+169 −0 ...src/main/com/microsoft/identity/common/java/providers/nativeauth/responses/signup/SignUpStartApiResponse.kt
+100 −0 ...j/src/main/com/microsoft/identity/common/java/providers/nativeauth/responses/signup/SignUpStartApiResult.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import com.microsoft.identity.client.exception.MsalClientException
import com.microsoft.identity.client.exception.MsalException
import com.microsoft.identity.client.statemachine.results.SignInResult
import com.microsoft.identity.client.statemachine.results.SignInUsingPasswordResult
import com.microsoft.identity.client.statemachine.results.SignUpResult
import com.microsoft.identity.client.statemachine.results.SignUpUsingPasswordResult
import com.microsoft.identity.client.statemachine.states.AccountResult

/**
Expand Down Expand Up @@ -105,4 +107,48 @@ interface INativeAuthPublicClientApplication : IPublicClientApplication {
* @throws MsalClientException if an account is already signed in.
*/
fun signInUsingPassword(username: String, password: CharArray, scopes: List<String>? = null, callback: NativeAuthPublicClientApplication.SignInUsingPasswordCallback)

/**
* Sign up the account starting from a username; Kotlin coroutines variant.
*
* @param username username of the account to sign up.
* @param attributes (Optional) user attributes to be used during account creation.
* @return [com.microsoft.identity.client.statemachine.results.SignUpResult] see detailed possible return state under the object.
* @throws MsalClientException if an account is already signed in.
*/
suspend fun signUp(username: String, attributes: UserAttributes? = null): SignUpResult

/**
* Sign up the account starting from a username; callback variant.
*
* @param username username of the account to sign up.
* @param attributes (Optional) user attributes to be used during account creation.
* @param callback [com.microsoft.identity.client.NativeAuthPublicClientApplication.SignUpCallback] to receive the result.
* @return [com.microsoft.identity.client.statemachine.results.SignUpResult] see detailed possible return state under the object.
* @throws MsalClientException if an account is already signed in.
*/
fun signUp(username: String, attributes: UserAttributes? = null, callback: NativeAuthPublicClientApplication.SignUpCallback)

/**
* Sign up the account using username and password. Kotlin coroutines variant.
*
* @param username username of the account to sign up.
* @param password password of the account to sign up.
* @param attributes (Optional) user attributes to be used during account creation
* @return [com.microsoft.identity.client.statemachine.results.SignUpUsingPasswordResult] see detailed possible return state under the object.
* @throws MsalClientException if an account is already signed in.
*/
suspend fun signUpUsingPassword(username: String, password: CharArray, attributes: UserAttributes? = null): SignUpUsingPasswordResult

/**
* Sign up the account using username and password; callback variant.
*
* @param username username of the account to sign up.
* @param password password of the account to sign up.
* @param attributes (Optional) user attributes to be used during account creation
* @param callback [com.microsoft.identity.client.NativeAuthPublicClientApplication.SignUpUsingPasswordCallback] to receive the result.
* @return [com.microsoft.identity.client.statemachine.results.SignUpUsingPasswordResult] see detailed possible return state under the object.
* @throws MsalClientException if an account is already signed in.
*/
fun signUpUsingPassword(username: String, password: CharArray, attributes: UserAttributes? = null, callback: NativeAuthPublicClientApplication.SignUpUsingPasswordCallback)
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) Microsoft Corporation.
// All rights reserved.
//
// This code is licensed under the MIT License.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package com.microsoft.identity.client

import com.microsoft.identity.common.java.providers.nativeauth.responses.UserAttributeApiResult

/**
* RequiredUserAttribute represents details about the account attributes required by the server.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure what we mean by these being required. There is a boolean flag here..so if require boolean was false then will these still be considered required?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These attributes are requested (required) by the server so they are called RequiredUserAttribute. The required field denotes whether it is mandatory for the attribute to be sent. The class names reflect the attribute names we get from the REST APIs.

Copy link
Contributor

Choose a reason for hiding this comment

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

So the boolean here will always be true. Honestly the naming here is too confusing. It sounds like the required boolean means something else but I'm not sure what it means

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree the naming is a bit confusing but it represents the attribute names from the REST API. I will talk to API folks to see if we can rename it. That change will be outside of this PR.

*/
data class RequiredUserAttribute(
//Name of the attribute
val attributeName: String?,

//Data type for the attribute
val type: String?,

//If the attribute is required
val required: Boolean?,

//Attribute value should match the constraints
val options: RequiredUserAttributeOptions?
)

internal fun List<UserAttributeApiResult>.toListOfRequiredUserAttribute(): List<RequiredUserAttribute> {
return this.map { it.toRequiredUserAttribute() }
}

internal fun UserAttributeApiResult.toRequiredUserAttribute(): RequiredUserAttribute {
Copy link
Contributor

Choose a reason for hiding this comment

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

Javadoc on these two methods

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

return RequiredUserAttribute(
attributeName = this.name,
type = this.type,
required = this.required,
options = this.options?.toListOfRequiredUserAttributeOptions()
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Microsoft Corporation.
// All rights reserved.
//
// This code is licensed under the MIT License.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package com.microsoft.identity.client

import com.microsoft.identity.common.java.providers.nativeauth.responses.UserAttributeOptionsApiResult

/**
* RequiredUserAttributeOptions contains the regular expression that the attribute value must match.
*/
data class RequiredUserAttributeOptions(
val regex: String?
)

internal fun List<UserAttributeOptionsApiResult>.toListOfRequiredUserAttributeOptions(): List<RequiredUserAttributeOptions> {
return this.map { it.toListOfRequiredUserAttributeOptions() }
}

fun UserAttributeOptionsApiResult.toListOfRequiredUserAttributeOptions(): RequiredUserAttributeOptions {
return RequiredUserAttributeOptions(
regex = regex
)
}
132 changes: 132 additions & 0 deletions msal/src/main/java/com/microsoft/identity/client/UserAttributes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.microsoft.identity.client
Copy link
Contributor

Choose a reason for hiding this comment

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

LICENSE

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added


import com.microsoft.identity.common.java.util.ObjectMapper

/**
* UserAttributes is a helper class for a client to provide user attributes required for signup
* operation in Native Auth
*/
class UserAttributes(internal val userAttributes: Map<String, String>) {
companion object Builder {
private const val CITY = "city"
private const val COUNTRY = "country"
private const val DISPLAY_NAME = "displayName"
private const val EMAIL_ADDRESS = "email"
private const val GIVEN_NAME = "givenName"
private const val JOB_TITLE = "jobTitle"
private const val POSTAL_CODE = "postalCode"
private const val STATE = "state"
private const val STREET_ADDRESS = "streetAddress"
private const val SURNAME = "surname"

private val userAttributes = mutableMapOf<String, String>()

/**
* Sets the city for user
* @param city: City for user
*/
fun city(city: String): Builder {
userAttributes[CITY] = city
return this
}

/**
* Sets the country for the user
* @param country: Country for the user
*/
fun country(country: String): Builder {
userAttributes[COUNTRY] = country
return this
}

/**
* Sets the name for display purposes for the user
* @param displayName: Display name for the user
*/
fun displayName(displayName: String): Builder {
userAttributes[DISPLAY_NAME] = displayName
return this
}

/**
* Sets the email address for the user
* @param emailAddress: Email address for the user
*/
fun emailAddress(emailAddress: String): Builder {
userAttributes[EMAIL_ADDRESS] = emailAddress
return this
}

/**
* Sets the given name for the user
* @param givenName: Given name for the user
*/
fun givenName(givenName: String): Builder {
userAttributes[GIVEN_NAME] = givenName
return this
}

/**
* Sets the job title for the user
* @param givenName: Given name for the user
*/
fun jobTitle(jobTitle: String): Builder {
userAttributes[JOB_TITLE] = jobTitle
return this
}

/**
* Sets the given name for the user
* @param givenName: Given name for the user
*/
fun postalCode(postalCode: String): Builder {
userAttributes[POSTAL_CODE] = postalCode
return this
}

/**
* Sets the state/province for the user
* @param state: State/province for the user
*/
fun state(state: String): Builder {
userAttributes[STATE] = state
return this
}

/**
* Sets the street address for the user
* @param streetAddress: Street address for the user
*/
fun streetAddress(streetAddress: String): Builder {
userAttributes[STREET_ADDRESS] = streetAddress
return this
}

/**
* Sets the surname for the user
* @param surname: Surname for the user
*/
fun surname(surname: String): Builder {
userAttributes[SURNAME] = surname
return this
}

/**
* Sets any custom attribute for the use
* @param key: Name of the attribute
* @param value: Attribute value
*/
fun customAttribute(key: String, value: String): Builder {
userAttributes[key] = value
return this
}

fun build(): UserAttributes {
return UserAttributes(userAttributes)
}
}
}

internal fun UserAttributes.toMap(): Map<String, String> {
return ObjectMapper.constructMapFromObject(userAttributes)
Copy link
Contributor

Choose a reason for hiding this comment

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

wasn't userAttributes already declared as a Map?

}
Loading