001// Generated by delombok at Fri Feb 21 00:22:14 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  private List<String> permissions;
061  @Facebook("permissions")
062  private String permissionsAsString;
063
064  public void setClient(FacebookClient client) {
065    this.client = client;
066  }
067
068  public FacebookClient getClient() {
069    return Optional.ofNullable(client).orElse(null);
070  }
071
072  /**
073   * Given a query string of the form {@code access_token=XXX} or {@code access_token=XXX&expires=YYY}, return an
074   * {@code AccessToken} instance.
075   * <p>
076   * The {@code queryString} is required to contain an {@code access_token} parameter with a non-{@code null} value. The
077   * {@code expires} value is optional and should be the number of seconds since the epoch. If the {@code expires} value
078   * cannot be parsed, the returned {@code AccessToken} will have a {@code null} {@code expires} value.
079   *
080   * @param queryString
081   *          The Facebook query string out of which to parse an {@code AccessToken} instance.
082   * @return An {@code AccessToken} instance which corresponds to the given {@code queryString}.
083   * @throws IllegalArgumentException
084   *           If no {@code access_token} parameter is present in the query string.
085   * @since 1.6.10
086   */
087  public static AccessToken fromQueryString(String queryString) {
088    // Query string can be of the form 'access_token=XXX' or
089    // 'access_token=XXX&expires=YYY'
090    Map<String, List<String>> urlParameters = extractParametersFromQueryString(queryString);
091    String extendedAccessToken = null;
092    String tokenType = null;
093    if (urlParameters.containsKey("access_token")) {
094      extendedAccessToken = urlParameters.get("access_token").get(0);
095    }
096    if (urlParameters.containsKey("token_type")) {
097      tokenType = urlParameters.get("token_type").get(0);
098    }
099    if (extendedAccessToken == null) {
100      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));
101    }
102    Date expires = null;
103    // If an expires or expires_in value was provided and it's a valid long, great - use it.
104    // Otherwise ignore it.
105    String rawExpires = null;
106    if (urlParameters.containsKey("expires")) {
107      rawExpires = urlParameters.get("expires").get(0);
108    }
109    if (urlParameters.containsKey("expires_in")) {
110      rawExpires = urlParameters.get("expires_in").get(0);
111    }
112    if (rawExpires != null && rawExpires.trim().matches("\\d+")) {
113      expires = new Date(new Date().getTime() + Long.parseLong(rawExpires) * 1000L);
114    }
115    AccessToken accessToken = new AccessToken();
116    accessToken.accessToken = extendedAccessToken;
117    accessToken.expires = expires;
118    accessToken.tokenType = tokenType;
119    return accessToken;
120  }
121
122  @Override
123  public int hashCode() {
124    return ReflectionUtils.hashCode(this);
125  }
126
127  @Override
128  public boolean equals(Object that) {
129    return ReflectionUtils.equals(this, that);
130  }
131
132  @Override
133  public String toString() {
134    return ReflectionUtils.toString(this);
135  }
136
137  @JsonMapper.JsonMappingCompleted
138  void convertExpires() {
139    if (rawExpires != null) {
140      expires = new Date(new Date().getTime() + 1000L * rawExpires);
141    }
142  }
143
144  @JsonMapper.JsonMappingCompleted
145  void convertPermissions() {
146    permissions = permissionsAsString != null ? Arrays.asList(permissionsAsString.split(",")) : null;
147  }
148
149  /**
150   * The access token's value.
151   *
152   * @return The access token's value.
153   */
154  @java.lang.SuppressWarnings("all")
155  public String getAccessToken() {
156    return this.accessToken;
157  }
158
159  /**
160   * The date on which the access token expires.
161   *
162   * @return The date on which the access token expires.
163   */
164  @java.lang.SuppressWarnings("all")
165  public Date getExpires() {
166    return this.expires;
167  }
168
169  /**
170   * The token type of this access token provided by Facebook
171   *
172   * @return the access token type
173   */
174  @java.lang.SuppressWarnings("all")
175  public String getTokenType() {
176    return this.tokenType;
177  }
178
179  @java.lang.SuppressWarnings("all")
180  public String getUserId() {
181    return this.userId;
182  }
183
184  @java.lang.SuppressWarnings("all")
185  public void setUserId(final String userId) {
186    this.userId = userId;
187  }
188
189  @java.lang.SuppressWarnings("all")
190  public List<String> getPermissions() {
191    return this.permissions;
192  }
193
194  @java.lang.SuppressWarnings("all")
195  public String getPermissionsAsString() {
196    return this.permissionsAsString;
197  }
198
199  @java.lang.SuppressWarnings("all")
200  public void setPermissionsAsString(final String permissionsAsString) {
201    this.permissionsAsString = permissionsAsString;
202  }
203}