|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 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.web.webauthn.jackson; |
| 18 | + |
| 19 | +import com.fasterxml.jackson.core.JsonParser; |
| 20 | +import com.fasterxml.jackson.core.JsonToken; |
| 21 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 22 | +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; |
| 23 | +import org.springframework.security.web.webauthn.api.AuthenticatorAttachment; |
| 24 | +import org.springframework.security.web.webauthn.api.AuthenticatorSelectionCriteria; |
| 25 | +import org.springframework.security.web.webauthn.api.AuthenticatorSelectionCriteria.AuthenticatorSelectionCriteriaBuilder; |
| 26 | +import org.springframework.security.web.webauthn.api.ResidentKeyRequirement; |
| 27 | +import org.springframework.security.web.webauthn.api.UserVerificationRequirement; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | + |
| 31 | +/** |
| 32 | + * Jackson deserializer for {@link AuthenticatorSelectionCriteria} |
| 33 | + * |
| 34 | + * @author Justin Cranford |
| 35 | + * @since 6.5 |
| 36 | + */ |
| 37 | +@SuppressWarnings("serial") |
| 38 | +class AuthenticatorSelectionCriteriaDeserializer extends StdDeserializer<AuthenticatorSelectionCriteria> { |
| 39 | + |
| 40 | + AuthenticatorSelectionCriteriaDeserializer() { |
| 41 | + super(AuthenticatorSelectionCriteria.class); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public AuthenticatorSelectionCriteria deserialize(JsonParser parser, DeserializationContext ctxt) |
| 46 | + throws IOException { |
| 47 | + final AuthenticatorSelectionCriteriaBuilder builder = AuthenticatorSelectionCriteria.builder(); |
| 48 | + while (parser.nextToken() != JsonToken.END_OBJECT) { |
| 49 | + final String fieldName = parser.currentName(); |
| 50 | + parser.nextToken(); |
| 51 | + if ("authenticatorAttachment".equals(fieldName)) { |
| 52 | + builder.authenticatorAttachment(AuthenticatorAttachment.valueOf(parser.getText())); |
| 53 | + } else if ("residentKey".equals(fieldName)) { |
| 54 | + builder.residentKey(ResidentKeyRequirement.valueOf(parser.getText())); |
| 55 | + } else if ("userVerification".equals(fieldName)) { |
| 56 | + builder.userVerification(UserVerificationRequirement.valueOf(parser.getText())); |
| 57 | + } else { |
| 58 | + throw new IOException("Unsupported field name: " + fieldName); |
| 59 | + } |
| 60 | + } |
| 61 | + return builder.build(); |
| 62 | + } |
| 63 | + |
| 64 | +} |
0 commit comments