Skip to content

Commit 22ca90c

Browse files
committed
change loginAs to return UserLoginResponse
add apiUrl to config
1 parent 07d72d0 commit 22ca90c

4 files changed

Lines changed: 61 additions & 14 deletions

File tree

src/main/java/io/permit/sdk/PermitConfig.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public class PermitConfig {
66
// main config vars
77
private final String token;
88
private final String pdp;
9+
private final String apiUrl;
910
private final Boolean debugMode;
1011

1112
// logger config
@@ -25,6 +26,7 @@ public class PermitConfig {
2526
private PermitConfig(Builder builder) {
2627
this.token = builder.token;
2728
this.pdp = builder.pdp;
29+
this.apiUrl = builder.apiUrl;
2830
this.debugMode = builder.debugMode;
2931
this.logLevel = builder.logLevel;
3032
this.logLabel = builder.logLabel;
@@ -40,6 +42,7 @@ private PermitConfig(Builder builder) {
4042
public String getToken() {
4143
return token;
4244
}
45+
public String getApiUrl() { return apiUrl; }
4346
public String getPdpAddress() {
4447
return pdp;
4548
}
@@ -74,7 +77,8 @@ public Boolean shouldUseDefaultTenantIfEmpty() {
7477
public static class Builder {
7578
// main config vars
7679
private String token;
77-
private String pdp = "http://localhost:7000";
80+
private String pdp = "http://localhost:7766";
81+
private String apiUrl = "https://api.permit.io";
7882
private Boolean debugMode = false;
7983

8084
// logger config

src/main/java/io/permit/sdk/api/ElementsClient.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
11
package io.permit.sdk.api;
22

3+
import com.google.gson.FieldNamingPolicy;
34
import com.google.gson.Gson;
5+
import com.google.gson.GsonBuilder;
46
import io.permit.sdk.PermitConfig;
57
import io.permit.sdk.api.models.*;
68
import okhttp3.*;
79
import org.slf4j.Logger;
810
import org.slf4j.LoggerFactory;
911

1012
import java.io.IOException;
13+
import java.util.HashMap;
1114
import java.util.List;
1215

1316
interface IElementsApi {
14-
UserLoginRequest loginAs(String userId, String tenantId) throws IOException, PermitApiException;
17+
UserLoginResponse loginAs(String userId, String tenantId) throws IOException, PermitApiException;
1518
}
1619

1720
public class ElementsClient implements IElementsApi {
18-
final static int HTTP_404_NOT_FOUND = 404;
19-
2021
final static Logger logger = LoggerFactory.getLogger(ApiClient.class);
2122
private final OkHttpClient client = new OkHttpClient();
2223
private final PermitConfig config;
2324
private final Headers headers;
24-
private final String baseUrl;
25+
private final String apiUrl;
2526

2627
public ElementsClient(PermitConfig config) {
2728
this.config = config;
2829
this.headers = new Headers.Builder()
2930
.add("Content-Type", "application/json")
3031
.add("Authorization", String.format("Bearer %s", this.config.getToken()))
3132
.build();
32-
this.baseUrl = this.config.getPdpAddress();
33+
this.apiUrl = this.config.getApiUrl();
3334
}
3435

3536
private void throwIfErrorResponseCode(String requestRepr, Response response, String responseContent, List<Integer> expectedErrorCodes) throws PermitApiException {
@@ -55,18 +56,20 @@ private void throwIfErrorResponseCode(String requestRepr, Response response, Str
5556
}
5657

5758
@Override
58-
public UserLoginRequest loginAs(String userId, String tenantId) throws IOException, PermitApiException {
59+
public UserLoginResponse loginAs(String userId, String tenantId) throws IOException, PermitApiException {
5960
UserLoginRequest element = new UserLoginRequest();
6061
element.tenantId = tenantId;
6162
element.userId = userId;
6263

6364
// request body
64-
Gson gson = new Gson();
65+
Gson gson = new GsonBuilder()
66+
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
67+
.create();
6568
String requestBody = gson.toJson(element);
6669
RequestBody body = RequestBody.create(requestBody, MediaType.parse("application/json"));
6770

6871
// create the request
69-
String url = String.format("%s/v2/auth/elements_login_as", this.baseUrl);
72+
String url = String.format("%s/v2/auth/elements_login_as", this.config.getApiUrl());
7073
Request request = new Request.Builder()
7174
.url(url)
7275
.headers(this.headers)
@@ -84,8 +87,15 @@ public UserLoginRequest loginAs(String userId, String tenantId) throws IOExcepti
8487
}
8588
String responseString = responseBody.string();
8689
throwIfErrorResponseCode(requestRepr, response, responseString);
87-
return gson.fromJson(responseString, UserLoginRequest.class);
90+
UserLoginResponse userLoginResponse = gson.fromJson(responseString, UserLoginResponse.class);
91+
userLoginResponse.content = new HashMap<>();
92+
userLoginResponse.content.put("url", userLoginResponse.redirectUrl);
93+
return userLoginResponse;
8894
}
8995
}
96+
97+
public String getApiUrl() {
98+
return apiUrl;
99+
}
90100
}
91101

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.permit.sdk.api.models;
2+
3+
import java.util.Map;
4+
5+
public class UserLoginResponse {
6+
public String error = null;
7+
public String token = null;
8+
public String extra = null;
9+
public String redirectUrl = null;
10+
public Map<String, String> content = null;
11+
}

src/test/java/io/permit/sdk/PermitIntegrationTests.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import com.google.common.base.Strings;
44
import com.google.gson.Gson;
55
import io.permit.sdk.api.PermitApiException;
6+
import io.permit.sdk.api.models.UserLoginResponse;
67
import io.permit.sdk.api.models.UserModel;
7-
import io.permit.sdk.enforcement.AssignedRole;
88
import io.permit.sdk.enforcement.Resource;
99
import io.permit.sdk.enforcement.User;
1010
import okhttp3.HttpUrl;
@@ -15,7 +15,7 @@
1515
import java.io.IOException;
1616
import java.net.InetSocketAddress;
1717
import java.net.Socket;
18-
import java.util.ArrayList;
18+
import java.util.Random;
1919

2020
import static org.junit.jupiter.api.Assertions.*;
2121

@@ -32,9 +32,12 @@ class PermitIntegrationTests {
3232
private final static int loggerSeparatorLength = 80;
3333
private boolean skipTests = false;
3434

35+
static Random rand = new Random();
36+
static String suffixedUserKey = "test|" + rand.nextInt();
37+
3538
private final static String roleKey = "captain";
3639
private final static String tenantKey = "tortuga";
37-
private final static String userKey = "test|13d4dd3ff127";
40+
private final static String userKey = suffixedUserKey;
3841
private final static String userEmail = "jack@pirates.com";
3942
private final static String userFirstName = "Jack";
4043
private final static String userLastName = "Sparrow";
@@ -90,7 +93,7 @@ private static void logTestIsStarting(String testName) {
9093
Boolean allowed = null;
9194
try {
9295
allowed = permit.check(
93-
User.fromString("55de594980944d48944dc10b9c70483c"),
96+
User.fromString(userKey),
9497
"create",
9598
Resource.fromString("document")
9699
);
@@ -101,6 +104,25 @@ private static void logTestIsStarting(String testName) {
101104
assertTrue(allowed, "permit.check() should be true");
102105
}
103106

107+
@Test void testPermitElementsLoginAs() {
108+
if (skipTests) {
109+
return;
110+
}
111+
logTestIsStarting("permitCheckSucceeds");
112+
Permit permit = new Permit(this.config);
113+
UserLoginResponse loginAs = null;
114+
try {
115+
loginAs = permit.elements.loginAs("raz@permit.io", "fafb66f9c98647ad954f129b9f2b1c84");
116+
} catch (IOException e) {
117+
fail(e);
118+
} catch (PermitApiException e) {
119+
e.printStackTrace();
120+
}
121+
122+
assertNotNull(loginAs.redirectUrl);
123+
assertNotNull(loginAs.content);
124+
}
125+
104126
@Test void testPermitApiUserLifecycle() {
105127
if (skipTests) {
106128
return;

0 commit comments

Comments
 (0)