|
| 1 | +/* |
| 2 | + * Copyright 2020-Present The Serverless Workflow Specification Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.serverlessworkflow.api.deserializers; |
| 17 | + |
| 18 | +import com.fasterxml.jackson.core.JsonParser; |
| 19 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 20 | +import com.fasterxml.jackson.databind.JsonNode; |
| 21 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; |
| 23 | +import io.serverlessworkflow.api.auth.AuthDefinition; |
| 24 | +import io.serverlessworkflow.api.auth.BasicAuthDefinition; |
| 25 | +import io.serverlessworkflow.api.auth.BearerAuthDefinition; |
| 26 | +import io.serverlessworkflow.api.auth.OauthDefinition; |
| 27 | +import io.serverlessworkflow.api.interfaces.WorkflowPropertySource; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | + |
| 31 | +public class AuthDefinitionDeserializer extends StdDeserializer<AuthDefinition> { |
| 32 | + |
| 33 | + private static final long serialVersionUID = 510l; |
| 34 | + |
| 35 | + @SuppressWarnings("unused") |
| 36 | + private WorkflowPropertySource context; |
| 37 | + |
| 38 | + public AuthDefinitionDeserializer() { |
| 39 | + this(AuthDefinition.class); |
| 40 | + } |
| 41 | + |
| 42 | + public AuthDefinitionDeserializer(Class<?> vc) { |
| 43 | + super(vc); |
| 44 | + } |
| 45 | + |
| 46 | + public AuthDefinitionDeserializer(WorkflowPropertySource context) { |
| 47 | + this(AuthDefinition.class); |
| 48 | + this.context = context; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public AuthDefinition deserialize(JsonParser jp, |
| 53 | + DeserializationContext ctxt) throws IOException { |
| 54 | + |
| 55 | + ObjectMapper mapper = (ObjectMapper) jp.getCodec(); |
| 56 | + JsonNode node = jp.getCodec().readTree(jp); |
| 57 | + |
| 58 | + AuthDefinition authDefinition = new AuthDefinition(); |
| 59 | + |
| 60 | + if(node.get("name") != null) { |
| 61 | + authDefinition.setName(node.get("name").asText()); |
| 62 | + } |
| 63 | + |
| 64 | + if(node.get("scheme") != null) { |
| 65 | + authDefinition.setScheme(AuthDefinition.Scheme.fromValue(node.get("scheme").asText())); |
| 66 | + } |
| 67 | + |
| 68 | + if(node.get("properties") != null) { |
| 69 | + JsonNode propsNode = node.get("properties"); |
| 70 | + |
| 71 | + if(propsNode.get("grantType") != null) { |
| 72 | + authDefinition.setOauth(mapper.treeToValue(propsNode, OauthDefinition.class)); |
| 73 | + } else if(propsNode.get("token") != null) { |
| 74 | + authDefinition.setBearerauth(mapper.treeToValue(propsNode, BearerAuthDefinition.class)); |
| 75 | + } else { |
| 76 | + authDefinition.setBasicauth(mapper.treeToValue(propsNode, BasicAuthDefinition.class)); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return authDefinition; |
| 81 | + } |
| 82 | +} |
0 commit comments