MCPcopy Index your code
hub / github.com/FX-HAO/keycloak-phone-authenticator

github.com/FX-HAO/keycloak-phone-authenticator @v9.0.2

Chat with this repo
repository ↗ · DeepWiki ↗ · release v9.0.2 ↗ · + Follow
236 symbols 398 edges 38 files 2 documented · 1%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

keycloak-phone-authenticator

To install the SMS Authenticator one has to:

  • Build and package the project:
  • $ mvn package

  • Add the jar to the Keycloak server:

  • $ cp target/keycloak-phone-authenticator-*.jar _KEYCLOAK_HOME_/providers/

Log in via SMS

image

To enable this you need to:

  1. copy the 'Browser' flow to 'Copy Of Browser' flow (Under Authentication > Flows)
  2. Using the Actions menu on the right-hand side of the 'Copy Of Browser Forms' subflow, select 'Add execution', using the drop-down select 'Username Password Or Phone Form'. Move it up to be the top. (Optional: if you want to enable Google Recaptcha, using the Actions menu on the right-hand side of the 'Username Password Or Phone Form', select 'Config' and enter in the site key and secret, see more: Google Recaptcha)
  3. Remove 'Username Password Form'
  4. In the 'Bindings' menu, change the browser flow from 'Browser' to 'Copy Of Browser'

'Copy Of Reset Credentials' flow shoule be like this: image

Reset credentials via SMS

WeChat13144f2eccd289afc7df3120fdb82abb

To enable this you need to:

  1. copy the 'Reset Credentials' flow to 'Copy Of Reset Credentials' flow (Under Authentication > Flows)
  2. Click 'Add execution' to add provider 'Reset Credential With Phone' and move it to be the top (Optional: if you want to enable Google Recaptcha, using the Actions menu on the right-hand side of the 'Reset Credential With Phone', select 'Config' and enter in the site key and secret, see more: Google Recaptcha)
  3. Click 'Add execution' to add provider 'Send Reset Email If Not Phone' and move it to be the second
  4. Remove 'Choose User' and 'Send Reset Email'
  5. In the 'Bindings' menu, change the reset credential flow from 'reset credentials' to 'Copy Of Reset Credential'

'Copy Of Reset Credentials' flow shoule be like this: image

Phone theme

image

To enable this you need to Select phone theme on the "Realm Settings > Themes" as below:

image

Build into keycloak

An example shows how to build this into keycloak using docker

Installation

This needs an SMS implementation to enable sending verification codes. There are some implementations below (thanks in advance if you'd like to add your implementation here):

Or you can implement your own SPIs.

Once you add an SMS SPI implementation, you can request a verification code with

curl -X POST \
  http://127.0.0.1:8081/auth/realms/$YOUR_REALM/verification_codes/ \
  -H 'content-type: application/json' \
  -d '{
    "phoneNumber": $PHONE_NUMBER
}'

And then use Verification Code authentication flow with the code to obtain an access code.

curl -X POST \
  http://localhost:8081/auth/realms/$YOUR_REALM/protocol/openid-connect/token \
  -H 'authorization: Basic c2h1bmRhby1hZG1pbjoxODc3MGYxMi02NjE4LTQwOTctYThmYi1kMjA0Mzg0Mzg4OTk=' \
  -H 'content-type: application/x-www-form-urlencoded' \
  -d 'grant_type=password&phone_number=$PHONE_NUMBER&code=$VERIFICATION_CODE'

Configuration

Configure your REALM to use the phone number and verification code Authentication. First create a new REALM (or select a previously created REALM).

Under Authentication > Flows: * Copy the 'Direct Grant' flow to 'Direct grant with phone' flow * Click on 'Actions > Add execution' on the 'Provide Phone Number' line * Click on 'Actions > Add execution' on the 'Provide Verification Code' line * Set both of 'Provide Phone Number' and 'Provide Verification Code' to 'REQUIRED'

Extension points exported contracts — how you extend this code

CaptchaService (Interface)
(no doc) [3 implementers]
src/main/java/com/hfx/keycloak/spi/CaptchaService.java
VerificationCodeService (Interface)
(no doc) [2 implementers]
src/main/java/com/hfx/keycloak/spi/VerificationCodeService.java
CaptchaServiceProviderFactory (Interface)
(no doc) [1 implementers]
src/main/java/com/hfx/keycloak/spi/CaptchaServiceProviderFactory.java
VerificationCodeServiceProviderFactory (Interface)
(no doc) [1 implementers]
src/main/java/com/hfx/keycloak/spi/VerificationCodeServiceProviderFactory.java
SmsServiceProviderFactory (Interface)
(no doc)
src/main/java/com/hfx/keycloak/spi/SmsServiceProviderFactory.java

Core symbols most depended-on inside this repo

getRealm
called by 20
src/main/java/com/hfx/keycloak/spi/impl/VerificationCodeServiceImpl.java
getId
called by 8
src/main/java/com/hfx/keycloak/services/PhoneAccountFormServiceProviderFactory.java
getUser
called by 7
src/main/java/com/hfx/keycloak/spi/UsernamePasswordOrPhoneForm.java
challenge
called by 7
src/main/java/com/hfx/keycloak/spi/UsernamePasswordOrPhoneForm.java
getId
called by 5
src/main/java/com/hfx/keycloak/spi/impl/GoogleRecaptchaServiceProviderFactory.java
setReferrerOnPage
called by 5
src/main/java/com/hfx/keycloak/services/PhoneAccountFormService.java
getEntityManager
called by 4
src/main/java/com/hfx/keycloak/spi/impl/VerificationCodeServiceImpl.java
getStatusCode
called by 2
src/main/java/com/hfx/keycloak/SmsException.java

Shape

Method 198
Class 32
Interface 6

Languages

Java100%

Modules by API surface

src/main/java/com/hfx/keycloak/jpa/VerificationCode.java15 symbols
src/main/java/com/hfx/keycloak/spi/UsernamePasswordOrPhoneFormFactory.java14 symbols
src/main/java/com/hfx/keycloak/VerificationCodeRepresentation.java14 symbols
src/main/java/com/hfx/keycloak/spi/VerificationCodeAuthenticatorFactory.java13 symbols
src/main/java/com/hfx/keycloak/spi/PhoneNumberAuthenticatorFactory.java13 symbols
src/main/java/com/hfx/keycloak/services/PhoneAccountFormService.java11 symbols
src/main/java/com/hfx/keycloak/spi/UsernamePasswordOrPhoneForm.java10 symbols
src/main/java/com/hfx/keycloak/spi/impl/VerificationCodeServiceImpl.java8 symbols
src/main/java/com/hfx/keycloak/spi/impl/VerificationCodeAuthenticator.java8 symbols
src/main/java/com/hfx/keycloak/spi/ResetCredentialWithPhone.java8 symbols
src/main/java/com/hfx/keycloak/SmsException.java8 symbols
src/main/java/com/hfx/keycloak/spi/ResetCredentialEmailWithPhone.java7 symbols

For agents

$ claude mcp add keycloak-phone-authenticator \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page