2

I was trying out Quarkus security integration with keycloak

Here is my rest endpoints

@Path("/jwt")
@RequestScoped
public class JWTRestController {

    @Inject
    @Claim(standard = Claims.preferred_username)
    Optional<JsonString> currentUsername;

    @GET
    @Path("/user")
    @Produces(MediaType.TEXT_PLAIN)
    @RolesAllowed({"user"})
    public String userData() {
        return "data for user ";
    }

    @GET
    @Path("/admin")
    @Produces(MediaType.TEXT_PLAIN)
    @RolesAllowed({"admin"})
    public String adminData() {
        return "data for admin ";
    }

}

My application properties

# Configuration file
quarkus.http.port=8082

# MP-JWT Config
mp.jwt.verify.publickey.location=http://localhost:8180/auth/realms/demo/protocol/openid-connect/certs
mp.jwt.verify.issuer=http://localhost:8180/auth/realms/demo
quarkus.smallrye-jwt.auth-mechanism=MP-JWT
quarkus.smallrye-jwt.realmName=quarkus-keycloak-demo
quarkus.smallrye-jwt.enabled=true

I have running instance of keycloak on my local development machine on port 8180 I have done all the per-requisite for keycloak and created realm, user, roles

I am able to get token from keycloak as follow -

export access_token=$(\
    curl -X POST http://localhost:8180/auth/realms/demo/protocol/openid-connect/token \
    --user demo-client:e0da2ad7-5f4c-49b3-ae54-dbd7a28d532a \
    -H 'content-type: application/x-www-form-urlencoded' \
    -d 'username=user&password=user&grant_type=password' | jq --raw-output '.access_token' \
 )

But here is problem when try to access the rest point /jwt/user

curl -v -H "Authorization: Bearer $access_token" http://localhost:8082/jwt/user

It results in

< HTTP/1.1 403 Forbidden
< Content-Length: 9
< Content-Type: text/plain;charset=UTF-8
<
* Connection #0 to host localhost left intact
Forbidden* Closing connection 0

Here is token details (I user jwt.io debugger to look inside the token )

enter image description here

Anyhelp would be highly appreciated

Rahul Wagh
  • 470
  • 1
  • 6
  • 23

2 Answers2

0

Please use quarkus-oidc, follow https://quarkus.io/guides/security-openid-connect for the service applications which accept the bearer tokens

Sergey Beryozkin
  • 688
  • 1
  • 4
  • 9
0

In the Keycloak, the role admin must be assigned to the demo-client.

About Quarkus JWT RBAC, the extension expects a token having a "groups" claim.

There is a Quarkus JWT configuration used to map a different claim for roles (in this case realm_access/roles):

smallrye.jwt.path.groups="realm_access/roles"

Swe77
  • 21
  • 4