Git Product home page Git Product logo

quarkus-keycloak's Introduction

quarkus angular keycloak

Keycloak Quarkus Angular demo showing:

Angular client uses APP_INITIALIZER to initiate auth module and components and login to the quarkus realm in keycloak prior to loading.

The Auth Guard and Auth Interceptor components map roles to component uri's see the app-routing-module.ts for usage.

In Quarkus, Keycloak is responsible for managing the roles and deciding who can access which routes. See the Keycloak backend-serivce client Authorizations tab for policy and permission details. The server side extension fetches resources on-demand from Keycloak where their URI are used to map the resources in your application that should be protected.

There is no TLS configured (this is on purpose to keep auth code clear, obviously don't do this in production! and we need TLS to be able to use all OAuth 2.1 best practices like server constrained key rotations)

images/ui-keycloak.png

Running the demo locally

Start Keycloak

podman-compose up -d

Browse to Admin Console and login as admin/admin

http://localhost:8180

Select Master -> Add Realm -> Select File to create quarkus realm

quarkus-realm.json

Start API Server

cd service
mvn quarkus:dev

Start Angular UI

cd ui
npm i
npm run start

User/Login and Roles in quarkus realm

admin / admin - roles [admin, user]
alice / alice - roles [user]

Use the UI broswer links and the browser console (F12 developer tools in chrome) for debug.

The User Interface has 3 angular components, each calls a service API XMLHttpRequest endpoint, which is displayed in console.log

admin - localhost:8080/api/admin
user - localhost:8080/api/users/me
public - localhost:8080/api
  • alice will be able to see the User and Public Component endpoints but not be able to browse to the Admin Component endpoint.
  • admin user can browse to the all component endpoints.
  • anonymous acess is available to the public api endpoint.

Unauthenticated access to admin, user endpoints will result in:

http localhost:8080/api/admin
HTTP/1.1 401 Unauthorized
content-length: 0

The public endpoint is always available e.g. from cli:

http localhost:8080/api
HTTP/1.1 200 OK
Content-Length: 15
Content-Type: application/json

{
    "userName": ""
}

CLI example request using curl or http client:

# Login
export access_token=$(\
curl -s -X POST http://localhost:8180/auth/realms/quarkus/protocol/openid-connect/token \
   --user backend-service:secret \
   -H 'content-type: application/x-www-form-urlencoded' \
   -d 'username=admin&password=admin&grant_type=password' | jq --raw-output '.access_token'
)

# curl
curl -v -X GET \
  http://localhost:8080/api/admin \
  -H "Authorization: Bearer "$access_token

# or use http
http localhost:8080/api/admin "Authorization: Bearer $access_token"

# Authenticated Result
HTTP/1.1 200 OK
Content-Length: 20
Content-Type: application/json

{
    "userName": "admin"
}

Notes

If you wanted to completely turn off authentication in the quarkus api server, set these in application.properties:

quarkus.keycloak.policy-enforcer.enable=false
quarkus.keycloak.policy-enforcer.paths.api.path=/api/*
quarkus.keycloak.policy-enforcer.paths.api.enforcement-mode=DISABLED

Currently the Angular UI flow initializes by directing to Keycloak first before loading anything (common for Single Page Web Apps), the public api is still accessible anonymously.

quarkus-keycloak's People

Contributors

eformat avatar

Stargazers

Juliano Mohr avatar Joel Wirāmu Pauling avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.