Commit a48fc8e4 authored by Matyáš Latner's avatar Matyáš Latner

ControlServer client secret validation

parent 2db0f041
......@@ -36,6 +36,7 @@ dependencies {
compile 'org.restlet.jse:org.restlet.ext.json:2.1.7'
compile 'org.restlet.jee:org.restlet.ext.servlet:2.1.7'
compile 'net.sf.uadetector:uadetector-resources:2014.09'
compile project(":util:clientSecret")
compile project(':RMBTSharedCode')
}
......
apply plugin: "java"
sourceSets.main.java.srcDirs = [ "src/" ]
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id=":util:clientSecret" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/../../RMBTAndroid" external.system.id="GRADLE" external.system.module.group="Netmetr.util" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
<option name="GRADLE_PROJECT_PATH" value=":util:clientSecret" />
</configuration>
</facet>
<facet type="java-gradle" name="Java-Gradle">
<configuration>
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
<option name="BUILDABLE" value="true" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/build/classes/main" />
<output-test url="file://$MODULE_DIR$/build/classes/test" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
<excludeFolder url="file://$MODULE_DIR$/build" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="clientSecret.clientSecret" level="project" />
</component>
</module>
\ No newline at end of file
package cz.nic.netmetr.clientSecret;
import java.security.MessageDigest;
public class ClientSecretHelper {
public static boolean checkClientSecret(String clientSecretHash, String uuid, String clientName, String versionName, int versionCode, long time, String clientSecret) {
if (isEmptyString(clientSecretHash)) {
return false;
}
return clientSecretHash.equals(prepareClientSecret(uuid, clientName, versionName, versionCode, time, clientSecret));
}
public static String prepareClientSecret(String uuid, String clientName, String versionName, int versionCode, long time, String clientSecret) {
String clientSecretSource = String.format("%s_%s_%s_%s_%s_%s", uuid, clientName, versionName, versionCode, time, clientSecret);
return calculateSha256Hash(clientSecretSource);
}
private static String calculateSha256Hash(String input) {
try{
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(input.getBytes("UTF-8"));
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < hash.length; i++) {
String hex = Integer.toHexString(0xff & hash[i]);
if(hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
} catch(Exception ex){
throw new RuntimeException(ex);
}
}
private static boolean isEmptyString(String str) {
return str == null || str.length() == 0;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id=":util" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/../RMBTAndroid" external.system.id="GRADLE" external.system.module.group="Netmetr" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
<option name="GRADLE_PROJECT_PATH" value=":util" />
</configuration>
</facet>
<facet type="java-gradle" name="Java-Gradle">
<configuration>
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
<option name="BUILDABLE" value="false" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
<excludeFolder url="file://$MODULE_DIR$/build" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment