|
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | | -package io.serverlessworkflow.impl.http.jwt; |
| 16 | +package io.serverlessworkflow.impl.executors.http.oauth.jackson; |
17 | 17 |
|
18 | | -import com.fasterxml.jackson.core.JsonProcessingException; |
19 | | -import com.fasterxml.jackson.databind.ObjectMapper; |
20 | | -import io.serverlessworkflow.http.jwt.JWT; |
21 | | -import io.serverlessworkflow.http.jwt.JWTConverter; |
| 18 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 19 | +import io.serverlessworkflow.impl.executors.http.oauth.JWT; |
| 20 | +import io.serverlessworkflow.impl.executors.http.oauth.JWTConverter; |
| 21 | +import io.serverlessworkflow.impl.jackson.JsonUtils; |
| 22 | +import java.io.IOException; |
22 | 23 | import java.nio.charset.StandardCharsets; |
23 | 24 | import java.util.Base64; |
24 | 25 | import java.util.Map; |
25 | 26 |
|
26 | 27 | public class JacksonJWTConverter implements JWTConverter { |
27 | 28 |
|
28 | | - private static final ObjectMapper MAPPER = new ObjectMapper(); |
29 | | - |
30 | 29 | @Override |
31 | 30 | public JWT fromToken(String token) throws IllegalArgumentException { |
32 | 31 | if (token == null || token.isBlank()) { |
33 | 32 | throw new IllegalArgumentException("JWT token must not be null or blank"); |
34 | 33 | } |
35 | | - |
36 | 34 | String[] parts = token.split("\\."); |
37 | 35 | if (parts.length < 2) { |
38 | | - throw new IllegalArgumentException("Invalid JWT token format"); |
| 36 | + throw new IllegalArgumentException( |
| 37 | + "Invalid JWT token format. There should at least two parts separated by :"); |
39 | 38 | } |
40 | | - try { |
41 | | - String headerJson = |
42 | | - new String(Base64.getUrlDecoder().decode(parts[0]), StandardCharsets.UTF_8); |
43 | | - String payloadJson = |
44 | | - new String(Base64.getUrlDecoder().decode(parts[1]), StandardCharsets.UTF_8); |
45 | | - |
46 | | - Map<String, Object> header = MAPPER.readValue(headerJson, Map.class); |
47 | | - Map<String, Object> claims = MAPPER.readValue(payloadJson, Map.class); |
| 39 | + return new JacksonJWTImpl(token, fromPart2Map(parts[0]), fromPart2Map(parts[1])); |
| 40 | + } |
48 | 41 |
|
49 | | - return new JacksonJWTImpl(token, header, claims); |
50 | | - } catch (JsonProcessingException e) { |
51 | | - throw new IllegalArgumentException("Failed to parse JWT token payload: " + e.getMessage(), e); |
| 42 | + private static final Map<String, Object> fromPart2Map(String part) { |
| 43 | + String decoded = new String(Base64.getUrlDecoder().decode(part), StandardCharsets.UTF_8); |
| 44 | + try { |
| 45 | + return JsonUtils.mapper().readValue(decoded, new TypeReference<Map<String, Object>>() {}); |
| 46 | + } catch (IOException e) { |
| 47 | + throw new IllegalArgumentException( |
| 48 | + "Invalid JTW token format. " + decoded + " is not a valid json", e); |
52 | 49 | } |
53 | 50 | } |
54 | 51 | } |
0 commit comments