|
| 1 | +/* |
| 2 | + * Copyright 2002-2020 the original author or 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 | + * https://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 | + |
| 17 | +package org.springframework.security.oauth2.server.authorization.jackson2; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | + |
| 21 | +import com.fasterxml.jackson.core.JsonParseException; |
| 22 | +import com.fasterxml.jackson.core.JsonParser; |
| 23 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 24 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
| 25 | +import com.fasterxml.jackson.databind.JsonNode; |
| 26 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 27 | +import com.fasterxml.jackson.databind.util.StdConverter; |
| 28 | + |
| 29 | +import org.springframework.security.oauth2.core.AuthorizationGrantType; |
| 30 | +import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest; |
| 31 | +import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest.Builder; |
| 32 | + |
| 33 | +/** |
| 34 | + * A {@code JsonDeserializer} for {@link OAuth2AuthorizationRequest}. |
| 35 | + * |
| 36 | + * @author Joe Grandja |
| 37 | + * @since 0.1.2 |
| 38 | + * @see OAuth2AuthorizationRequest |
| 39 | + * @see OAuth2AuthorizationRequestMixin |
| 40 | + */ |
| 41 | +final class OAuth2AuthorizationRequestDeserializer extends JsonDeserializer<OAuth2AuthorizationRequest> { |
| 42 | + |
| 43 | + private static final StdConverter<JsonNode, AuthorizationGrantType> AUTHORIZATION_GRANT_TYPE_CONVERTER = new StdConverters.AuthorizationGrantTypeConverter(); |
| 44 | + |
| 45 | + @Override |
| 46 | + public OAuth2AuthorizationRequest deserialize(JsonParser parser, DeserializationContext context) |
| 47 | + throws IOException { |
| 48 | + ObjectMapper mapper = (ObjectMapper) parser.getCodec(); |
| 49 | + JsonNode root = mapper.readTree(parser); |
| 50 | + return deserialize(parser, mapper, root); |
| 51 | + } |
| 52 | + |
| 53 | + private OAuth2AuthorizationRequest deserialize(JsonParser parser, ObjectMapper mapper, JsonNode root) |
| 54 | + throws JsonParseException { |
| 55 | + AuthorizationGrantType authorizationGrantType = AUTHORIZATION_GRANT_TYPE_CONVERTER |
| 56 | + .convert(JsonNodeUtils.findObjectNode(root, "authorizationGrantType")); |
| 57 | + Builder builder = getBuilder(parser, authorizationGrantType); |
| 58 | + builder.authorizationUri(JsonNodeUtils.findStringValue(root, "authorizationUri")); |
| 59 | + builder.clientId(JsonNodeUtils.findStringValue(root, "clientId")); |
| 60 | + builder.redirectUri(JsonNodeUtils.findStringValue(root, "redirectUri")); |
| 61 | + builder.scopes(JsonNodeUtils.findValue(root, "scopes", JsonNodeUtils.STRING_SET, mapper)); |
| 62 | + builder.state(JsonNodeUtils.findStringValue(root, "state")); |
| 63 | + builder.additionalParameters( |
| 64 | + JsonNodeUtils.findValue(root, "additionalParameters", JsonNodeUtils.STRING_OBJECT_MAP, mapper)); |
| 65 | + builder.authorizationRequestUri(JsonNodeUtils.findStringValue(root, "authorizationRequestUri")); |
| 66 | + builder.attributes(JsonNodeUtils.findValue(root, "attributes", JsonNodeUtils.STRING_OBJECT_MAP, mapper)); |
| 67 | + return builder.build(); |
| 68 | + } |
| 69 | + |
| 70 | + private Builder getBuilder(JsonParser parser, |
| 71 | + AuthorizationGrantType authorizationGrantType) throws JsonParseException { |
| 72 | + if (AuthorizationGrantType.AUTHORIZATION_CODE.equals(authorizationGrantType)) { |
| 73 | + return OAuth2AuthorizationRequest.authorizationCode(); |
| 74 | + } |
| 75 | + if (AuthorizationGrantType.IMPLICIT.equals(authorizationGrantType)) { |
| 76 | + return OAuth2AuthorizationRequest.implicit(); |
| 77 | + } |
| 78 | + throw new JsonParseException(parser, "Invalid authorizationGrantType"); |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments