<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>realm - Inero Software - Software Consulting</title>
	<atom:link href="https://inero-software.com/tag/realm/feed/" rel="self" type="application/rss+xml" />
	<link>https://inero-software.com/tag/realm/</link>
	<description>We unleash innovations using cutting-edge technologies, modern design and AI</description>
	<lastBuildDate>Wed, 06 Nov 2024 07:28:50 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://inero-software.com/wp-content/uploads/2018/11/inero-logo-favicon.png</url>
	<title>realm - Inero Software - Software Consulting</title>
	<link>https://inero-software.com/tag/realm/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">153509928</site>	<item>
		<title>Behind the Scenes: Custom SMS Authenticator with Keycloak</title>
		<link>https://inero-software.com/custom-sms-authenticator-with-keycloak/</link>
		
		<dc:creator><![CDATA[Marceli Formela]]></dc:creator>
		<pubDate>Fri, 14 Jun 2024 10:29:52 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Company]]></category>
		<category><![CDATA[Keycloak]]></category>
		<category><![CDATA[keycloak]]></category>
		<category><![CDATA[MFA]]></category>
		<category><![CDATA[OTP]]></category>
		<category><![CDATA[realm]]></category>
		<category><![CDATA[sms]]></category>
		<category><![CDATA[sms gateway]]></category>
		<category><![CDATA[SMS-based OTP]]></category>
		<category><![CDATA[SPI]]></category>
		<guid isPermaLink="false">https://inero-software.com/?p=5838</guid>

					<description><![CDATA[<p>Artykuł <a href="https://inero-software.com/custom-sms-authenticator-with-keycloak/">Behind the Scenes: Custom SMS Authenticator with Keycloak</a> pochodzi z serwisu <a href="https://inero-software.com">Inero Software - Software Consulting</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<div class="row"><div class="col-sm-1"></div><div class="col-sm-10">
<p>&nbsp;</p>
<p><span style="font-weight: 400;">With the increasing number of cyber threats, multi-factor authentication (MFA) has become a standard security measure. MFA enhances protection by requiring users to verify their identity through multiple methods. In today’s digital world MFA has become a standard security practice, adding an extra layer of protection by requiring users to provide multiple forms of verification. </span></p>
<p><span style="font-weight: 400;">Among the various advanced authentication methods, SMS-based authentication stands out for its balance of security and user convenience. However, sometimes creating a custom SMS authenticator within an identity provider like Keycloak can be a complex and nuanced process, demanding an understanding of its architecture and extensibility.</span></p>
<p>&nbsp;</p>
<h3><b>Service Provider Interface (SPI)</b></h3>
<p><span style="font-weight: 400;">Keycloak aims to address the majority of use cases without forcing you to create custom code. However, it also offers flexibility for customization. To support this, Keycloak provides several SPIs that allow you to implement your own solutions. We are going to implement an authenticator that requires a valid SMS code. To create this feature, we must at least implement the org.keycloak.authentication.AuthenticatorFactory and Authenticator interfaces. The <em>AuthenticatorFactory</em> is responsible for creating instances of an Authenticator. They both extend a more generic Provider and <em>ProviderFactory</em> set of interfaces that other Keycloak components do.</span></p>
<p>&nbsp;</p>
<h3><b>Packaging classes</b></h3>
<p><span style="font-weight: 400;">We will package our classes within a single project. It must contain a file named org.keycloak.authentication.AuthenticatorFactory and must be contained in the META-INF/services/ directory. This file must list the fully qualified class name of each <em>AuthenticatorFactory</em> implementation you have in the jar. For example:</span></p>
<pre><span style="font-weight: 400;">pl.inero.keycloakext.authenticator.sms.SmsAuthenticatorFactory</span>
<span style="font-weight: 400;">pl.inero.keycloakext.authenticator.custom.CustomUsernamePasswordFormFactory</span>
<span style="font-weight: 400;">pl.inero.keycloakext.authenticator.custom.CustomCookieAuthenticatorFactory</span></pre>
<p><span style="font-weight: 400;">This services/ file is used by Keycloak to scan the providers it has to load into the system.</span></p>
<p>&nbsp;</p>
<h3><b>CredentialModel and CredentialProvider</b></h3>
<p><span style="font-weight: 400;">The first step is to configure our Credential related classes since the user&#8217;s phone number should be stored as credential record. As you can see below, the <em>Sms2faCredentialData</em> class is a straightforward data container for storing the phone number associated with the user.</span></p>
<pre><span style="font-weight: 400;">public class </span><span style="font-weight: 400;">Sms2faCredentialData {
</span>
<span style="font-weight: 400;">private </span><span style="font-weight: 400;">String phoneNumber</span><span style="font-weight: 400;">;
</span>
<span style="font-weight: 400;">@SuppressWarnings(</span><span style="font-weight: 400;">"unused"</span><span style="font-weight: 400;">) </span><span style="font-weight: 400;">//used for credentials deserialization</span>
<span style="font-weight: 400;">public </span><span style="font-weight: 400;">Sms2faCredentialData() {</span>
<span style="font-weight: 400;">}
</span>
<span style="font-weight: 400;">public </span><span style="font-weight: 400;">Sms2faCredentialData(String phoneNumber) {</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">this</span><span style="font-weight: 400;">.phoneNumber = phoneNumber</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">}
</span>
<span style="font-weight: 400;">public </span><span style="font-weight: 400;">String getPhoneNumber() {</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">return </span><span style="font-weight: 400;">phoneNumber</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">}
</span>
<span style="font-weight: 400;">@SuppressWarnings(</span><span style="font-weight: 400;">"unused"</span><span style="font-weight: 400;">) </span><span style="font-weight: 400;">//used for credentials deserialization</span>
<span style="font-weight: 400;">public void </span><span style="font-weight: 400;">setPhoneNumber(String phoneNumber) {</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">this</span><span style="font-weight: 400;">.phoneNumber = phoneNumber</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">}</span>
<span style="font-weight: 400;">}</span></pre>
<p><span style="font-weight: 400;">The next step is to extend the <em>CredentialModel</em> class that can generate the correct format of the credential in the database. For the <em>Sms2faCredentialModel</em> objects to be fully functional, they need to encompass not only the raw JSON data inherited from their parent but also encapsulate the unmarshalled objects within their own attributes. This ensures wide accessibility and utilization of the credentials, providing easy integration and handling of authentication processes.</span></p>
<pre><span style="font-weight: 400;">public class </span><span style="font-weight: 400;">Sms2faCredentialModel </span><span style="font-weight: 400;">extends </span><span style="font-weight: 400;">CredentialModel {
</span>
<span style="font-weight: 400;">public static final </span><span style="font-weight: 400;">String TYPE = </span><span style="font-weight: 400;">"sms2fa"</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">private </span><span style="font-weight: 400;">Sms2faCredentialData smsCredentials</span><span style="font-weight: 400;">;
</span>
<span style="font-weight: 400;">public </span><span style="font-weight: 400;">Sms2faCredentialModel(Sms2faCredentialData smsCredentials) {</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">try </span><span style="font-weight: 400;">{</span>
<span style="font-weight: 400;">        </span> <span style="font-weight: 400;">this</span><span style="font-weight: 400;">.smsCredentials = smsCredentials</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">        </span> <span style="font-weight: 400;">setCredentialData(JsonSerialization.writeValueAsString(smsCredentials))</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">        </span> <span style="font-weight: 400;">setUserLabel(</span><span style="font-weight: 400;">"tel: " </span><span style="font-weight: 400;">+ smsCredentials.getPhoneNumber())</span><span style="font-weight: 400;">;
</span>
<span style="font-weight: 400;">        </span> <span style="font-weight: 400;">setType(TYPE)</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">} </span><span style="font-weight: 400;">catch </span><span style="font-weight: 400;">(IOException e) {</span>
<span style="font-weight: 400;">        </span> <span style="font-weight: 400;">throw new </span><span style="font-weight: 400;">RuntimeException(e)</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">}</span>
<span style="font-weight: 400;">}
</span>
<span style="font-weight: 400;">public </span><span style="font-weight: 400;">String getPhoneNumber() {</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">return </span><span style="font-weight: 400;">smsCredentials.getPhoneNumber()</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">}</span>
<span style="font-weight: 400;">}</span></pre>
<p><span style="font-weight: 400;">Similar to other providers within Keycloak, the creation of the <em>CredentialProvider</em> requires the presence of a corresponding <em>CredentialsProviderFactory</em>. In meeting this requirement, we implement the <em>Sms2faCredentialProviderFactory</em> requirement, we implement the<em> Sms2faCredentialProviderFactor</em></span></p>
<pre><span style="font-weight: 400;">public class </span><span style="font-weight: 400;">Sms2faCredentialProviderFactory  </span><span style="font-weight: 400;">implements 
</span><span style="font-weight: 400;">CredentialProviderFactory&lt;Sms2faCredentialProvider&gt; {</span>

<span style="font-weight: 400;">public static final </span><span style="font-weight: 400;">String PROVIDER_ID = </span><span style="font-weight: 400;">"keycloak-ext-sms2fa"</span><span style="font-weight: 400;">;</span>

<span style="font-weight: 400;">@Override</span>
<span style="font-weight: 400;">public </span><span style="font-weight: 400;">String getId() {</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">return </span><span style="font-weight: 400;">PROVIDER_ID</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">}</span>

<span style="font-weight: 400;">@Override</span>
<span style="font-weight: 400;">public </span><span style="font-weight: 400;">CredentialProvider&lt;Sms2faCredentialModel&gt; create(KeycloakSession session) {</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">return new </span><span style="font-weight: 400;">Sms2faCredentialProvider(session)</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">}</span>
<span style="font-weight: 400;">}</span></pre>
<p><span style="font-weight: 400;">The <em>CredentialProvider</em> interface is structured with a generic parameter that extends a <em>CredentialModel</em>, ensuring compatibility across a spectrum of credential types. Additionally, we need to implement the <em>CredentialInputValidator</em> interface, indicating to Keycloak that this provider is equipped to authenticate credentials for our custom Authenticator. Although we won’t dive into the full architecture here, Keycloak documentation covers additional methods.</span></p>
<p><span style="font-weight: 400;">Our implementation includes functionalities to create and delete credentials. These functionalities use the credential manager, responsible for the storage and retrieval of credentials, whether they’re stored locally or within federated storage systems.</span></p>
<pre><span style="font-weight: 400;">@Override</span>
<span style="font-weight: 400;">public </span><span style="font-weight: 400;">CredentialModel createCredential(RealmModel realm</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">UserModel user</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">Sms2faCredentialModel credentialModel) {</span>
<span style="font-weight: 400;">if </span><span style="font-weight: 400;">(credentialModel.getCreatedDate() == </span><span style="font-weight: 400;">null</span><span style="font-weight: 400;">) {</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">credentialModel.setCreatedDate(Time.currentTimeMillis())</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">return </span><span style="font-weight: 400;">user.credentialManager().createStoredCredential(credentialModel)</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">}</span>

<span style="font-weight: 400;">@Override</span>
<span style="font-weight: 400;">public boolean </span><span style="font-weight: 400;">deleteCredential(RealmModel realm</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">UserModel user</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">String credentialId) {</span>
<span style="font-weight: 400;">logger.debugv(</span><span style="font-weight: 400;">"Delete Sms2fa credential. username = {0}, credentialId = {1}"</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">user.getUsername()</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">credentialId)</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">return </span><span style="font-weight: 400;">user.credentialManager().removeStoredCredentialById(credentialId)</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">}</span></pre>
<p><span style="font-weight: 400;">For the <em>CredentialInputValidator,</em> the main method to implement is the isValid, which tests whether a credential is valid for a given user in a given realm. This is the method that is called by the Authenticator when it seeks to validate the user’s input.</span></p>
<pre><span style="font-weight: 400;">@Override</span>
<span style="font-weight: 400;">public boolean </span><span style="font-weight: 400;">isValid(RealmModel realm</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">UserModel user</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">CredentialInput credentialInput) {</span>
<span style="font-weight: 400;">final </span><span style="font-weight: 400;">Sms2faCredentialModel credentialModel = getDefaultCredential(session</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">realm</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">user)</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">final </span><span style="font-weight: 400;">String secretData = credentialModel.getSecretData()</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">return </span><span style="font-weight: 400;">secretData != </span><span style="font-weight: 400;">null </span><span style="font-weight: 400;">&amp;&amp; secretData.equals(credentialInput.getChallengeResponse())</span><span style="font-weight: 400;">;</span></pre>
<p><span style="font-weight: 400;">Now we should have everything to be able to move on to implementing the authenticator itself.</span></p>
<p>&nbsp;</p>
<h3><b>AuthenticatorFactory and Authenticator</b></h3>
<p><span style="font-weight: 400;">The SmsAuthenticatorFactory class encapsulated the logic needed to configure and create instances of the SmsAuthenticator, which performs SMS-based OTP validation. It supports customization through several configurable properties.</span></p>
<pre><span style="font-weight: 400;">supports customization through several configurable properties.</span>
<span style="font-weight: 400;">public class </span><span style="font-weight: 400;">SmsAuthenticatorFactory </span><span style="font-weight: 400;">implements </span><span style="font-weight: 400;">AuthenticatorFactory {</span>

<span style="font-weight: 400;">@Override</span>
<span style="font-weight: 400;">public </span><span style="font-weight: 400;">String getId() {</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">return </span><span style="font-weight: 400;">"sms-authenticator"</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">}</span>

<span style="font-weight: 400;">@Override</span>
<span style="font-weight: 400;">public </span><span style="font-weight: 400;">String getDisplayType() {</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">return </span><span style="font-weight: 400;">"SMS Authentication"</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">}</span>
<span style="font-weight: 400;">@Override</span>
<span style="font-weight: 400;">public </span><span style="font-weight: 400;">String getHelpText() {</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">return </span><span style="font-weight: 400;">"Validates an OTP sent via SMS to the users mobile phone."</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">}</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">(…)</span>
<span style="font-weight: 400;">}</span></pre>
<p><span style="font-weight: 400;">Now let’s dive into the Authenticator itself. The primary method to focus is sendChallenge(). When the flow is initially triggered, this method is invoked. It’s important to notice that it doesn’t handle the processing of the SMS code form. Rather, its role is to either render the page or continue the flow.</span></p>
<p><span style="font-weight: 400;">The HTML page requesting the received code is presented to the user, who then inputs the code and submits it. Then an HTTP request is sent to the flow via the action URL specified in the HTML form. This triggers the action() method within our Authenticator implementation. If the provided code is invalid, we reconstruct the HTML Form, appending an error message. Following this, we utilize failureChallenge(), passing the reason for the failure. It operated similarly to challenge(), but additionally logs the error, helping to detect any attack possibility.</span></p>
<pre><span style="font-weight: 400;">private void </span><span style="font-weight: 400;">sendChallenge(AuthenticationFlowContext context) {</span>
<span style="font-weight: 400;">(…)</span>
<span style="font-weight: 400;">credentialModel.setSecretData(code)</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">user.credentialManager().updateStoredCredential(credentialModel)</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">AuthenticationSessionModel authSession = context.getAuthenticationSession()</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">authSession.setAuthNote(</span><span style="font-weight: 400;">"ttl"</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">Long.toString(System.currentTimeMillis() + (ttl * </span><span style="font-weight: 400;">1000L</span><span style="font-weight: 400;">)))</span><span style="font-weight: 400;">;</span>

<span style="font-weight: 400;">try </span><span style="font-weight: 400;">{</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">/* sending SMS */</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">context.challenge(context.form().setAttribute(</span><span style="font-weight: 400;">"realm"</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">context.getRealm()).createForm(TPL_CODE))</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">} </span><span style="font-weight: 400;">catch </span><span style="font-weight: 400;">(Exception e) {</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">context.failureChallenge(AuthenticationFlowError.INTERNAL_ERROR</span><span style="font-weight: 400;">,</span>
<span style="font-weight: 400;">            </span> <span style="font-weight: 400;">context.form().setError(</span><span style="font-weight: 400;">"smsAuthSmsNotSent"</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">e.getMessage())</span>
<span style="font-weight: 400;">                    </span> <span style="font-weight: 400;">.createErrorPage(Response.Status.INTERNAL_SERVER_ERROR))</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">}</span>
<span style="font-weight: 400;">}</span>

<span style="font-weight: 400;">@Override</span>
<span style="font-weight: 400;">public void </span><span style="font-weight: 400;">action(AuthenticationFlowContext context) {</span>
<span style="font-weight: 400;">(…)</span>
<span style="font-weight: 400;">final </span><span style="font-weight: 400;">Sms2faCredentialModel credentialModel = getCredentialProvider(session).getDefaultCredential(session</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">context.getRealm()</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">user)</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">boolean </span><span style="font-weight: 400;">isValid = getCredentialProvider(context.getSession()).isValid(context.getRealm()</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">context.getUser()</span><span style="font-weight: 400;">,</span>
<span style="font-weight: 400;">       new </span><span style="font-weight: 400;">UserCredentialModel(credentialModel.getId()</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">getCredentialProvider(context.getSession()).getType()</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">enteredCode))</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">if </span><span style="font-weight: 400;">(isValid) {</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">if </span><span style="font-weight: 400;">(Long.parseLong(ttl) &lt; System.currentTimeMillis()) {</span>
<span style="font-weight: 400;">        </span> <span style="font-weight: 400;">// expired</span>
<span style="font-weight: 400;">        </span> <span style="font-weight: 400;">context.failureChallenge(AuthenticationFlowError.EXPIRED_CODE</span><span style="font-weight: 400;">,
</span>
<span style="font-weight: 400;">                </span> <span style="font-weight: 400;">context.form().setError(</span><span style="font-weight: 400;">"smsAuthCodeExpired"</span><span style="font-weight: 400;">).createErrorPage(Response.Status.BAD_REQUEST))</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">} </span><span style="font-weight: 400;">else </span><span style="font-weight: 400;">{</span>
<span style="font-weight: 400;">        </span> <span style="font-weight: 400;">// valid</span>
<span style="font-weight: 400;">        </span> <span style="font-weight: 400;">context.success()</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">}</span>
<span style="font-weight: 400;">} </span><span style="font-weight: 400;">else </span><span style="font-weight: 400;">{</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">context.getEvent().user(user).error(Errors.INVALID_USER_CREDENTIALS)</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">context.failureChallenge(AuthenticationFlowError.INVALID_CREDENTIALS</span><span style="font-weight: 400;">,</span>
<span style="font-weight: 400;">            </span> <span style="font-weight: 400;">context.form().setAttribute(</span><span style="font-weight: 400;">"realm"</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">context.getRealm())</span>
<span style="font-weight: 400;">                    </span> <span style="font-weight: 400;">.setError(</span><span style="font-weight: 400;">"smsAuthCodeInvalid"</span><span style="font-weight: 400;">).createForm(TPL_CODE))</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">}</span>
<span style="font-weight: 400;">}</span></pre>
<h3></h3>
<h3><b>Authentication Flow</b></h3>
<p><span style="font-weight: 400;">To add an Authenticator into a flow, administrators must navigate to the Console. By accessing the Authentication section and navigating to the Flow tabs, they should see existing flows. Built-in flows cannot be directly modified, so to integrate the newly created Authenticator, we need to either duplicate an existing flow or create a new one from scratch.</span></p>
<p><img fetchpriority="high" decoding="async" data-attachment-id="5841" data-permalink="https://inero-software.com/custom-sms-authenticator-with-keycloak/2-7/" data-orig-file="https://inero-software.com/wp-content/uploads/2024/06/2-3.png" data-orig-size="1920,1080" data-comments-opened="0" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="2" data-image-description="" data-image-caption="" data-medium-file="https://inero-software.com/wp-content/uploads/2024/06/2-3-300x169.png" data-large-file="https://inero-software.com/wp-content/uploads/2024/06/2-3-1030x579.png" tabindex="0" role="button" class="aligncenter wp-image-5841 size-full" src="https://inero-software.com/wp-content/uploads/2024/06/2-3.png" alt="" width="1920" height="1080" srcset="https://inero-software.com/wp-content/uploads/2024/06/2-3.png 1920w, https://inero-software.com/wp-content/uploads/2024/06/2-3-300x169.png 300w, https://inero-software.com/wp-content/uploads/2024/06/2-3-1030x579.png 1030w, https://inero-software.com/wp-content/uploads/2024/06/2-3-768x432.png 768w, https://inero-software.com/wp-content/uploads/2024/06/2-3-1536x864.png 1536w, https://inero-software.com/wp-content/uploads/2024/06/2-3-533x300.png 533w" sizes="(max-width: 1920px) 100vw, 1920px" /></p>
<h3><b>Required actions</b></h3>
<p><span style="font-weight: 400;">If the phone is not set up, we should trigger a custom required action. Again, we should add the fully qualified class name to the META-INF/services directory and implement <em>RequiredActionProvider</em> interface. <em>Method requiredActionChallenge()</em> is responsible for rendering the HTML that will drive the required action.</span></p>
<pre><span style="font-weight: 400;">@Override</span>
<span style="font-weight: 400;">public void </span><span style="font-weight: 400;">requiredActionChallenge(RequiredActionContext context) {</span>
<span style="font-weight: 400;">LoginFormsProvider form = context.form()</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">if </span><span style="font-weight: 400;">(getSmsAuthenticatorConfig(context) == </span><span style="font-weight: 400;">null</span><span style="font-weight: 400;">) {</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">form.setError(</span><span style="font-weight: 400;">"smsAuthMissingAuthenticatorConfig"</span><span style="font-weight: 400;">)</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">}</span>
<span style="font-weight: 400;">final </span><span style="font-weight: 400;">Response response = form.createForm(</span><span style="font-weight: 400;">"sms-2fa-register.ftl"</span><span style="font-weight: 400;">)</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">context.challenge(response)</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">}</span></pre>
<p><span style="font-weight: 400;">This part is responsible for processing input from the HTML form of the required action. After entering the received SMS code, the phone number should be saved in the database as a credential. The next time we log in, we will be able to use this form of OTP.</span></p>
<pre><span style="font-weight: 400;">@Override</span>
<span style="font-weight: 400;">public void </span><span style="font-weight: 400;">processAction(RequiredActionContext context) {</span>
<span style="font-weight: 400;">    (…)</span>
<span style="font-weight: 400;">final </span><span style="font-weight: 400;">String phoneNumber = params.getFirst(</span><span style="font-weight: 400;">"phoneNumber"</span><span style="font-weight: 400;">)</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">final </span><span style="font-weight: 400;">String code = params.getFirst(</span><span style="font-weight: 400;">"code"</span><span style="font-weight: 400;">)</span><span style="font-weight: 400;">;</span>

<span style="font-weight: 400;">if</span><span style="font-weight: 400;">(StringUtils.isBlank(phoneNumber) &amp;&amp; StringUtils.isBlank(authSession.getAuthNote(</span><span style="font-weight: 400;">"phone_number"</span><span style="font-weight: 400;">))) {</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">//if no phone number is set, redirect to the first page</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">requiredActionChallenge(context)</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">return;</span>
<span style="font-weight: 400;">}</span>
<span style="font-weight: 400;">if </span><span style="font-weight: 400;">(phoneNumber != </span><span style="font-weight: 400;">null</span><span style="font-weight: 400;">) {</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">sendPhoneNumberVerificationChallenge(context</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">authSession</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">phoneNumber</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">smsAuthenticatorConfig.getConfig())</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">return;</span>
<span style="font-weight: 400;">}</span>
<span style="font-weight: 400;">if </span><span style="font-weight: 400;">(code != </span><span style="font-weight: 400;">null</span><span style="font-weight: 400;">) {</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">/* verify provided SMS code */</span> 
<span style="font-weight: 400;">    </span><span style="font-weight: 400;">}</span>
<span style="font-weight: 400;">}</span></pre>
<p><span style="font-weight: 400;">The final thing you have to do is go into the Admin Console and Required Actions tab. Your new action should now be displayed and enabled in the required actions list.</span></p>
<p><img decoding="async" data-attachment-id="5840" data-permalink="https://inero-software.com/custom-sms-authenticator-with-keycloak/1-6/" data-orig-file="https://inero-software.com/wp-content/uploads/2024/06/1-3.png" data-orig-size="1920,1080" data-comments-opened="0" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="1" data-image-description="" data-image-caption="" data-medium-file="https://inero-software.com/wp-content/uploads/2024/06/1-3-300x169.png" data-large-file="https://inero-software.com/wp-content/uploads/2024/06/1-3-1030x579.png" tabindex="0" role="button" class="wp-image-5840 aligncenter" src="https://inero-software.com/wp-content/uploads/2024/06/1-3-300x169.png" alt="" width="774" height="436" srcset="https://inero-software.com/wp-content/uploads/2024/06/1-3-300x169.png 300w, https://inero-software.com/wp-content/uploads/2024/06/1-3-1030x579.png 1030w, https://inero-software.com/wp-content/uploads/2024/06/1-3-768x432.png 768w, https://inero-software.com/wp-content/uploads/2024/06/1-3-1536x864.png 1536w, https://inero-software.com/wp-content/uploads/2024/06/1-3-533x300.png 533w, https://inero-software.com/wp-content/uploads/2024/06/1-3.png 1920w" sizes="(max-width: 774px) 100vw, 774px" /></p>
<p><span style="font-weight: 400;">If the user hasn’t provided a phone number before and SMS authenticator is set up as required in the Authentication Flow, a new view should appear.</span></p>
<p><img decoding="async" data-attachment-id="5842" data-permalink="https://inero-software.com/custom-sms-authenticator-with-keycloak/3-5/" data-orig-file="https://inero-software.com/wp-content/uploads/2024/06/3-3.png" data-orig-size="1920,1080" data-comments-opened="0" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="3" data-image-description="" data-image-caption="" data-medium-file="https://inero-software.com/wp-content/uploads/2024/06/3-3-300x169.png" data-large-file="https://inero-software.com/wp-content/uploads/2024/06/3-3-1030x579.png" tabindex="0" role="button" class="alignnone wp-image-5842 size-full" src="https://inero-software.com/wp-content/uploads/2024/06/3-3.png" alt="" width="1920" height="1080" srcset="https://inero-software.com/wp-content/uploads/2024/06/3-3.png 1920w, https://inero-software.com/wp-content/uploads/2024/06/3-3-300x169.png 300w, https://inero-software.com/wp-content/uploads/2024/06/3-3-1030x579.png 1030w, https://inero-software.com/wp-content/uploads/2024/06/3-3-768x432.png 768w, https://inero-software.com/wp-content/uploads/2024/06/3-3-1536x864.png 1536w, https://inero-software.com/wp-content/uploads/2024/06/3-3-533x300.png 533w" sizes="(max-width: 1920px) 100vw, 1920px" /></p>
<p><span style="font-weight: 400;">In summary. configuring SMS MFA involves setting up custom required actions and authenticator providers, but of course, there are other things to cover like Keycloak &lt;-&gt; SMS gateway communication. What we looked at in this post is of course only part of the possible configuration, but the most important and Keycloak-specific one.</span></p>
<p><span style="font-weight: 400;">One of the significant advantages of SMS MFA implementation is its widespread accessibility, as most users already have mobile phones capable of receiving SMS messages. Additionally, it provides a straightforward user experience, requiring minimal setup and familiarity for users. However, this mechanism does have its drawbacks, including potential vulnerabilities such as SIM swapping attacks or interception of SMS codes. Moreover, SMS delivery can sometimes be unreliable, leading to delays or failed delivery, impacting the user experience. We should be aware of this before we decide on such a method, especially in the era of newer solutions like mobile-app OTP.</span></p>
<p><a href="https://inero-software.com/best-keycloak-practices/"><img loading="lazy" decoding="async" data-attachment-id="5833" data-permalink="https://inero-software.com/multi-factor-authentication-in-keycloak/4-4/" data-orig-file="https://inero-software.com/wp-content/uploads/2024/06/4-2.png" data-orig-size="1200,100" data-comments-opened="0" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="4" data-image-description="" data-image-caption="" data-medium-file="https://inero-software.com/wp-content/uploads/2024/06/4-2-300x25.png" data-large-file="https://inero-software.com/wp-content/uploads/2024/06/4-2-1030x86.png" tabindex="0" role="button" class="alignnone wp-image-5833 size-full" src="https://inero-software.com/wp-content/uploads/2024/06/4-2.png" alt="" width="1200" height="100" srcset="https://inero-software.com/wp-content/uploads/2024/06/4-2.png 1200w, https://inero-software.com/wp-content/uploads/2024/06/4-2-300x25.png 300w, https://inero-software.com/wp-content/uploads/2024/06/4-2-1030x86.png 1030w, https://inero-software.com/wp-content/uploads/2024/06/4-2-768x64.png 768w" sizes="(max-width: 1200px) 100vw, 1200px" /></a></p>
<p></p></div><div class="col-sm-1"></div></div>
<p>Artykuł <a href="https://inero-software.com/custom-sms-authenticator-with-keycloak/">Behind the Scenes: Custom SMS Authenticator with Keycloak</a> pochodzi z serwisu <a href="https://inero-software.com">Inero Software - Software Consulting</a>.</p>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">5838</post-id>	</item>
	</channel>
</rss>
