Giter Club home page Giter Club logo

hiami-forgejo-keycloak's Introduction

Authentication in Forgejo using Keycloak

License: BSD3

Trigger Jenkins Pipe

Introduction

This repository was initially started as part of the hackathon 2024 part of the symposium to create integration between Keycloak and Forgejo.

Contributors

  • @Gallypette
  • @Chrisr3d
  • @DavidCruciani

Maintainers

  • @SDKAAA

Instructions

First Install Keycloak and Forgejo by changing directory to folders keycloak and Forgejo and run the following command in each

docker compose up

In case you don't have docker engine installed install it as per the instructions here: https://docs.docker.com/engine/install/fedora/#install-using-the-repository

And then start docker with: sudo systemctl start docker

Automatic Configuration

Keycloak

  • Initiate keycloak by running the script under the keycloak folder and running this command:

    ./post_compose_kc.sh

    This will create the realm and client in keycloak Remark: in case you change the default values make sure to update the .env files under cli_init

[WIP] Forgejo

  • Forgejo Automatic config still under construction, please skip to the manual config

Manual Configuration

Part 1: Keycloak Configuration

  • Create a Realm

Create Realm

If you intend to have multiple applications integrated with Keycloak, using the same realm for multiple applications/clients will enable the users to SSO seamlessly without the need for re-login. Otherwise you can create 1 realm per application/client.

  • Create a Client

  • Now go in credentials tab and save the client secret

    Client Secret

    Remark: this tab does not appears if the "Client authentication" is not set in the previous step.

Part 2: Forgejo Configuration

  • Navigate in the browser to the Forgejo site at http://localhost:30002 and add a username, password and email for the admin user and lunch the setup.

  • Connect as admin user (usually after setup, the admin user is automatically connected)

  • In Forgejo, Click on the right top corner on your profile pic, go in site administration

    Site Admin
  • Under "Identity and Access" go in "Authentication sources"

  • Click on "Add Authentification Source"

Admin View

That's it!

New users created in the keycloak realm will be able to login to forgejo by clicking on login using keycloak You can also enable the creation of new users from keycloak which will automatically be able to sign in to forgejo:

Admin View

Authorization

The below steps explain how to provide basic authorization where access to Forgejo is controlled by whether a user is member of a Keycloak group or not.

This method will use a specific "Required Claim Name" and "Required Claim Value" in Forgejo while sending this info from Keycloak using the client scope mappers.

Hereafter KC refers to Keycloak and FJ to Forgejo.

  • in KC create a group called forgejogroup
  • in FJ create 2 users (user1 and user2 obviousbly create them throught he SSO to KC)
  • add user user1 to forgejogroup (do NOT add user2 to the group; the purpose of user2 is to test if the authorization works for user and not for user2)
  • in KC, edit the group and create the attribute with the following:
    • Key: user_type
    • Value: forgejo_user
  • in FJ, in admin settings/Identity&Access/Authentication sources, edit the Keycloak source and enter the below values in the below fields:
    • Required claim name: user_type
    • Required claim value: forgejo_user
  • Update the authentication source.
  • in KC, under Client Scopes, create a new client scope called forgejoclientscope
  • under this KC client scope, add a mapper with the following info:
    • Mapper Type: "Group Membership"
    • Name: forgejogroup
    • Token Claim Name: forgejogrouptoken
  • under this KC client scope, add a second mapper with the following info:
    • Mapper Type: "User Attribute"
    • Name: user_type
    • User Attribute: user_type
    • Token Claim Name: user_type
  • in KC, edit the client and add the above selected client to the scopes of the client

That's it!
user1 can now sign in and get access to FJ while user2 can sign in but will receive an error.

.

Full Authorization still needs to be investigated:

We did not success in having authorization to work with keycloak. The crux of the issue is that neither forgejo nor gitea support nested structures for roles. See the following issues: go-gitea/gitea#10016 https://codeberg.org/forgejo/forgejo/issues/1756

The answer lies in mapping roles to a flat token claim.

hiami-forgejo-keycloak's People

Contributors

sdkaaa avatar deadlyelder avatar davidcruciani avatar

hiami-forgejo-keycloak's Issues

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.