Skip to content

Commit

Permalink
initial code for keycloak scrypt provider
Browse files Browse the repository at this point in the history
  • Loading branch information
aldyco committed Jul 1, 2019
1 parent 922f8ea commit d8f40d2
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 0 deletions.
62 changes: 62 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Java related
*jar
*.class

# Eclipse
.classpath
.project
.settings

# Leiningen
classes/
lib/
native/
checkouts/
target/
.lein-deps-sum
.lein-failures
.lein-repl-history
.lein-cljsbuild-repl
.lein-plugins/
repl-port
.nrepl-port
.repl
.lein-deps-tree
.lein-env

# Temp Files
tmp/
*.orig
*~
.*.swp
.*.swo
*.tmp
*.bak
[#]*[#]
.\#*
.vscode/

# Editors (IntelliJ / Eclipse)
*/.idea
*/.classpath
*/.project
*/.settings
*.iml
.vscode/

# OS X
.DS_Store

# Logging
*.log
logs/

# Docs
autodoc/
docs



# hadi
.directory
.idea/
52 changes: 52 additions & 0 deletions dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zeniuseducation</groupId>
<artifactId>keycloak-scrypt</artifactId>
<name>Keycloak Scrypt</name>
<version>0.1.0</version>
<description>Scrypt Password Hash Algorithm</description>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer>
<mainClass>net.zenius.keycloak.providers.credential.hash.Scrypt</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

75 changes: 75 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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>
<groupId>com.zeniuseducation</groupId>
<artifactId>keycloak-scrypt</artifactId>
<version>0.1.0</version>
<name>Keycloak Scrypt</name>
<description>Scrypt Password Hash Algorithm</description>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<!-- Run shade goal on package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<!-- add Main-Class to manifest file -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>net.zenius.keycloak.providers.credential.hash.Scrypt</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-core</artifactId>
<version>6.0.1</version>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-server-spi</artifactId>
<version>6.0.1</version>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-server-spi-private</artifactId>
<version>6.0.1</version>
</dependency>
<dependency>
<groupId>com.lambdaworks</groupId>
<artifactId>scrypt</artifactId>
<version>1.4.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.zeniuseducation.keycloak.providers.credential.hash;

import org.keycloak.Config;
import org.keycloak.credential.CredentialModel;
import org.keycloak.credential.hash.PasswordHashProviderFactory;
import org.keycloak.credential.hash.PasswordHashProvider;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.models.PasswordPolicy;
import org.keycloak.models.UserCredentialModel;

import com.lambdaworks.crypto.SCryptUtil;

public class Scrypt implements PasswordHashProviderFactory, PasswordHashProvider {

public static final String ID = "scrypt";

// public CredentialModel encode(String rawPassword, int iterations, Byte salt) {
//
// String encodedPassword = encode(rawPassword, iterations);
//
// CredentialModel credentials = new CredentialModel();
// credentials.setAlgorithm(ID);
// credentials.setType(UserCredentialModel.PASSWORD);
// credentials.setSalt("".getBytes());
// credentials.setHashIterations(iterations);
// credentials.setValue(encodedPassword);
// return credentials;
// }

@Override
public boolean policyCheck(PasswordPolicy policy, CredentialModel credential) {
return credential.getHashIterations() == policy.getHashIterations() && ID.equals(credential.getAlgorithm());
}
@Override
public void encode(String rawPassword, int iterations, CredentialModel credential) {

String encodedPassword = encode(rawPassword, iterations);

credential.setAlgorithm(ID);
credential.setType(UserCredentialModel.PASSWORD);
credential.setSalt("".getBytes());
credential.setHashIterations(iterations);
credential.setValue(encodedPassword);
}

@Override
public boolean verify(String rawPassword, CredentialModel credential) {
return SCryptUtil.check(rawPassword, credential.getValue());
}

@Override
public PasswordHashProvider create(KeycloakSession session) {
return this;
}

@Override
public void init(Config.Scope config) {
}

@Override
public void postInit(KeycloakSessionFactory factory) {
}

public void close() {
}

@Override
public String getId() {
return ID;
}

@Override
public String encode(String rawPassword, int iterations) {
try {
return SCryptUtil.scrypt(rawPassword, iterations, 16, 1);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.zeniuseducation.keycloak.providers.credential.hash.Scrypt

0 comments on commit d8f40d2

Please sign in to comment.