The goal of this project is to implement seamless Keycloak login via Telegram Login Widget.
There are several projects that implement a similar feature but all of them require either user interaction with the bot in the Telegram interface (going to the Telegram application and entering code or generating a link to an external application). Such solutions are good, but a process is inconvenient in many cases and often requires the implementation of additional logic outside Keycloak.
This project, on the other hand, provides the ability to log in without leaving the browser in just a few clicks and does not require the user to perform any special actions.
In addition, the possibilities are not limited only to Browser Flow. You can easily set up authorization through external applications using Implicit Flow with data validation on the Keycloak side.
For a quick introduction to the features, you can deploy a demo project from this repository using docker-compose. This will deploy Keycloak with this plugin and import a demo realm with pre-configured flows.
- Java 17 or higher installed
- Docker and docker-compose
- You have a Telegram bot, and you know its username and token
Use git to clone this repository into your computer.
git clone https://github.com/rickispp/telegram-web-keycloak-authenticatorBuild project
./gradlew buildRun docker compose
docker-compose upSend to @BotFather command for install domain
/setdomain http://keycloak.localhostThen configure authenticator in Keycloak
- Go to the page http://keycloak.localhost/admin/master/console/#/demo/authentication and log in with the
admin/admincredentials. - Select a flow named
Copy of browser - Find the
Telegram Web Login Widgetand click on theSettingsbutton - Fill in the fields
Alias,Bot TokenandBot usernamewithout@(e.g.sample_bot, not@sample_bot) and save configuration
-
After deployment and configuration, you can go to the page http://keycloak.localhost/realms/demo/account/
-
You will see login page that should have login button via Telegram.
- When you click on the button, the Telegram authorization process will start
- After successful authorization, Keycloak will prompt you to enter your email (because this parameter is required in the demo realm settings).
- Finally, by entering an email, you will be redirected to a page with user data. The authorization process has been completed, and a new user has been created in keycloak based on the data received from Telegram.
-
Download and install plugin
- Download
telegram-web-keycloak-authenticator-x.x.x.jarfrom this page https://github.com/rickispp/telegram-web-keycloak-authenticator/releases - Put the downloaded file in a folder
<YOUR_KEYCLOAK_HOME>/providers/ - Run or restart your Keycloak instance
- Download
-
Set Bot Domain
- Send to @BotFather command
/setdomain https://<your_keycloak_host>
-
Add authenticator to your flows
- Go to
Authentication -> Flows -> Browser flow(Direct grant flow also available). - Click on
Add step, findTelegram Web Login Widgetand add step. - Place
Telegram Web Login WidgetafterCookiestep and changeRequirementtoAlternative. - Configure
Telegram Web Login Widgetstep by fillingBot usernameandBot Tokenfields.
- Go to
-
Add Telegram Login Widget to the login page
- If you are using a standard theme, you can simply change it to
keycloak.v2-telegram-web-loginon the pageRealm settings -> Themes -> Login theme. - If you are using a non-standard theme, then you need to add the Telegram Login Widget script to the login page of your theme
<#if telegram_bot_username?? && telegram_redirect_uri??> <div class="telegram-web-login-widget-container"> <script async src="https://telegram.org/js/telegram-widget.js?22" data-telegram-login="${telegram_bot_username}" data-size="large" data-auth-url="${telegram_redirect_uri}" data-request-access="write"></script> </div> </#if>
- If you are using a standard theme, you can simply change it to
-
Add
Content-Security-Policy- Open your
Realm settingsand go toSecurity defensestab - Edit
Content-Security-Policyfield by addinghttps://oauth.telegram.org/toframe-srcandframe-ancestors. For exampleframe-src 'self' https://oauth.telegram.org/; frame-ancestors 'self' https://oauth.telegram.org/; object-src 'none';
- Open your
-
Check that the configuration is correct
- Go to your login page. For example,
https://<your_keycloak_host>/realms/<your_realm>/account/ - You should see login page with a Telegram login button.
- Go to your login page. For example,
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.





