[KEYCLOAK-6164] - Authorization services client doesn't work with JDK7

This commit is contained in:
pedroigor 2018-01-31 13:42:30 -02:00
parent a5f675d693
commit bda57d00df
11 changed files with 43 additions and 39 deletions

View file

@ -18,8 +18,6 @@
<description>KeyCloak AuthZ: Client API</description> <description>KeyCloak AuthZ: Client API</description>
<properties> <properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<keycloak.osgi.export> <keycloak.osgi.export>
org.keycloak.authorization.client.* org.keycloak.authorization.client.*
</keycloak.osgi.export> </keycloak.osgi.export>

View file

@ -30,7 +30,7 @@ import org.keycloak.util.JsonSerialization;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URI; import java.net.URI;
import java.util.function.Supplier; import java.util.concurrent.Callable;
/** /**
* <p>This is class serves as an entry point for clients looking for access to Keycloak Authorization Services. * <p>This is class serves as an entry point for clients looking for access to Keycloak Authorization Services.
@ -40,7 +40,7 @@ import java.util.function.Supplier;
public class AuthzClient { public class AuthzClient {
private final Http http; private final Http http;
private Supplier<String> patSupplier; private Callable<String> patSupplier;
public static AuthzClient create() { public static AuthzClient create() {
InputStream configStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("keycloak.json"); InputStream configStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("keycloak.json");
@ -141,13 +141,13 @@ public class AuthzClient {
return this.deployment; return this.deployment;
} }
private Supplier<String> createPatSupplier() { private Callable<String> createPatSupplier() {
if (patSupplier == null) { if (patSupplier == null) {
patSupplier = new Supplier<String>() { patSupplier = new Callable<String>() {
AccessTokenResponse clientToken = obtainAccessToken(); AccessTokenResponse clientToken = obtainAccessToken();
@Override @Override
public String get() { public String call() {
String token = clientToken.getToken(); String token = clientToken.getToken();
try { try {

View file

@ -19,7 +19,7 @@ package org.keycloak.authorization.client.resource;
import static org.keycloak.authorization.client.util.Throwables.handleAndWrapException; import static org.keycloak.authorization.client.util.Throwables.handleAndWrapException;
import java.util.function.Supplier; import java.util.concurrent.Callable;
import org.keycloak.authorization.client.representation.PermissionRequest; import org.keycloak.authorization.client.representation.PermissionRequest;
import org.keycloak.authorization.client.representation.PermissionResponse; import org.keycloak.authorization.client.representation.PermissionResponse;
@ -32,9 +32,9 @@ import org.keycloak.util.JsonSerialization;
public class PermissionResource { public class PermissionResource {
private final Http http; private final Http http;
private final Supplier<String> pat; private final Callable<String> pat;
public PermissionResource(Http http, Supplier<String> pat) { public PermissionResource(Http http, Callable<String> pat) {
this.http = http; this.http = http;
this.pat = pat; this.pat = pat;
} }
@ -42,7 +42,7 @@ public class PermissionResource {
public PermissionResponse forResource(PermissionRequest request) { public PermissionResponse forResource(PermissionRequest request) {
try { try {
return this.http.<PermissionResponse>post("/authz/protection/permission") return this.http.<PermissionResponse>post("/authz/protection/permission")
.authorizationBearer(this.pat.get()) .authorizationBearer(this.pat.call())
.json(JsonSerialization.writeValueAsBytes(request)) .json(JsonSerialization.writeValueAsBytes(request))
.response().json(PermissionResponse.class).execute(); .response().json(PermissionResponse.class).execute();
} catch (Exception cause) { } catch (Exception cause) {

View file

@ -20,7 +20,7 @@ package org.keycloak.authorization.client.resource;
import static org.keycloak.authorization.client.util.Throwables.handleAndWrapException; import static org.keycloak.authorization.client.util.Throwables.handleAndWrapException;
import java.util.Set; import java.util.Set;
import java.util.function.Supplier; import java.util.concurrent.Callable;
import org.keycloak.authorization.client.representation.RegistrationResponse; import org.keycloak.authorization.client.representation.RegistrationResponse;
import org.keycloak.authorization.client.representation.ResourceRepresentation; import org.keycloak.authorization.client.representation.ResourceRepresentation;
@ -33,9 +33,9 @@ import org.keycloak.util.JsonSerialization;
public class ProtectedResource { public class ProtectedResource {
private final Http http; private final Http http;
private final Supplier<String> pat; private final Callable<String> pat;
public ProtectedResource(Http http, Supplier<String> pat) { public ProtectedResource(Http http, Callable<String> pat) {
this.http = http; this.http = http;
this.pat = pat; this.pat = pat;
} }
@ -43,7 +43,7 @@ public class ProtectedResource {
public RegistrationResponse create(ResourceRepresentation resource) { public RegistrationResponse create(ResourceRepresentation resource) {
try { try {
return this.http.<RegistrationResponse>post("/authz/protection/resource_set") return this.http.<RegistrationResponse>post("/authz/protection/resource_set")
.authorizationBearer(this.pat.get()) .authorizationBearer(this.pat.call())
.json(JsonSerialization.writeValueAsBytes(resource)) .json(JsonSerialization.writeValueAsBytes(resource))
.response().json(RegistrationResponse.class).execute(); .response().json(RegistrationResponse.class).execute();
} catch (Exception cause) { } catch (Exception cause) {
@ -54,7 +54,7 @@ public class ProtectedResource {
public void update(ResourceRepresentation resource) { public void update(ResourceRepresentation resource) {
try { try {
this.http.<RegistrationResponse>put("/authz/protection/resource_set/" + resource.getId()) this.http.<RegistrationResponse>put("/authz/protection/resource_set/" + resource.getId())
.authorizationBearer(this.pat.get()) .authorizationBearer(this.pat.call())
.json(JsonSerialization.writeValueAsBytes(resource)).execute(); .json(JsonSerialization.writeValueAsBytes(resource)).execute();
} catch (Exception cause) { } catch (Exception cause) {
throw handleAndWrapException("Could not update resource", cause); throw handleAndWrapException("Could not update resource", cause);
@ -64,7 +64,7 @@ public class ProtectedResource {
public RegistrationResponse findById(String id) { public RegistrationResponse findById(String id) {
try { try {
return this.http.<RegistrationResponse>get("/authz/protection/resource_set/" + id) return this.http.<RegistrationResponse>get("/authz/protection/resource_set/" + id)
.authorizationBearer(this.pat.get()) .authorizationBearer(this.pat.call())
.response().json(RegistrationResponse.class).execute(); .response().json(RegistrationResponse.class).execute();
} catch (Exception cause) { } catch (Exception cause) {
throw handleAndWrapException("Could not find resource", cause); throw handleAndWrapException("Could not find resource", cause);
@ -74,7 +74,7 @@ public class ProtectedResource {
public Set<String> findByFilter(String filter) { public Set<String> findByFilter(String filter) {
try { try {
return this.http.<Set>get("/authz/protection/resource_set") return this.http.<Set>get("/authz/protection/resource_set")
.authorizationBearer(this.pat.get()) .authorizationBearer(this.pat.call())
.param("filter", filter) .param("filter", filter)
.response().json(Set.class).execute(); .response().json(Set.class).execute();
} catch (Exception cause) { } catch (Exception cause) {
@ -85,7 +85,7 @@ public class ProtectedResource {
public Set<String> findAll() { public Set<String> findAll() {
try { try {
return this.http.<Set>get("/authz/protection/resource_set") return this.http.<Set>get("/authz/protection/resource_set")
.authorizationBearer(this.pat.get()) .authorizationBearer(this.pat.call())
.response().json(Set.class).execute(); .response().json(Set.class).execute();
} catch (Exception cause) { } catch (Exception cause) {
throw handleAndWrapException("Could not find resource", cause); throw handleAndWrapException("Could not find resource", cause);
@ -95,7 +95,7 @@ public class ProtectedResource {
public void delete(String id) { public void delete(String id) {
try { try {
this.http.delete("/authz/protection/resource_set/" + id) this.http.delete("/authz/protection/resource_set/" + id)
.authorizationBearer(this.pat.get()) .authorizationBearer(this.pat.call())
.execute(); .execute();
} catch (Exception cause) { } catch (Exception cause) {
throw handleAndWrapException("Could not delete resource", cause); throw handleAndWrapException("Could not delete resource", cause);

View file

@ -17,7 +17,7 @@
*/ */
package org.keycloak.authorization.client.resource; package org.keycloak.authorization.client.resource;
import java.util.function.Supplier; import java.util.concurrent.Callable;
import org.keycloak.authorization.client.representation.TokenIntrospectionResponse; import org.keycloak.authorization.client.representation.TokenIntrospectionResponse;
import org.keycloak.authorization.client.util.Http; import org.keycloak.authorization.client.util.Http;
@ -27,10 +27,10 @@ import org.keycloak.authorization.client.util.Http;
*/ */
public class ProtectionResource { public class ProtectionResource {
private final Supplier<String> pat; private final Callable<String> pat;
private final Http http; private final Http http;
public ProtectionResource(Http http, Supplier<String> pat) { public ProtectionResource(Http http, Callable<String> pat) {
if (pat == null) { if (pat == null) {
throw new RuntimeException("No access token was provided when creating client for Protection API."); throw new RuntimeException("No access token was provided when creating client for Protection API.");
} }

View file

@ -17,6 +17,11 @@
<name>KeyCloak AuthZ: Provider Parent</name> <name>KeyCloak AuthZ: Provider Parent</name>
<description>KeyCloak AuthZ: Provider Parent</description> <description>KeyCloak AuthZ: Provider Parent</description>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<modules> <modules>
<module>common</module> <module>common</module>
<module>drools</module> <module>drools</module>

View file

@ -21,10 +21,4 @@
<module>policy</module> <module>policy</module>
<module>client</module> <module>client</module>
</modules> </modules>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project> </project>

View file

@ -46,7 +46,14 @@ public class AdminAlbumService {
List<Album> result = this.entityManager.createQuery("from Album").getResultList(); List<Album> result = this.entityManager.createQuery("from Album").getResultList();
for (Album album : result) { for (Album album : result) {
albums.computeIfAbsent(album.getUserId(), key -> new ArrayList<>()).add(album); List<Album> userAlbums = albums.get(album.getUserId());
if (userAlbums == null) {
userAlbums = new ArrayList<>();
albums.put(album.getUserId(), userAlbums);
}
userAlbums.add(album);
} }
return Response.ok(albums).build(); return Response.ok(albums).build();

View file

@ -28,6 +28,7 @@ import java.security.Principal;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.UUID;
@Path("/album") @Path("/album")
@Transaction @Transaction
@ -47,6 +48,7 @@ public class AlbumService {
public Response create(Album newAlbum) { public Response create(Album newAlbum) {
Principal userPrincipal = request.getUserPrincipal(); Principal userPrincipal = request.getUserPrincipal();
newAlbum.setId(UUID.randomUUID().toString());
newAlbum.setUserId(userPrincipal.getName()); newAlbum.setUserId(userPrincipal.getName());
Query queryDuplicatedAlbum = this.entityManager.createQuery("from Album where name = :name and userId = :userId"); Query queryDuplicatedAlbum = this.entityManager.createQuery("from Album where name = :name and userId = :userId");
@ -68,7 +70,7 @@ public class AlbumService {
@Path("{id}") @Path("{id}")
@DELETE @DELETE
public Response delete(@PathParam("id") String id) { public Response delete(@PathParam("id") String id) {
Album album = this.entityManager.find(Album.class, Long.valueOf(id)); Album album = this.entityManager.find(Album.class, id);
try { try {
deleteProtectedResource(album); deleteProtectedResource(album);
@ -90,7 +92,7 @@ public class AlbumService {
@Path("{id}") @Path("{id}")
@Produces("application/json") @Produces("application/json")
public Response findById(@PathParam("id") String id) { public Response findById(@PathParam("id") String id) {
List result = this.entityManager.createQuery("from Album where id = :id").setParameter("id", Long.valueOf(id)).getResultList(); List result = this.entityManager.createQuery("from Album where id = :id").setParameter("id", id).getResultList();
if (result.isEmpty()) { if (result.isEmpty()) {
return Response.status(Status.NOT_FOUND).build(); return Response.status(Status.NOT_FOUND).build();

View file

@ -20,7 +20,6 @@ package org.keycloak.example.photoz.entity;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType; import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import java.util.ArrayList; import java.util.ArrayList;
@ -33,8 +32,7 @@ import java.util.List;
public class Album { public class Album {
@Id @Id
@GeneratedValue private String id;
private Long id;
@Column(nullable = false) @Column(nullable = false)
private String name; private String name;
@ -45,11 +43,11 @@ public class Album {
@Column(nullable = false) @Column(nullable = false)
private String userId; private String userId;
public Long getId() { public String getId() {
return this.id; return this.id;
} }
public void setId(final Long id) { public void setId(final String id) {
this.id = id; this.id = id;
} }

View file

@ -17,8 +17,8 @@
<description/> <description/>
<properties> <properties>
<maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.source>1.7</maven.compiler.source>
</properties> </properties>
<modules> <modules>