Skip to content

Issue 48298: Align API Filter Operators #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# The LabKey Remote API Library for Java - Change Log

## version 5.3.0-SNAPSHOT
## version 5.4.0-SNAPSHOT
*Released*: TBD
* Update to Gradle 8.2.1 and adjust away from deprecated syntax

## version 5.3.0
*Released*: 7 September 2023
* Update to Gradle 8.3 and adjust away from deprecated syntax
* Add Ontology and Lineage filter type operators
* Add `TotpManager`, a simple helper that generates TOTP one-time passwords
* Update Commons Codec, HttpCore, and Gradle Plugins versions

## version 5.2.0
*Released*: 3 May 2023
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ repositories {

group "org.labkey.api"

version "5.3.0-SNAPSHOT"
version "5.4.0-SNAPSHOT"

dependencies {
api "org.json:json:${jsonObjectVersion}"
Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ artifactory_contextUrl=https://labkey.jfrog.io/artifactory
sourceCompatibility=17
targetCompatibility=17

gradlePluginsVersion=1.41.0
gradlePluginsVersion=1.41.2

commonsCodecVersion=1.15
commonsCodecVersion=1.16.0
commonsLoggingVersion=1.2

hamcrestVersion=1.3

httpclient5Version=5.2.1
httpcore5Version=5.2.1
httpcore5Version=5.2.2

jsonObjectVersion=20230618

Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
5 changes: 4 additions & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,13 @@ location of your Java installation."
fi
else
JAVACMD=java
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
if ! command -v java >/dev/null 2>&1
then
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
fi

# Increase the maximum file descriptors if we can.
Expand Down
22 changes: 19 additions & 3 deletions src/org/labkey/remoteapi/query/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ public enum Operator
// WARNING: Keep in sync and in order with all other client apis and docs:
// - server: CompareType.java
// - java: Filter.java
// - js: Filter.js
// - js: Types.ts
// - R: makeFilter.R, makeFilter.Rd
// - Python & Perl don't have a filter operator enum
// - Python: query.py
// - Perl doesn't have a filter operator enum

//
// These operators require a data value
Expand Down Expand Up @@ -90,7 +91,22 @@ public enum Operator
//

Q("Search", "q", "Q", true),
WHERE("Where", "where", "WHERE", true)
WHERE("Where", "where", "WHERE", true),

//
// Ontology operators
//

ONTOLOGY_IN_SUBTREE("Is In Subtree", "concept:insubtree", "ONTOLOGY_IN_SUBTREE", true),
ONTOLOGY_NOT_IN_SUBTREE("Is Not In Subtree", "concept:notinsubtree", "ONTOLOGY_NOT_IN_SUBTREE", true),

//
// Lineage operators
//

EXP_CHILD_OF("Is Child Of", "exp:childof", "EXP_CHILD_OF", true),
EXP_PARENT_OF("Is Parent Of", "exp:parentof", "EXP_PARENT_OF", true),
EXP_LINEAGE_OF("In The Lineage Of", "exp:lineageof", "EXP_LINEAGE_OF", true)
;

private static final Map<String, Operator> _programmaticNameToOperator = Arrays.stream(Operator.values())
Expand Down
14 changes: 14 additions & 0 deletions src/org/labkey/remoteapi/totp/CodeGenerationException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright (c) 2019 Sam Stevens
*
* Licensed under the MIT License
*
* https://github.com/samdjstevens v1.7.1
*/
package org.labkey.remoteapi.totp;

public class CodeGenerationException extends Exception {
public CodeGenerationException(String message, Throwable cause) {
super(message, cause);
}
}
84 changes: 84 additions & 0 deletions src/org/labkey/remoteapi/totp/CodeGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2019 Sam Stevens
*
* Licensed under the MIT License
*
* https://github.com/samdjstevens v1.7.1
*/
package org.labkey.remoteapi.totp;

import org.apache.commons.codec.binary.Base32;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.InvalidParameterException;
import java.security.NoSuchAlgorithmException;

public class CodeGenerator
{
private final HashingAlgorithm algorithm;
private final int digits;

public CodeGenerator(HashingAlgorithm algorithm, int digits) {
if (algorithm == null) {
throw new InvalidParameterException("HashingAlgorithm must not be null.");
}
if (digits < 1) {
throw new InvalidParameterException("Number of digits must be higher than 0.");
}

this.algorithm = algorithm;
this.digits = digits;
}

public String generate(String key, long counter) throws CodeGenerationException {
try {
byte[] hash = generateHash(key, counter);
return getDigitsFromHash(hash);
} catch (Exception e) {
throw new CodeGenerationException("Failed to generate code. See nested exception.", e);
}
}

/**
* Generate a HMAC-SHA1 hash of the counter number.
*/
private byte[] generateHash(String key, long counter) throws InvalidKeyException, NoSuchAlgorithmException {
byte[] data = new byte[8];
long value = counter;
for (int i = 8; i-- > 0; value >>>= 8) {
data[i] = (byte) value;
}

// Create a HMAC-SHA1 signing key from the shared key
Base32 codec = new Base32();
byte[] decodedKey = codec.decode(key);
SecretKeySpec signKey = new SecretKeySpec(decodedKey, algorithm.getHmacAlgorithm());
Mac mac = Mac.getInstance(algorithm.getHmacAlgorithm());
mac.init(signKey);

// Create a hash of the counter value
return mac.doFinal(data);
}

/**
* Get the n-digit code for a given hash.
*/
private String getDigitsFromHash(byte[] hash) {
int offset = hash[hash.length - 1] & 0xF;

long truncatedHash = 0;

for (int i = 0; i < 4; ++i) {
truncatedHash <<= 8;
truncatedHash |= (hash[offset + i] & 0xFF);
}

truncatedHash &= 0x7FFFFFFF;
truncatedHash %= Math.pow(10, digits);

// Left pad with 0s for a n-digit code
return String.format("%0" + digits + "d", truncatedHash);
}
}
32 changes: 32 additions & 0 deletions src/org/labkey/remoteapi/totp/HashingAlgorithm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2019 Sam Stevens
*
* Licensed under the MIT License
*
* https://github.com/samdjstevens v1.7.1
*/

package org.labkey.remoteapi.totp;

public enum HashingAlgorithm {

SHA1("HmacSHA1", "SHA1"), //default
SHA256("HmacSHA256", "SHA256"),
SHA512("HmacSHA512", "SHA512");

private final String hmacAlgorithm;
private final String friendlyName;

HashingAlgorithm(String hmacAlgorithm, String friendlyName) {
this.hmacAlgorithm = hmacAlgorithm;
this.friendlyName = friendlyName;
}

public String getHmacAlgorithm() {
return hmacAlgorithm;
}

public String getFriendlyName() {
return friendlyName;
}
}
16 changes: 16 additions & 0 deletions src/org/labkey/remoteapi/totp/TimeProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright (c) 2019 Sam Stevens
*
* Licensed under the MIT License
*
* https://github.com/samdjstevens v1.7.1
*/
package org.labkey.remoteapi.totp;

import java.time.Instant;

public class TimeProvider {
public long getTime() throws RuntimeException {
return Instant.now().getEpochSecond();
}
}
20 changes: 20 additions & 0 deletions src/org/labkey/remoteapi/totp/TotpManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.labkey.remoteapi.totp;

public class TotpManager
{
// Generate a TOTP one-time password based on the default parameters (30 second time step, 6 digits, SHA1)
public static String generateCode(String secretKey) throws CodeGenerationException
{
return generateCode(secretKey, 30, 6, HashingAlgorithm.SHA1);
}

// Generate a TOTP one-time password using custom parameters
public static String generateCode(String secretKey, int timeStep, int digits, HashingAlgorithm hashingAlgorithm) throws CodeGenerationException
{
TimeProvider timeProvider = new TimeProvider();
long currentBucket = Math.floorDiv(timeProvider.getTime(), timeStep);
CodeGenerator codeGenerator = new CodeGenerator(hashingAlgorithm, digits);

return codeGenerator.generate(secretKey, currentBucket);
}
}