-
Notifications
You must be signed in to change notification settings - Fork 160
Remove jwt lib dependency #2833
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,99 @@ | ||
| /* | ||
| * (C) Copyright IBM Corp. 2021 | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package com.ibm.fhir.smart; | ||
|
|
||
| import java.io.StringReader; | ||
| import java.util.Base64; | ||
| import java.util.Base64.Decoder; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import jakarta.json.Json; | ||
| import jakarta.json.JsonObject; | ||
| import jakarta.json.JsonReaderFactory; | ||
| import jakarta.json.JsonString; | ||
| import jakarta.json.JsonValue; | ||
| import jakarta.json.JsonValue.ValueType; | ||
|
|
||
| /** | ||
| * A minimal alternative to the com.auth0:java-jwt library. | ||
| * This flavor mimics the java-jwt API but implement only the [very small] subset of functionality that is | ||
| * actually needed by the fhir-smart policy enforcement. | ||
| */ | ||
| public class JWT { | ||
| private static final JsonReaderFactory JSON_READER_FACTORY = Json.createReaderFactory(null); | ||
| private static final Decoder decoder = Base64.getDecoder(); | ||
|
|
||
| public static DecodedJWT decode(String jwt) { | ||
| String[] parts = jwt.split("\\."); | ||
| if (parts.length < 2 || parts.length > 3 || (parts.length == 2 && !jwt.endsWith("."))) { | ||
| throw new IllegalArgumentException("Invalid JWT: expected 3 parts but found " + parts.length); | ||
| } | ||
|
|
||
| return new DecodedJWT( | ||
| new String(decoder.decode(parts[0])), | ||
| new String(decoder.decode(parts[1])), | ||
| parts.length == 2 ? null : new String(decoder.decode(parts[2]).toString()) | ||
| ); | ||
| } | ||
|
|
||
| public static class DecodedJWT { | ||
| final JsonObject header; | ||
| final JsonObject data; | ||
| final JsonObject signature; | ||
|
|
||
| private DecodedJWT(String header, String data, String signature) { | ||
| this.header = JSON_READER_FACTORY.createReader(new StringReader(header)).readObject(); | ||
| this.data = JSON_READER_FACTORY.createReader(new StringReader(data)).readObject(); | ||
| this.signature = signature == null ? null : | ||
| JSON_READER_FACTORY.createReader(new StringReader(signature)).readObject(); | ||
| } | ||
|
|
||
| public Claim getClaim(String name) { | ||
| return new Claim(data.get(name)); | ||
| } | ||
| } | ||
|
|
||
| public static class Claim { | ||
| final JsonValue value; | ||
|
|
||
| private Claim(JsonValue value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public String asString() { | ||
| if (value == null) return null; | ||
|
|
||
| switch (value.getValueType()) { | ||
| case STRING: | ||
| return ((JsonString) value).getString(); | ||
| case TRUE: | ||
| case FALSE: | ||
| case NUMBER: | ||
| case NULL: | ||
| case OBJECT: | ||
| case ARRAY: | ||
| default: | ||
| return value.toString(); | ||
| } | ||
| } | ||
|
|
||
| public List<String> asList() { | ||
| List<String> list = value.asJsonArray().getValuesAs(JsonString.class).stream() | ||
| .map(s -> s.getString()) | ||
| .collect(Collectors.toList()); | ||
| return Collections.unmodifiableList(list); | ||
| } | ||
|
|
||
| public boolean isNull() { | ||
| if (value == null || value.getValueType() == ValueType.NULL) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
fhir-smart/src/test/java/com/ibm/fhir/smart/test/JWTTest.java
This file contains hidden or 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,39 @@ | ||
| /* | ||
| * (C) Copyright IBM Corp. 2021 | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package com.ibm.fhir.smart.test; | ||
|
|
||
| import static org.testng.Assert.assertEquals; | ||
| import static org.testng.Assert.assertTrue; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import org.testng.annotations.Test; | ||
|
|
||
| import com.auth0.jwt.algorithms.Algorithm; | ||
| import com.ibm.fhir.smart.JWT; | ||
| import com.ibm.fhir.smart.JWT.DecodedJWT; | ||
|
|
||
| /** | ||
| * | ||
| */ | ||
| public class JWTTest { | ||
| private String testString = "value"; | ||
| private List<String> testList = Arrays.asList(new String[] {"value1", "value2"}); | ||
|
|
||
| private String jwt = com.auth0.jwt.JWT.create() | ||
| .withClaim("string", testString) | ||
| .withClaim("array", testList) | ||
| .sign(Algorithm.none()); | ||
|
|
||
| @Test | ||
| public void test() { | ||
| DecodedJWT decodedJwt = JWT.decode(jwt); | ||
| assertEquals(decodedJwt.getClaim("string").asString(), testString); | ||
| assertEquals(decodedJwt.getClaim("array").asList(), testList); | ||
| assertTrue(decodedJwt.getClaim("bogus").isNull()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.