Skip to content

Commit

Permalink
Issue #1066
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-stastny committed May 31, 2024
1 parent 5ee866d commit 2b4e968
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.json.JSONObject;

import cz.incad.kramerius.utils.StringUtils;
import cz.incad.kramerius.utils.conf.KConfiguration;

public class ClientKeycloakConfig {

Expand Down Expand Up @@ -36,7 +37,12 @@ public String getSecret() {
return secret;
}

public String loginKeycloak(String redirectUrl) {
public String loginKeycloak(String redirectUrl, String type) {

if (type == null || !StringUtils.isAnyString(type)) {
type = KConfiguration.getInstance().getConfiguration().getString("default.eduid.type","all");;
}

StringBuilder builder = new StringBuilder();
builder.append(this.authServer);
if (!builder.toString().endsWith("/")) builder.append("/");
Expand All @@ -47,6 +53,11 @@ public String loginKeycloak(String redirectUrl) {
builder.append("&redirect_uri="+redirectUrl);
}
builder.append("&response_type=code");

if (type!= null) {
builder.append("#"+type);
}

return builder.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,13 @@ public Response changePassword(JSONObject rawdata) {

@GET
@Path("auth/login")
public Response login(@QueryParam("redirect_uri") String redirectUri) {

public Response login(@QueryParam("redirect_uri") String redirectUri, @QueryParam("type") String type) {
try {
String path = WORKING_DIR + "/keycloak.json";
String str = IOUtils.toString(new FileInputStream(path),"UTF-8");
ClientKeycloakConfig cnf = ClientKeycloakConfig.load(new JSONObject(str));
URI uri = URI.create(cnf.loginKeycloak(redirectUri));
URI uri = URI.create(cnf.loginKeycloak(redirectUri, type));
return Response.temporaryRedirect(uri).build();
} catch (IOException e) {
throw new GenericApplicationException(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.junit.Assert;
import org.junit.Test;

import cz.incad.kramerius.utils.conf.KConfiguration;


public class ClientKeycloakConfigTest {

Expand All @@ -28,9 +30,18 @@ public void testConfig() {
Assert.assertTrue(config.getRealm().equals("kramerius"));
Assert.assertTrue(config.getResource().equals("krameriusClient"));
Assert.assertTrue(config.getSecret().equals("XXXXXXXXXXXX"));

KConfiguration.getInstance().getConfiguration().setProperty("default.eduid.type","idp");

String url = config.loginKeycloak("http://localhost:4200/keycloak");
Assert.assertEquals("https://k7.inovatika.dev/auth/realms/kramerius/protocol/openid-connect/auth?client_id=krameriusClient&redirect_uri=http://localhost:4200/keycloak&response_type=code", url);
String url = config.loginKeycloak("http://localhost:4200/keycloak",null);
Assert.assertEquals("https://k7.inovatika.dev/auth/realms/kramerius/protocol/openid-connect/auth?client_id=krameriusClient&redirect_uri=http://localhost:4200/keycloak&response_type=code#idp", url);

String urlIdp = config.loginKeycloak("http://localhost:4200/keycloak","idp");
Assert.assertEquals("https://k7.inovatika.dev/auth/realms/kramerius/protocol/openid-connect/auth?client_id=krameriusClient&redirect_uri=http://localhost:4200/keycloak&response_type=code#idp", urlIdp);

String urlForm = config.loginKeycloak("http://localhost:4200/keycloak","form");
Assert.assertEquals("https://k7.inovatika.dev/auth/realms/kramerius/protocol/openid-connect/auth?client_id=krameriusClient&redirect_uri=http://localhost:4200/keycloak&response_type=code#form", urlForm);

}

}

0 comments on commit 2b4e968

Please sign in to comment.