-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from permitio/raz/per-3687-publish-java-sdk-wit…
…h-elements Raz/per 3687 publish java sdk with elements
- Loading branch information
Showing
6 changed files
with
152 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
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 { | ||
UserLoginResponse loginAs(String userId, String tenantId) throws IOException, PermitApiException; | ||
} | ||
|
||
public class ElementsClient implements IElementsApi { | ||
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 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.apiUrl = this.config.getApiUrl(); | ||
} | ||
|
||
private void throwIfErrorResponseCode(String requestRepr, Response response, String responseContent, List<Integer> expectedErrorCodes) throws PermitApiException { | ||
String log = String.format("Received response: %s : status code %d : %s", requestRepr, response.code(), responseContent); | ||
if (!response.isSuccessful() && this.config.isDebugMode()) { | ||
this.logger.error(log); | ||
} else { | ||
this.logger.debug(log); | ||
} | ||
if (!response.isSuccessful() && !expectedErrorCodes.contains(response.code())) { | ||
throw new PermitApiException( | ||
String.format( | ||
"unexpected status code: %d for request: %s", | ||
response.code(), | ||
requestRepr | ||
) | ||
); | ||
} | ||
} | ||
|
||
private void throwIfErrorResponseCode(String requestRepr, Response response, String responseContent) throws PermitApiException { | ||
throwIfErrorResponseCode(requestRepr, response, responseContent, List.of()); | ||
} | ||
|
||
@Override | ||
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 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.config.getApiUrl()); | ||
Request request = new Request.Builder() | ||
.url(url) | ||
.headers(this.headers) | ||
.post(body) | ||
.build(); | ||
|
||
String requestRepr = String.format("permit.elements.login_as(%s)", requestBody); | ||
this.logger.debug(String.format("Sending request: %s", requestRepr)); | ||
|
||
// send the request | ||
try (Response response = client.newCall(request).execute()) { | ||
ResponseBody responseBody = response.body(); | ||
if (responseBody == null) { | ||
throw new IOException("got empty response"); | ||
} | ||
String responseString = responseBody.string(); | ||
throwIfErrorResponseCode(requestRepr, response, responseString); | ||
UserLoginResponse userLoginResponse = gson.fromJson(responseString, UserLoginResponse.class); | ||
userLoginResponse.content = new HashMap<>(); | ||
userLoginResponse.content.put("url", userLoginResponse.redirectUrl); | ||
return userLoginResponse; | ||
} | ||
} | ||
|
||
public String getApiUrl() { | ||
return apiUrl; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package io.permit.sdk.api.models; | ||
|
||
public class UserLoginRequest { | ||
public String userId = null; | ||
public String tenantId = null; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/io/permit/sdk/api/models/UserLoginResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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.*; | ||
|
||
|
@@ -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"; | ||
|
@@ -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") | ||
); | ||
|
@@ -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; | ||
|