001/******************************************************************************* 002 * Copyright 2017 The MIT Internet Trust Consortium 003 * 004 * Portions copyright 2011-2013 The MITRE Corporation 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 *******************************************************************************/ 018package org.mitre.oauth2.introspectingfilter; 019 020import java.util.Date; 021import java.util.HashSet; 022import java.util.Map; 023import java.util.Set; 024import java.util.concurrent.TimeUnit; 025 026import org.springframework.security.oauth2.common.OAuth2AccessToken; 027import org.springframework.security.oauth2.common.OAuth2RefreshToken; 028 029import com.google.common.base.Splitter; 030import com.google.common.collect.Sets; 031import com.google.gson.JsonObject; 032 033 034public class OAuth2AccessTokenImpl implements OAuth2AccessToken { 035 036 private JsonObject introspectionResponse; 037 private String tokenString; 038 private Set<String> scopes = new HashSet<>(); 039 private Date expireDate; 040 041 042 public OAuth2AccessTokenImpl(JsonObject introspectionResponse, String tokenString) { 043 this.setIntrospectionResponse(introspectionResponse); 044 this.tokenString = tokenString; 045 if (introspectionResponse.get("scope") != null) { 046 scopes = Sets.newHashSet(Splitter.on(" ").split(introspectionResponse.get("scope").getAsString())); 047 } 048 049 if (introspectionResponse.get("exp") != null) { 050 expireDate = new Date(introspectionResponse.get("exp").getAsLong() * 1000L); 051 } 052 } 053 054 055 @Override 056 public Map<String, Object> getAdditionalInformation() { 057 return null; 058 } 059 060 @Override 061 public Set<String> getScope() { 062 return scopes; 063 } 064 065 @Override 066 public OAuth2RefreshToken getRefreshToken() { 067 return null; 068 } 069 070 @Override 071 public String getTokenType() { 072 return BEARER_TYPE; 073 } 074 075 @Override 076 public boolean isExpired() { 077 if (expireDate != null && expireDate.before(new Date())) { 078 return true; 079 } 080 return false; 081 } 082 083 @Override 084 public Date getExpiration() { 085 return expireDate; 086 } 087 088 @Override 089 public int getExpiresIn() { 090 if (expireDate != null) { 091 return (int)TimeUnit.MILLISECONDS.toSeconds(expireDate.getTime() - (new Date()).getTime()); 092 } 093 return 0; 094 } 095 096 @Override 097 public String getValue() { 098 return tokenString; 099 } 100 101 102 /** 103 * @return the token 104 */ 105 public JsonObject getIntrospectionResponse() { 106 return introspectionResponse; 107 } 108 109 110 /** 111 * @param token the token to set 112 */ 113 public void setIntrospectionResponse(JsonObject token) { 114 this.introspectionResponse = token; 115 } 116 117}