Skip to content

Commit

Permalink
change loginAs to return UserLoginResponse
Browse files Browse the repository at this point in the history
add apiUrl to config
  • Loading branch information
RazcoDev committed Jan 22, 2023
1 parent 07d72d0 commit 22ca90c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
6 changes: 5 additions & 1 deletion src/main/java/io/permit/sdk/PermitConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class PermitConfig {
// main config vars
private final String token;
private final String pdp;
private final String apiUrl;
private final Boolean debugMode;

// logger config
Expand All @@ -25,6 +26,7 @@ public class PermitConfig {
private PermitConfig(Builder builder) {
this.token = builder.token;
this.pdp = builder.pdp;
this.apiUrl = builder.apiUrl;
this.debugMode = builder.debugMode;
this.logLevel = builder.logLevel;
this.logLabel = builder.logLabel;
Expand All @@ -40,6 +42,7 @@ private PermitConfig(Builder builder) {
public String getToken() {
return token;
}
public String getApiUrl() { return apiUrl; }
public String getPdpAddress() {
return pdp;
}
Expand Down Expand Up @@ -74,7 +77,8 @@ public Boolean shouldUseDefaultTenantIfEmpty() {
public static class Builder {
// main config vars
private String token;
private String pdp = "http://localhost:7000";
private String pdp = "http://localhost:7766";
private String apiUrl = "https://api.permit.io";
private Boolean debugMode = false;

// logger config
Expand Down
28 changes: 19 additions & 9 deletions src/main/java/io/permit/sdk/api/ElementsClient.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
package io.permit.sdk.api;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.permit.sdk.PermitConfig;
import io.permit.sdk.api.models.*;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

interface IElementsApi {
UserLoginRequest loginAs(String userId, String tenantId) throws IOException, PermitApiException;
UserLoginResponse loginAs(String userId, String tenantId) throws IOException, PermitApiException;
}

public class ElementsClient implements IElementsApi {
final static int HTTP_404_NOT_FOUND = 404;

final static Logger logger = LoggerFactory.getLogger(ApiClient.class);
private final OkHttpClient client = new OkHttpClient();
private final PermitConfig config;
private final Headers headers;
private final String baseUrl;
private final String apiUrl;

public ElementsClient(PermitConfig config) {
this.config = config;
this.headers = new Headers.Builder()
.add("Content-Type", "application/json")
.add("Authorization", String.format("Bearer %s", this.config.getToken()))
.build();
this.baseUrl = this.config.getPdpAddress();
this.apiUrl = this.config.getApiUrl();
}

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

@Override
public UserLoginRequest loginAs(String userId, String tenantId) throws IOException, PermitApiException {
public UserLoginResponse loginAs(String userId, String tenantId) throws IOException, PermitApiException {
UserLoginRequest element = new UserLoginRequest();
element.tenantId = tenantId;
element.userId = userId;

// request body
Gson gson = new Gson();
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
String requestBody = gson.toJson(element);
RequestBody body = RequestBody.create(requestBody, MediaType.parse("application/json"));

// create the request
String url = String.format("%s/v2/auth/elements_login_as", this.baseUrl);
String url = String.format("%s/v2/auth/elements_login_as", this.config.getApiUrl());
Request request = new Request.Builder()
.url(url)
.headers(this.headers)
Expand All @@ -84,8 +87,15 @@ public UserLoginRequest loginAs(String userId, String tenantId) throws IOExcepti
}
String responseString = responseBody.string();
throwIfErrorResponseCode(requestRepr, response, responseString);
return gson.fromJson(responseString, UserLoginRequest.class);
UserLoginResponse userLoginResponse = gson.fromJson(responseString, UserLoginResponse.class);
userLoginResponse.content = new HashMap<>();
userLoginResponse.content.put("url", userLoginResponse.redirectUrl);
return userLoginResponse;
}
}

public String getApiUrl() {
return apiUrl;
}
}

11 changes: 11 additions & 0 deletions src/main/java/io/permit/sdk/api/models/UserLoginResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.permit.sdk.api.models;

import java.util.Map;

public class UserLoginResponse {
public String error = null;
public String token = null;
public String extra = null;
public String redirectUrl = null;
public Map<String, String> content = null;
}
30 changes: 26 additions & 4 deletions src/test/java/io/permit/sdk/PermitIntegrationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.google.common.base.Strings;
import com.google.gson.Gson;
import io.permit.sdk.api.PermitApiException;
import io.permit.sdk.api.models.UserLoginResponse;
import io.permit.sdk.api.models.UserModel;
import io.permit.sdk.enforcement.AssignedRole;
import io.permit.sdk.enforcement.Resource;
import io.permit.sdk.enforcement.User;
import okhttp3.HttpUrl;
Expand All @@ -15,7 +15,7 @@
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Random;

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

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

static Random rand = new Random();
static String suffixedUserKey = "test|" + rand.nextInt();

private final static String roleKey = "captain";
private final static String tenantKey = "tortuga";
private final static String userKey = "test|13d4dd3ff127";
private final static String userKey = suffixedUserKey;
private final static String userEmail = "[email protected]";
private final static String userFirstName = "Jack";
private final static String userLastName = "Sparrow";
Expand Down Expand Up @@ -90,7 +93,7 @@ private static void logTestIsStarting(String testName) {
Boolean allowed = null;
try {
allowed = permit.check(
User.fromString("55de594980944d48944dc10b9c70483c"),
User.fromString(userKey),
"create",
Resource.fromString("document")
);
Expand All @@ -101,6 +104,25 @@ private static void logTestIsStarting(String testName) {
assertTrue(allowed, "permit.check() should be true");
}

@Test void testPermitElementsLoginAs() {
if (skipTests) {
return;
}
logTestIsStarting("permitCheckSucceeds");
Permit permit = new Permit(this.config);
UserLoginResponse loginAs = null;
try {
loginAs = permit.elements.loginAs("[email protected]", "fafb66f9c98647ad954f129b9f2b1c84");
} catch (IOException e) {
fail(e);
} catch (PermitApiException e) {
e.printStackTrace();
}

assertNotNull(loginAs.redirectUrl);
assertNotNull(loginAs.content);
}

@Test void testPermitApiUserLifecycle() {
if (skipTests) {
return;
Expand Down

0 comments on commit 22ca90c

Please sign in to comment.