Skip to content

Commit

Permalink
Password settings and other minor fixes (#95)
Browse files Browse the repository at this point in the history
* Get and set password settings for project and tenants

* pom version
  • Loading branch information
slavikm authored Feb 7, 2024
1 parent af528c1 commit 94e6f0c
Show file tree
Hide file tree
Showing 31 changed files with 678 additions and 39 deletions.
2 changes: 1 addition & 1 deletion examples/management-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<dependency>
<groupId>com.descope</groupId>
<artifactId>java-sdk</artifactId>
<version>1.0.12</version>
<version>1.0.13</version>
</dependency>
<dependency>
<groupId>info.picocli</groupId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.descope</groupId>
<artifactId>java-sdk</artifactId>
<modelVersion>4.0.0</modelVersion>
<version>1.0.12</version>
<version>1.0.13</version>
<name>${project.groupId}:${project.artifactId}</name>
<description>Java library used to integrate with Descope.</description>
<url>https://github.com/descope/descope-java</url>
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/descope/exception/ClientFunctionalException.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@ protected ClientFunctionalException(String message, String code) {
setCode(code);
}

protected ClientFunctionalException(String message, String code, Throwable cause) {
super(message, cause);
setCode(code);
}

public static ClientFunctionalException invalidToken() {
String message = "Invalid Token";
return new ClientFunctionalException(message, INVALID_TOKEN);
}

public static ClientFunctionalException invalidToken(Throwable cause) {
String message = "Invalid Token";
return new ClientFunctionalException(message, INVALID_TOKEN);
}
}
12 changes: 11 additions & 1 deletion src/main/java/com/descope/literals/Routes.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public static class AuthEndPoints {
public static final String LOG_OUT_LINK = "/v1/auth/logout";
public static final String LOG_OUT_ALL_LINK = "/v1/auth/logoutall";

// My details
public static final String ME_LINK = "/v1/auth/me";
public static final String HISTORY_LINK = "/v1/auth/me/history";

// MagicLink
public static final String SIGN_IN_MAGIC_LINK = "/v1/auth/magiclink/signin";
public static final String SIGN_UP_MAGIC_LINK = "/v1/auth/magiclink/signup";
Expand Down Expand Up @@ -102,14 +106,17 @@ public static class ManagementEndPoints {
public static final String USER_SET_PASSWORD_LINK = "/v1/mgmt/user/password/set";
public static final String USER_EXPIRE_PASSWORD_LINK = "/v1/mgmt/user/password/expire";
public static final String USER_CREATE_EMBEDDED_LINK = "/v1/mgmt/user/signin/embeddedlink";
public static final String USER_HISTORY_LINK = "/v1/mgmt/user/history";

// Tenant
public static final String CREATE_TENANT_LINK = "/v1/mgmt/tenant/create";
public static final String UPDATE_TENANT_LINK = "/v1/mgmt/tenant/update";
public static final String DELETE_TENANT_LINK = "/v1/mgmt/tenant/delete";
public static final String LOAD_TENANT_LINK = "/v1/mgmt/tenant";
public static final String LOAD_ALL_TENANTS_LINK = "/v1/mgmt/tenant/all";
public static final String TENANT_SEARCH_ALL_LINK = "/v1/mgmt/tenant/search";

public static final String GET_TENANT_SETTINGS_LINK = "/v1/mgmt/tenant/settings";

// SSO
public static final String SSO_GET_SETTINGS_LINK = "/mgmt/sso/settings";
public static final String SSO_DELETE_SETTINGS_LINK = "/mgmt/sso/settings";
Expand Down Expand Up @@ -179,5 +186,8 @@ public static class ManagementEndPoints {
public static final String MANAGEMENT_AUTHZ_RE_TARGETS = "/v1/mgmt/authz/re/targets";
public static final String MANAGEMENT_AUTHZ_RE_TARGET_ALL = "/v1/mgmt/authz/re/targetall";
public static final String MANAGEMENT_AUTHZ_GET_MODIFIED = "/v1/mgmt/authz/getmodified";

// Password settings
public static final String MANAGEMENT_PASSWORD_SETTINGS = "/v1/mgmt/password/settings";
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/descope/model/mgmt/AccessKeyRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
@Data
@Builder
public class AccessKeyRequest {

private String name;
private long expireTime;
private List<String> roleNames;
private List<Map<String, Object>> keyTenants;
private String userId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ public class AccessKeyResponseDetails {
private long expireTime;
private String createdBy;
private String clientId;
private String userId;
}
2 changes: 2 additions & 0 deletions src/main/java/com/descope/model/mgmt/ManagementServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.descope.sdk.mgmt.FlowService;
import com.descope.sdk.mgmt.GroupService;
import com.descope.sdk.mgmt.JwtService;
import com.descope.sdk.mgmt.PasswordSettingsService;
import com.descope.sdk.mgmt.PermissionService;
import com.descope.sdk.mgmt.ProjectService;
import com.descope.sdk.mgmt.RolesService;
Expand All @@ -30,4 +31,5 @@ public class ManagementServices {
AuditService auditService;
AuthzService authzService;
ProjectService projectService;
PasswordSettingsService passwordSettingsService;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.descope.model.passwordsettings;

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

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PasswordSettings {
private Boolean enabled;
private Integer minLength;
private Boolean lowercase;
private Boolean uppercase;
private Boolean number;
private Boolean nonAlphanumeric;
private Boolean expiration;
private Integer expirationWeeks;
private Boolean reuse;
private Integer reuseAmount;
private Boolean lock;
private Integer lockAttempts;
}
2 changes: 2 additions & 0 deletions src/main/java/com/descope/model/tenant/Tenant.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public class Tenant {
String name;
List<String> selfProvisioningDomains;
Map<String, Object> customAttributes;
String authType;
List<String> domains;
}
26 changes: 26 additions & 0 deletions src/main/java/com/descope/model/tenant/TenantSettings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.descope.model.tenant;

import com.fasterxml.jackson.annotation.JsonAlias;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TenantSettings {
List<String> domains;
List<String> selfProvisioningDomains;
@JsonAlias({"enabled"})
Boolean sessionSettingsEnabled;
Integer refreshTokenExpiration;
String refreshTokenExpirationUnit;
Integer sessionTokenExpiration;
String sessionTokenExpirationUnit;
Boolean enableInactivity;
Integer inactivityTime;
String inactivityTimeUnit;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.descope.model.user.response;

import java.time.Instant;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserHistoryResponse {
String userId;
Integer loginTime;
String city;
String country;
String ip;

public Instant getLoginTimeInstant() {
return Instant.ofEpochSecond(loginTime);
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/descope/proxy/ApiProxy.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.descope.proxy;

import com.fasterxml.jackson.core.type.TypeReference;
import java.net.URI;

public interface ApiProxy {
<R> R get(URI uri, Class<R> returnClz);

<R> R getArray(URI uri, TypeReference<R> typeReference);

<B, R> R post(URI uri, B body, Class<R> returnClz);

<B, R> R postAndGetArray(URI uri, B body, TypeReference<R> typeReference);

<B, R> R delete(URI uri, B body, Class<R> returnClz);
}
29 changes: 24 additions & 5 deletions src/main/java/com/descope/proxy/impl/AbstractProxyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.descope.exception.ServerCommonException;
import com.descope.model.client.SdkInfo;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -61,11 +62,12 @@ long getRetryHeader(final ClassicHttpResponse res) {
}

@SneakyThrows
<B, R> R exchange(ClassicHttpRequest req, Class<R> returnClz) {
<B, R> R exchange(ClassicHttpRequest req, Class<R> returnClz, TypeReference<R> typeReference) {
addHeaders(req);
log.debug(String.format("Sending %s request to %s", req.getMethod(), req.getRequestUri()));
try (final CloseableHttpClient httpClient = HttpClients.createDefault()) {
return httpClient.execute(req, new HttpClientResponseHandler<R>() {
@SuppressWarnings("resource")
@Override
public R handleResponse(ClassicHttpResponse response) throws HttpException, IOException {
try (final ClassicHttpResponse res = response) {
Expand Down Expand Up @@ -102,7 +104,9 @@ public R handleResponse(ClassicHttpResponse response) throws HttpException, IOEx
bs.toString(), String.valueOf(res.getCode()), bs.toString());
}
}
R r = objectMapper.readValue(tee, returnClz);
R r = returnClz != null
? objectMapper.readValue(tee, returnClz)
: objectMapper.readValue(tee, typeReference);
if (log.isDebugEnabled()) {
String resStr = bs.toString();
log.debug(String.format("Received response %s",
Expand Down Expand Up @@ -145,14 +149,29 @@ protected <B, R> R post(URI uri, B body, Class<R> returnClz) {
final byte[] payload = objectMapper.writeValueAsBytes(body);
builder.setEntity(new ByteArrayEntity(payload, ContentType.APPLICATION_JSON));
}
return exchange(builder.build(), returnClz);
return exchange(builder.build(), returnClz, null);
}

@SneakyThrows
protected <B, R> R post(URI uri, B body, TypeReference<R> typeReference) {
final ClassicRequestBuilder builder = ClassicRequestBuilder.post(uri);
if (body != null) {
final ObjectMapper objectMapper = new ObjectMapper().setSerializationInclusion(Include.NON_NULL);
final byte[] payload = objectMapper.writeValueAsBytes(body);
builder.setEntity(new ByteArrayEntity(payload, ContentType.APPLICATION_JSON));
}
return exchange(builder.build(), null, typeReference);
}

protected <R> R get(URI uri, Class<R> returnClz) {
return exchange(ClassicRequestBuilder.get(uri).build(), returnClz);
return exchange(ClassicRequestBuilder.get(uri).build(), returnClz, null);
}

protected <R> R get(URI uri, TypeReference<R> typeReference) {
return exchange(ClassicRequestBuilder.get(uri).build(), null, typeReference);
}

protected <B, R> R delete(URI uri, B body, Class<R> returnClz) {
return exchange(ClassicRequestBuilder.delete(uri).build(), returnClz);
return exchange(ClassicRequestBuilder.delete(uri).build(), returnClz, null);
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/descope/proxy/impl/ApiProxyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.descope.model.client.SdkInfo;
import com.descope.proxy.ApiProxy;
import com.fasterxml.jackson.core.type.TypeReference;
import java.net.URI;
import java.util.function.Supplier;

Expand All @@ -21,11 +22,21 @@ public <B, R> R post(URI uri, B body, Class<R> returnClz) {
return super.post(uri, body, returnClz);
}

@Override
public <B, R> R postAndGetArray(URI uri, B body, TypeReference<R> typeReference) {
return super.post(uri, body, typeReference);
}

@Override
public <R> R get(URI uri, Class<R> returnClz) {
return super.get(uri, returnClz);
}

@Override
public <R> R getArray(URI uri, TypeReference<R> typeReference) {
return super.get(uri, typeReference);
}

@Override
public <B, R> R delete(URI uri, B body, Class<R> returnClz) {
return super.delete(uri, body, returnClz);
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/descope/sdk/auth/AuthenticationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.descope.exception.DescopeException;
import com.descope.model.jwt.Token;
import com.descope.model.user.response.UserHistoryResponse;
import com.descope.model.user.response.UserResponse;
import java.util.List;

public interface AuthenticationService {
Expand Down Expand Up @@ -204,4 +206,22 @@ boolean validatePermissions(Token token, String tenant, List<String> permissions
* @throws DescopeException if there is an error
*/
void logoutAll(String refreshToken) throws DescopeException;

/**
* Use to retrieve current session user details. The request requires a valid refresh token.
*
* @param refreshToken a valid refresh token
* @return {@link UserResponse} returns the user details.
* @throws DescopeException if there is an error or token is not valid
*/
UserResponse me(String refreshToken) throws DescopeException;

/**
* Use to retrieve current session user history. The request requires a valid refresh token.
*
* @param refreshToken a valid refresh token
* @return {@link UserHistoryResponse} returns the user authentication history.
* @throws DescopeException if there is an error or token is not valid
*/
List<UserHistoryResponse> history(String refreshToken) throws DescopeException;
}
Loading

0 comments on commit 94e6f0c

Please sign in to comment.