OAuth2AccessTokenImpl.java

  1. /*******************************************************************************
  2.  * Copyright 2017 The MIT Internet Trust Consortium
  3.  *
  4.  * Portions copyright 2011-2013 The MITRE Corporation
  5.  *
  6.  * Licensed under the Apache License, Version 2.0 (the "License");
  7.  * you may not use this file except in compliance with the License.
  8.  * You may obtain a copy of the License at
  9.  *
  10.  *   http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing, software
  13.  * distributed under the License is distributed on an "AS IS" BASIS,
  14.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15.  * See the License for the specific language governing permissions and
  16.  * limitations under the License.
  17.  *******************************************************************************/
  18. package org.mitre.oauth2.introspectingfilter;

  19. import java.util.Date;
  20. import java.util.HashSet;
  21. import java.util.Map;
  22. import java.util.Set;
  23. import java.util.concurrent.TimeUnit;

  24. import org.springframework.security.oauth2.common.OAuth2AccessToken;
  25. import org.springframework.security.oauth2.common.OAuth2RefreshToken;

  26. import com.google.common.base.Splitter;
  27. import com.google.common.collect.Sets;
  28. import com.google.gson.JsonObject;


  29. public class OAuth2AccessTokenImpl implements OAuth2AccessToken {

  30.     private JsonObject introspectionResponse;
  31.     private String tokenString;
  32.     private Set<String> scopes = new HashSet<>();
  33.     private Date expireDate;


  34.     public OAuth2AccessTokenImpl(JsonObject introspectionResponse, String tokenString) {
  35.         this.setIntrospectionResponse(introspectionResponse);
  36.         this.tokenString = tokenString;
  37.         if (introspectionResponse.get("scope") != null) {
  38.             scopes = Sets.newHashSet(Splitter.on(" ").split(introspectionResponse.get("scope").getAsString()));
  39.         }

  40.         if (introspectionResponse.get("exp") != null) {
  41.             expireDate = new Date(introspectionResponse.get("exp").getAsLong() * 1000L);
  42.         }
  43.     }


  44.     @Override
  45.     public Map<String, Object> getAdditionalInformation() {
  46.         return null;
  47.     }

  48.     @Override
  49.     public Set<String> getScope() {
  50.         return scopes;
  51.     }

  52.     @Override
  53.     public OAuth2RefreshToken getRefreshToken() {
  54.         return null;
  55.     }

  56.     @Override
  57.     public String getTokenType() {
  58.         return BEARER_TYPE;
  59.     }

  60.     @Override
  61.     public boolean isExpired() {
  62.         if (expireDate != null && expireDate.before(new Date())) {
  63.             return true;
  64.         }
  65.         return false;
  66.     }

  67.     @Override
  68.     public Date getExpiration() {
  69.         return expireDate;
  70.     }

  71.     @Override
  72.     public int getExpiresIn() {
  73.         if (expireDate != null) {
  74.             return (int)TimeUnit.MILLISECONDS.toSeconds(expireDate.getTime() - (new Date()).getTime());
  75.         }
  76.         return 0;
  77.     }

  78.     @Override
  79.     public String getValue() {
  80.         return tokenString;
  81.     }


  82.     /**
  83.      * @return the token
  84.      */
  85.     public JsonObject getIntrospectionResponse() {
  86.         return introspectionResponse;
  87.     }


  88.     /**
  89.      * @param token the token to set
  90.      */
  91.     public void setIntrospectionResponse(JsonObject token) {
  92.         this.introspectionResponse = token;
  93.     }

  94. }