Skip to content
This repository has been archived by the owner on Aug 25, 2024. It is now read-only.

Commit

Permalink
fix: support old jwt configuration params (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoloboschi authored Aug 20, 2024
1 parent 9e3b7a0 commit 17bf492
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,10 @@
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@
package ai.langstream.apigateway.auth.impl.jwt.admin;

import ai.langstream.apigateway.auth.common.store.RevokedTokensStoreConfiguration;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;

public record JwtAuthenticationProviderConfiguration(
@JsonProperty("secret-key") String secretKey,
@JsonProperty("public-key") String publicKey,
@JsonProperty("auth-claim") String authClaim,
@JsonProperty("public-alg") String publicAlg,
@JsonProperty("audience-claim") String audienceClaim,
@JsonAlias("secret-key") String secretKey,
@JsonAlias("public-key") String publicKey,
@JsonAlias("auth-claim") String authClaim,
@JsonAlias("public-alg") String publicAlg,
@JsonAlias("audience-claim") String audienceClaim,
String audience,
@JsonProperty("admin-roles") List<String> adminRoles,
@JsonProperty("jwks-hosts-allowlist") String jwksHostsAllowlist,
@JsonAlias("admin-roles") List<String> adminRoles,
@JsonAlias("jwks-hosts-allowlist") String jwksHostsAllowlist,
@JsonProperty("revoked-tokens-store") RevokedTokensStoreConfiguration revokedTokenStore) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package ai.langstream.apigateway.auth.impl.jwt.admin;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;

class JwtAuthenticationProviderConfigurationTest {

protected static final ObjectMapper yamlConfigReader = new ObjectMapper(new YAMLFactory());

@Test
void parseCamelCase() {
final String yaml = """
adminRoles:
- admin
- super-admin
audience: a
audienceClaim: aud
authClaim: role
publicAlg: RS256
publicKey: --key--
secretKey: --skey--
jwksHostsAllowlist: https://localhost
""";
final JwtAuthenticationProviderConfiguration tokenProperties = parse(yaml);
assertEquals(List.of("admin", "super-admin"), tokenProperties.adminRoles());
assertEquals("a", tokenProperties.audience());
assertEquals("aud", tokenProperties.audienceClaim());
assertEquals("role", tokenProperties.authClaim());
assertEquals("RS256", tokenProperties.publicAlg());
assertEquals("--key--", tokenProperties.publicKey());
assertEquals("https://localhost", tokenProperties.jwksHostsAllowlist());
assertEquals("--skey--", tokenProperties.secretKey());
}

@Test
void parseKebabCase() {
final String yaml = """
admin-roles:
- admin
- super-admin
audience: a
audience-claim: aud
auth-claim: role
public-alg: RS256
public-key: --key--
secret-key: --skey--
jwks-hosts-allowlist: https://localhost
""";
final JwtAuthenticationProviderConfiguration tokenProperties = parse(yaml);
assertEquals(List.of("admin", "super-admin"), tokenProperties.adminRoles());
assertEquals("a", tokenProperties.audience());
assertEquals("aud", tokenProperties.audienceClaim());
assertEquals("role", tokenProperties.authClaim());
assertEquals("RS256", tokenProperties.publicAlg());
assertEquals("--key--", tokenProperties.publicKey());
assertEquals("https://localhost", tokenProperties.jwksHostsAllowlist());
assertEquals("--skey--", tokenProperties.secretKey());
}

@SneakyThrows
private static JwtAuthenticationProviderConfiguration parse(String yaml) {
Map map = yamlConfigReader.readValue(yaml, Map.class);
final JwtAuthenticationProviderConfiguration tokenProperties =
yamlConfigReader.convertValue(map, JwtAuthenticationProviderConfiguration.class);
return tokenProperties;
}
}

0 comments on commit 17bf492

Please sign in to comment.