001// Generated by delombok at Thu Mar 06 11:40:42 UTC 2025 002/* 003 * Copyright (c) 2010-2025 Mark Allen, Norbert Bartels. 004 * 005 * Permission is hereby granted, free of charge, to any person obtaining a copy 006 * of this software and associated documentation files (the "Software"), to deal 007 * in the Software without restriction, including without limitation the rights 008 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 009 * copies of the Software, and to permit persons to whom the Software is 010 * furnished to do so, subject to the following conditions: 011 * 012 * The above copyright notice and this permission notice shall be included in 013 * all copies or substantial portions of the Software. 014 * 015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 018 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 021 * THE SOFTWARE. 022 */ 023package com.restfb; 024 025import static com.restfb.util.UrlUtils.extractParametersFromQueryString; 026import static java.lang.String.format; 027import java.util.*; 028import com.restfb.util.ReflectionUtils; 029 030/** 031 * Represents an access token/expiration date pair. 032 * <p> 033 * Facebook returns these types when performing access token-related operations - see 034 * {@link FacebookClient#convertSessionKeysToAccessTokens(String, String, String...)}, 035 * {@link FacebookClient#obtainAppAccessToken(String, String)}, and 036 * {@link FacebookClient#obtainExtendedAccessToken(String, String, String)} for details. 037 * 038 * @author <a href="http://restfb.com">Mark Allen</a> 039 */ 040public class AccessToken { 041 /** 042 * The access token's value. 043 */ 044 @Facebook("access_token") 045 private String accessToken; 046 @Facebook("expires_in") 047 private Long rawExpires; 048 /** 049 * The date on which the access token expires. 050 */ 051 private Date expires; 052 /** 053 * The token type of this access token provided by Facebook 054 */ 055 @Facebook("token_type") 056 private String tokenType; 057 @Facebook("user_id") 058 private String userId; 059 private FacebookClient client; 060 @Facebook 061 private List<String> permissions; 062 063 public void setClient(FacebookClient client) { 064 this.client = client; 065 } 066 067 public FacebookClient getClient() { 068 return Optional.ofNullable(client).orElse(null); 069 } 070 071 /** 072 * Given a query string of the form {@code access_token=XXX} or {@code access_token=XXX&expires=YYY}, return an 073 * {@code AccessToken} instance. 074 * <p> 075 * The {@code queryString} is required to contain an {@code access_token} parameter with a non-{@code null} value. The 076 * {@code expires} value is optional and should be the number of seconds since the epoch. If the {@code expires} value 077 * cannot be parsed, the returned {@code AccessToken} will have a {@code null} {@code expires} value. 078 * 079 * @param queryString 080 * The Facebook query string out of which to parse an {@code AccessToken} instance. 081 * @return An {@code AccessToken} instance which corresponds to the given {@code queryString}. 082 * @throws IllegalArgumentException 083 * If no {@code access_token} parameter is present in the query string. 084 * @since 1.6.10 085 */ 086 public static AccessToken fromQueryString(String queryString) { 087 // Query string can be of the form 'access_token=XXX' or 088 // 'access_token=XXX&expires=YYY' 089 Map<String, List<String>> urlParameters = extractParametersFromQueryString(queryString); 090 String extendedAccessToken = null; 091 String tokenType = null; 092 if (urlParameters.containsKey("access_token")) { 093 extendedAccessToken = urlParameters.get("access_token").get(0); 094 } 095 if (urlParameters.containsKey("token_type")) { 096 tokenType = urlParameters.get("token_type").get(0); 097 } 098 if (extendedAccessToken == null) { 099 throw new IllegalArgumentException(format("Was expecting a query string of the form \'access_token=XXX\' or \'access_token=XXX&expires=YYY\'. Instead, the query string was \'%s\'", queryString)); 100 } 101 Date expires = null; 102 // If an expires or expires_in value was provided and it's a valid long, great - use it. 103 // Otherwise ignore it. 104 String rawExpires = null; 105 if (urlParameters.containsKey("expires")) { 106 rawExpires = urlParameters.get("expires").get(0); 107 } 108 if (urlParameters.containsKey("expires_in")) { 109 rawExpires = urlParameters.get("expires_in").get(0); 110 } 111 if (rawExpires != null && rawExpires.trim().matches("\\d+")) { 112 expires = new Date(new Date().getTime() + Long.parseLong(rawExpires) * 1000L); 113 } 114 AccessToken accessToken = new AccessToken(); 115 accessToken.accessToken = extendedAccessToken; 116 accessToken.expires = expires; 117 accessToken.tokenType = tokenType; 118 return accessToken; 119 } 120 121 @Override 122 public int hashCode() { 123 return ReflectionUtils.hashCode(this); 124 } 125 126 @Override 127 public boolean equals(Object that) { 128 return ReflectionUtils.equals(this, that); 129 } 130 131 @Override 132 public String toString() { 133 return ReflectionUtils.toString(this); 134 } 135 136 @JsonMapper.JsonMappingCompleted 137 void convertExpires() { 138 if (rawExpires != null) { 139 expires = new Date(new Date().getTime() + 1000L * rawExpires); 140 } 141 } 142 143 /** 144 * The access token's value. 145 * 146 * @return The access token's value. 147 */ 148 @java.lang.SuppressWarnings("all") 149 public String getAccessToken() { 150 return this.accessToken; 151 } 152 153 /** 154 * The date on which the access token expires. 155 * 156 * @return The date on which the access token expires. 157 */ 158 @java.lang.SuppressWarnings("all") 159 public Date getExpires() { 160 return this.expires; 161 } 162 163 /** 164 * The token type of this access token provided by Facebook 165 * 166 * @return the access token type 167 */ 168 @java.lang.SuppressWarnings("all") 169 public String getTokenType() { 170 return this.tokenType; 171 } 172 173 @java.lang.SuppressWarnings("all") 174 public String getUserId() { 175 return this.userId; 176 } 177 178 @java.lang.SuppressWarnings("all") 179 public void setUserId(final String userId) { 180 this.userId = userId; 181 } 182 183 @java.lang.SuppressWarnings("all") 184 public List<String> getPermissions() { 185 return this.permissions; 186 } 187 188 @java.lang.SuppressWarnings("all") 189 public void setPermissions(final List<String> permissions) { 190 this.permissions = permissions; 191 } 192}