Skip to content

Commit

Permalink
Add batch create and invite (#85)
Browse files Browse the repository at this point in the history
* 1. Add batch create and invite
2. Clean user objects to correctly reflect reality

* Forgot new files
  • Loading branch information
slavikm authored Dec 9, 2023
1 parent efbd385 commit 38dd4d4
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 56 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-core</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>uk.org.webcompere</groupId>
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/descope/enums/BatchUserPasswordAlgorithm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.descope.enums;

import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;

public enum BatchUserPasswordAlgorithm {
BATCH_USER_PASSWORD_ALGORITHM_BCRYPT("bcrypt"),
BATCH_USER_PASSWORD_ALGORITHM_PBKDF2SHA1("pbkdf2sha1"),
BATCH_USER_PASSWORD_ALGORITHM_PBKDF2SHA256("pbkdf2sha256"),
BATCH_USER_PASSWORD_ALGORITHM_PBKDF2SHA512("pbkdf2sha512");

@Getter
@JsonValue
private final String value;

BatchUserPasswordAlgorithm(String value) {
this.value = value;
}
}
1 change: 1 addition & 0 deletions src/main/java/com/descope/literals/Routes.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public static class AuthEndPoints {
public static class ManagementEndPoints {
// User
public static final String CREATE_USER_LINK = "/v1/mgmt/user/create";
public static final String CREATE_USERS_BATCH_LINK = "/v1/mgmt/user/create/batch";
public static final String UPDATE_USER_LINK = "/v1/mgmt/user/update";
public static final String DELETE_USER_LINK = "/v1/mgmt/user/delete";
public static final String DELETE_ALL_TEST_USERS_LINK = "/v1/mgmt/user/test/delete/all";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.descope.model.user.request;

import com.descope.enums.BatchUserPasswordAlgorithm;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class BatchUserPasswordHashed {
BatchUserPasswordAlgorithm algorithm;
byte[] hash;
byte[] salt;
int iterations;
}
18 changes: 18 additions & 0 deletions src/main/java/com/descope/model/user/request/BatchUserRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.descope.model.user.request;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class BatchUserRequest extends UserRequest {
String loginId;
String password;
BatchUserPasswordHashed hashedPassword;
}
38 changes: 28 additions & 10 deletions src/main/java/com/descope/model/user/request/UserRequest.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,53 @@
package com.descope.model.user.request;


import static com.descope.utils.CollectionUtils.addIfNotNull;

import com.descope.model.auth.AssociatedTenant;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

@Data
@Builder
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class UserRequest {
String userId;
String loginId;
String email;
Boolean verifiedEmail;
String phone;
Boolean verifiedPhone;
String displayName;
String givenName;
String middleName;
String familyName;
List<String> roleNames;
List<AssociatedTenant> userTenants;
Map<String, Object> customAttributes;
String picture;
Boolean invite;
Boolean test;
String inviteUrl;
Boolean sendEmail;
@JsonProperty("sendSMS")
Boolean sendSMS;
List<String> additionalLoginIds;

public Map<String, Object> toMap() {
Map<String, Object> m = new HashMap<>();
addIfNotNull(m, "email", email);
addIfNotNull(m, "verifiedEmail", verifiedEmail);
addIfNotNull(m, "phone", phone);
addIfNotNull(m, "verifiedPhone", verifiedPhone);
addIfNotNull(m, "displayName", displayName);
addIfNotNull(m, "givenName", givenName);
addIfNotNull(m, "middleName", middleName);
addIfNotNull(m, "familyName", familyName);
addIfNotNull(m, "roleNames", roleNames);
addIfNotNull(m, "userTenants", userTenants);
addIfNotNull(m, "customAttributes", customAttributes);
addIfNotNull(m, "picture", picture);
addIfNotNull(m, "test", test);
addIfNotNull(m, "additionalLoginIds", additionalLoginIds);
return m;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.descope.model.user.response;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserFailedResponse {
String failure;
UserResponse user;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.descope.model.user.response;

import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UsersBatchResponse {
private List<UserResponse> createdUsers;
private List<UserFailedResponse> failedUsers;
}
3 changes: 2 additions & 1 deletion src/main/java/com/descope/proxy/impl/AbstractProxyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.descope.exception.RateLimitExceededException;
import com.descope.exception.ServerCommonException;
import com.descope.model.client.SdkInfo;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -136,7 +137,7 @@ private void addHeaders(ClassicHttpRequest req) {
protected <B, R> R post(URI uri, B body, Class<R> returnClz) {
final ClassicRequestBuilder builder = ClassicRequestBuilder.post(uri);
if (body != null) {
final ObjectMapper objectMapper = new ObjectMapper();
final ObjectMapper objectMapper = new ObjectMapper().setSerializationInclusion(Include.NON_NULL);
final byte[] payload = objectMapper.writeValueAsBytes(body);
builder.setEntity(new ByteArrayEntity(payload, ContentType.APPLICATION_JSON));
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/descope/sdk/mgmt/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.descope.enums.DeliveryMethod;
import com.descope.exception.DescopeException;
import com.descope.model.auth.InviteOptions;
import com.descope.model.user.request.BatchUserRequest;
import com.descope.model.user.request.UserRequest;
import com.descope.model.user.request.UserSearchRequest;
import com.descope.model.user.response.AllUsersResponseDetails;
Expand All @@ -11,6 +12,7 @@
import com.descope.model.user.response.OTPTestUserResponse;
import com.descope.model.user.response.ProviderTokenResponse;
import com.descope.model.user.response.UserResponseDetails;
import com.descope.model.user.response.UsersBatchResponse;
import java.util.List;
import java.util.Map;

Expand All @@ -30,6 +32,15 @@ public interface UserService {
*/
UserResponseDetails create(String loginId, UserRequest request) throws DescopeException;

/**
* Create users in batch.
*
* @param users the {@link List} of users
* @return {@link UsersBatchResponse} with successfully created users and failed users
* @throws DescopeException If there occurs any exception, a subtype of this exception will be thrown.
*/
UsersBatchResponse createBatch(List<BatchUserRequest> users) throws DescopeException;

/**
* Create a new test user. You can later generate OTP, Magic link and enchanted link to use in the
* test without the need of 3rd party messaging services. Those users are not counted as part of
Expand Down Expand Up @@ -63,6 +74,16 @@ public interface UserService {
*/
UserResponseDetails invite(String loginId, UserRequest request, InviteOptions options) throws DescopeException;

/**
* Create users in batch and invite them via an email / text message.
*
* @param users the {@link List} of users
* @param options Additional options for the invitation, such as invite URL.
* @return {@link UsersBatchResponse} with successfully created users and failed users
* @throws DescopeException If there occurs any exception, a subtype of this exception will be thrown.
*/
UsersBatchResponse inviteBatch(List<BatchUserRequest> users, InviteOptions options) throws DescopeException;

/**
* Update an existing user.
*
Expand Down
Loading

0 comments on commit 38dd4d4

Please sign in to comment.