|
| 1 | +/* |
| 2 | + * Copyright (C) 2018 Google Inc. |
| 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 | + |
| 17 | +package org.oidc.service.oauth2; |
| 18 | + |
| 19 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 20 | + |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import org.oidc.common.ClientAuthenticationMethod; |
| 24 | +import org.oidc.common.EndpointName; |
| 25 | +import org.oidc.common.HttpMethod; |
| 26 | +import org.oidc.common.MessageType; |
| 27 | +import org.oidc.common.MissingRequiredAttributeException; |
| 28 | +import org.oidc.common.SerializationType; |
| 29 | +import org.oidc.common.ServiceName; |
| 30 | +import org.oidc.common.UnsupportedSerializationTypeException; |
| 31 | +import org.oidc.common.ValueException; |
| 32 | +import org.oidc.msg.InvalidClaimException; |
| 33 | +import org.oidc.msg.Message; |
| 34 | +import org.oidc.msg.SerializationException; |
| 35 | +import org.oidc.msg.oauth2.AccessTokenRequest; |
| 36 | +import org.oidc.msg.oauth2.AccessTokenResponse; |
| 37 | +import org.oidc.service.AbstractService; |
| 38 | +import org.oidc.service.base.HttpArguments; |
| 39 | +import org.oidc.service.base.ServiceConfig; |
| 40 | +import org.oidc.service.base.ServiceContext; |
| 41 | +import org.oidc.service.data.State; |
| 42 | + |
| 43 | +public class AccessToken extends AbstractService { |
| 44 | + |
| 45 | + public AccessToken(ServiceContext serviceContext, State state, ServiceConfig serviceConfig) { |
| 46 | + super(serviceContext, state, serviceConfig); |
| 47 | + this.serviceName = ServiceName.ACCESS_TOKEN; |
| 48 | + this.endpointName = EndpointName.TOKEN; |
| 49 | + this.requestMessage = new AccessTokenRequest(); |
| 50 | + this.responseMessage = new AccessTokenResponse(); |
| 51 | + this.isSynchronous = true; |
| 52 | + this.serializationType = SerializationType.URL_ENCODED; |
| 53 | + this.deserializationType = SerializationType.JSON; |
| 54 | + this.httpMethod = HttpMethod.POST; |
| 55 | + this.expectedResponseClass = AccessTokenResponse.class; |
| 56 | + this.defaultAuthenticationMethod = ClientAuthenticationMethod.CLIENT_SECRET_BASIC; |
| 57 | + |
| 58 | + /** TODO: Add preCosntructors |
| 59 | + * 1. verify state parameter exists as req param or as preContrs param |
| 60 | + * 2. get with state auth req and response. Extends your arguments with request/response |
| 61 | + * 3. Set grant_type if not set |
| 62 | + this.preConstructors = (List<RequestArgumentProcessor>) Arrays.asList(...); |
| 63 | + */ |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected void doUpdateServiceContext(Message response, String stateKey) |
| 69 | + throws MissingRequiredAttributeException, ValueException, InvalidClaimException { |
| 70 | + if (!(responseMessage instanceof AccessTokenResponse)) { |
| 71 | + throw new ValueException("response not instance of AccessTokenResponse"); |
| 72 | + } |
| 73 | + if (responseMessage.getClaims().containsKey("expires_in")) { |
| 74 | + responseMessage.getClaims().put("__expires_at", (System.currentTimeMillis() / 1000) |
| 75 | + + (long) responseMessage.getClaims().get("expires_in")); |
| 76 | + } |
| 77 | + getState().storeItem(response, stateKey, MessageType.TOKEN_RESPONSE); |
| 78 | + } |
| 79 | + |
| 80 | + public HttpArguments finalizeGetRequestParameters(HttpArguments httpArguments, |
| 81 | + Map<String, Object> requestArguments) |
| 82 | + throws ValueException, MissingRequiredAttributeException, JsonProcessingException, |
| 83 | + UnsupportedSerializationTypeException, SerializationException, InvalidClaimException { |
| 84 | + |
| 85 | + return httpArguments; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + protected Message doConstructRequest(Map<String, Object> requestArguments) |
| 90 | + throws MissingRequiredAttributeException { |
| 91 | + return new AccessTokenRequest(requestArguments); |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments