| 1 | /** | |
| 2 |    Copyright 2018 Carlos Macasaet | |
| 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 | package com.macasaet.fernet.jersey; | |
| 17 | ||
| 18 | import static org.glassfish.jersey.server.spi.internal.ValueParamProvider.Priority.NORMAL; | |
| 19 | ||
| 20 | import java.util.function.Function; | |
| 21 | ||
| 22 | import javax.inject.Singleton; | |
| 23 | import javax.ws.rs.NotAuthorizedException; | |
| 24 | ||
| 25 | import org.glassfish.jersey.server.ContainerRequest; | |
| 26 | import org.glassfish.jersey.server.model.Parameter; | |
| 27 | import org.glassfish.jersey.server.spi.internal.ValueParamProvider; | |
| 28 | ||
| 29 | import com.macasaet.fernet.Token; | |
| 30 | import com.macasaet.fernet.jaxrs.FernetToken; | |
| 31 | ||
| 32 | /** | |
| 33 |  * {@link ValueParamProvider} that generates a Fernet {@link Token} from a REST request's auth header. | |
| 34 |  * | |
| 35 |  * <p>Copyright © 2018 Carlos Macasaet.</p> | |
| 36 |  * | |
| 37 |  * @see https://stackoverflow.com/a/50980611/914887 | |
| 38 |  * @author Carlos Macasaet | |
| 39 |  */ | |
| 40 | @Singleton | |
| 41 | class FernetTokenValueParamProvider implements ValueParamProvider { | |
| 42 | ||
| 43 |     private final TokenHeaderUtility tokenHeaderUtility; | |
| 44 | ||
| 45 |     public FernetTokenValueParamProvider() { | |
| 46 |         this(new TokenHeaderUtility()); | |
| 47 |     } | |
| 48 | ||
| 49 |     protected FernetTokenValueParamProvider(final TokenHeaderUtility tokenHeaderUtility) { | |
| 50 | 1
1. <init> : negated conditional → KILLED |         if (tokenHeaderUtility == null) { | 
| 51 |             throw new IllegalArgumentException("tokenHeaderUtility cannot be null"); | |
| 52 |         } | |
| 53 |         this.tokenHeaderUtility = tokenHeaderUtility; | |
| 54 |     } | |
| 55 | ||
| 56 |     public Function<ContainerRequest, Token> getValueProvider(final Parameter parameter) { | |
| 57 | 1
1. getValueProvider : mutated return of Object value for com/macasaet/fernet/jersey/FernetTokenValueParamProvider::getValueProvider to ( if (x != null) null else throw new RuntimeException ) → KILLED |         return request -> { | 
| 58 | 2
1. lambda$getValueProvider$0 : negated conditional → KILLED 2. lambda$getValueProvider$0 : negated conditional → KILLED |             if (parameter.getRawType().equals(Token.class) && parameter.isAnnotationPresent(FernetToken.class)) { | 
| 59 |                 final Token xAuthorizationToken = getTokenHeaderUtility().getXAuthorizationToken(request); | |
| 60 | 1
1. lambda$getValueProvider$0 : negated conditional → KILLED |                 if (xAuthorizationToken != null) { | 
| 61 | 1
1. lambda$getValueProvider$0 : mutated return of Object value for com/macasaet/fernet/jersey/FernetTokenValueParamProvider::lambda$getValueProvider$0 to ( if (x != null) null else throw new RuntimeException ) → KILLED |                     return xAuthorizationToken; | 
| 62 |                 } | |
| 63 |                 final Token authorizationToken = getTokenHeaderUtility().getAuthorizationToken(request); | |
| 64 | 1
1. lambda$getValueProvider$0 : negated conditional → KILLED |                 if (authorizationToken != null) { | 
| 65 | 1
1. lambda$getValueProvider$0 : mutated return of Object value for com/macasaet/fernet/jersey/FernetTokenValueParamProvider::lambda$getValueProvider$0 to ( if (x != null) null else throw new RuntimeException ) → KILLED |                     return authorizationToken; | 
| 66 |                 } | |
| 67 |                 throw new NotAuthorizedException("Bearer error=\"invalid_token\", error_description=\"no token found in Authorization or X-Authorization header\""); | |
| 68 |             } | |
| 69 |             throw new IllegalStateException("misconfigured annotation"); | |
| 70 |         }; | |
| 71 |     } | |
| 72 | ||
| 73 |     public PriorityType getPriority() { | |
| 74 | 1
1. getPriority : mutated return of Object value for com/macasaet/fernet/jersey/FernetTokenValueParamProvider::getPriority to ( if (x != null) null else throw new RuntimeException ) → KILLED |         return NORMAL; | 
| 75 |     } | |
| 76 | ||
| 77 |     protected TokenHeaderUtility getTokenHeaderUtility() { | |
| 78 | 1
1. getTokenHeaderUtility : mutated return of Object value for com/macasaet/fernet/jersey/FernetTokenValueParamProvider::getTokenHeaderUtility to ( if (x != null) null else throw new RuntimeException ) → KILLED |         return tokenHeaderUtility; | 
| 79 |     } | |
| 80 | ||
| 81 | } | |
| Mutations | ||
| 50 | 1.1 | |
| 57 | 1.1 | |
| 58 | 1.1 2.2 | |
| 60 | 1.1 | |
| 61 | 1.1 | |
| 64 | 1.1 | |
| 65 | 1.1 | |
| 74 | 1.1 | |
| 78 | 1.1 |