-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from yogeswaran-htc-git/twofactor-api-factory-…
…integration Two factor SMS factory integration
- Loading branch information
Showing
7 changed files
with
197 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Twilio SMS Sender Provider | ||
|
||
**Not verify in Quarkus 19.0.1** | ||
|
||
```sh | ||
cp target/providers/keycloak-phone-provider.jar ${KEYCLOAK_HOME}/providers/ | ||
cp target/providers/keycloak-phone-provider.resources.jar ${KEYCLOAK_HOME}/providers/ | ||
cp target/providers/keycloak-sms-provider-twilio.jar ${KEYCLOAK_HOME}/providers/ | ||
|
||
|
||
${KEYCLOAK_HOME}/bin/kc.sh build | ||
|
||
${KEYCLOAK_HOME}/bin/kc.sh start --spi-phone-default-service=twilio \ | ||
--spi-message-sender-service-twilio-account=${account} \ | ||
--spi-message-sender-service-twilio-token=${token} \ | ||
--spi-message-sender-service-twilio-number=${servicePhoneNumber} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>cc.coopersoft</groupId> | ||
<artifactId>keycloak-phone-provider-parent</artifactId> | ||
<version>2.3.4-snapshot</version> | ||
</parent> | ||
|
||
<artifactId>keycloak-sms-provider-twofactorapi</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>cc.coopersoft</groupId> | ||
<artifactId>keycloak-phone-provider</artifactId> | ||
<version>2.3.4-snapshot</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.twilio.sdk</groupId> | ||
<artifactId>twilio</artifactId> | ||
<version>9.2.4</version> | ||
</dependency> | ||
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp --> | ||
<dependency> | ||
<groupId>com.squareup.okhttp3</groupId> | ||
<artifactId>okhttp</artifactId> | ||
<version>4.10.0</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-assembly-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>single</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<descriptorRefs> | ||
<descriptorRef>jar-with-dependencies</descriptorRef> | ||
</descriptorRefs> | ||
<finalName>${project.build.finalName}</finalName> | ||
<appendAssemblyId>false</appendAssemblyId> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-dependency-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>copy</goal> | ||
</goals> | ||
<configuration> | ||
<artifactItems> | ||
<artifactItem> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>${project.artifactId}</artifactId> | ||
<version>${project.version}</version> | ||
</artifactItem> | ||
</artifactItems> | ||
<outputDirectory>../target/providers</outputDirectory> | ||
<stripClassifier>true</stripClassifier> | ||
<stripVersion>true</stripVersion> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
35 changes: 35 additions & 0 deletions
35
...persoft/keycloak/phone/providers/sender/TwoFactorMessageSenderServiceProviderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package cc.coopersoft.keycloak.phone.providers.sender; | ||
|
||
import cc.coopersoft.keycloak.phone.providers.spi.MessageSenderService; | ||
import cc.coopersoft.keycloak.phone.providers.spi.MessageSenderServiceProviderFactory; | ||
import org.keycloak.Config.Scope; | ||
import org.keycloak.models.KeycloakSession; | ||
import org.keycloak.models.KeycloakSessionFactory; | ||
|
||
public class TwoFactorMessageSenderServiceProviderFactory implements MessageSenderServiceProviderFactory { | ||
|
||
private Scope config; | ||
|
||
@Override | ||
public MessageSenderService create(KeycloakSession session) { | ||
return new TwoFactorSmsSenderServiceProvider(config,session.getContext().getRealm().getDisplayName()); | ||
} | ||
|
||
@Override | ||
public void init(Scope config) { | ||
this.config = config; | ||
} | ||
|
||
@Override | ||
public void postInit(KeycloakSessionFactory keycloakSessionFactory) { | ||
} | ||
|
||
@Override | ||
public void close() { | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return "two-factor"; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...java/cc/coopersoft/keycloak/phone/providers/sender/TwoFactorSmsSenderServiceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package cc.coopersoft.keycloak.phone.providers.sender; | ||
|
||
import cc.coopersoft.keycloak.phone.providers.exception.MessageSendException; | ||
import cc.coopersoft.keycloak.phone.providers.spi.FullSmsSenderAbstractService; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import org.jboss.logging.Logger; | ||
import org.keycloak.Config.Scope; | ||
|
||
import javax.annotation.PostConstruct; | ||
|
||
public class TwoFactorSmsSenderServiceProvider extends FullSmsSenderAbstractService { | ||
|
||
private static final Logger logger = Logger.getLogger(TwoFactorSmsSenderServiceProvider.class); | ||
private String twoFactorApiKey; | ||
private static final String twoFactorUrl = "https://2factor.in/API/V1/"; | ||
private OkHttpClient client; | ||
|
||
@PostConstruct | ||
public void doSetUp() { | ||
client = new OkHttpClient().newBuilder() | ||
.build(); | ||
} | ||
|
||
TwoFactorSmsSenderServiceProvider(Scope config, String realmDisplay) { | ||
super(realmDisplay); | ||
this.twoFactorApiKey = config.get("twoFactorApiKey"); | ||
|
||
} | ||
|
||
@Override | ||
public void sendMessage(String phoneNumber, String message) throws MessageSendException { | ||
|
||
Request request = new Request.Builder() | ||
.url(twoFactorUrl + twoFactorApiKey + "/SMS/" + phoneNumber + "/AUTOGEN/OTP1") | ||
.get() | ||
.build(); | ||
try (Response response = client.newCall(request).execute()) { | ||
String responseString = response.body().string(); | ||
if (response.isSuccessful()) { | ||
logger.info(responseString + ": sms sent successfully"); | ||
} else { | ||
logger.error(responseString + ": sms sending failed"); | ||
throw new MessageSendException(response.code(), | ||
String.valueOf(response.code()), | ||
response.message()); | ||
} | ||
} catch (Exception e) { | ||
logger.error(e.getMessage()); | ||
throw new MessageSendException(400, | ||
String.valueOf(400), | ||
e.getMessage()); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...F/services/cc.coopersoft.keycloak.phone.providers.spi.MessageSenderServiceProviderFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
cc.coopersoft.keycloak.phone.providers.sender.TwilioMessageSenderServiceProviderFactory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters