001// Generated by delombok at Thu Jun 13 16:13:05 UTC 2024
002/*
003 * Copyright (c) 2010-2024 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 com.restfb.util.ReflectionUtils;
026import java.util.Date;
027import java.util.List;
028import java.util.Map;
029import java.util.Optional;
030import static com.restfb.util.UrlUtils.extractParametersFromQueryString;
031import static java.lang.String.format;
032
033/**
034 * Represents an access token/expiration date pair.
035 * <p>
036 * Facebook returns these types when performing access token-related operations - see
037 * {@link FacebookClient#convertSessionKeysToAccessTokens(String, String, String...)},
038 * {@link FacebookClient#obtainAppAccessToken(String, String)}, and
039 * {@link FacebookClient#obtainExtendedAccessToken(String, String, String)} for details.
040 *
041 * @author <a href="http://restfb.com">Mark Allen</a>
042 */
043public class AccessToken {
044    /**
045     * The access token's value.
046     */
047    @Facebook("access_token")
048    private String accessToken;
049    @Facebook("expires_in")
050    private Long rawExpires;
051    /**
052     * The date on which the access token expires.
053     */
054    private Date expires;
055    /**
056     * The token type of this access token provided by Facebook
057     */
058    @Facebook("token_type")
059    private String tokenType;
060    @Facebook("user_id")
061    private String userId;
062    private FacebookClient client;
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.
077     * The {@code expires} value is optional and should be the number of seconds since the epoch. If the {@code expires}
078     * value cannot be parsed, the returned {@code AccessToken} will have a {@code null} {@code expires} value.
079     *
080     * @param queryString 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 If no {@code access_token} parameter is present in the query string.
083     * @since 1.6.10
084     */
085    public static AccessToken fromQueryString(String queryString) {
086        // Query string can be of the form 'access_token=XXX' or
087        // 'access_token=XXX&expires=YYY'
088        Map<String, List<String>> urlParameters = extractParametersFromQueryString(queryString);
089        String extendedAccessToken = null;
090        String tokenType = null;
091        if (urlParameters.containsKey("access_token")) {
092            extendedAccessToken = urlParameters.get("access_token").get(0);
093        }
094        if (urlParameters.containsKey("token_type")) {
095            tokenType = urlParameters.get("token_type").get(0);
096        }
097        if (extendedAccessToken == null) {
098            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));
099        }
100        Date expires = null;
101        // If an expires or expires_in value was provided and it's a valid long, great - use it.
102        // Otherwise ignore it.
103        String rawExpires = null;
104        if (urlParameters.containsKey("expires")) {
105            rawExpires = urlParameters.get("expires").get(0);
106        }
107        if (urlParameters.containsKey("expires_in")) {
108            rawExpires = urlParameters.get("expires_in").get(0);
109        }
110        if (rawExpires != null && rawExpires.trim().matches("\\d+")) {
111            expires = new Date(new Date().getTime() + Long.parseLong(rawExpires) * 1000L);
112        }
113        AccessToken accessToken = new AccessToken();
114        accessToken.accessToken = extendedAccessToken;
115        accessToken.expires = expires;
116        accessToken.tokenType = tokenType;
117        return accessToken;
118    }
119
120    @Override
121    public int hashCode() {
122        return ReflectionUtils.hashCode(this);
123    }
124
125    @Override
126    public boolean equals(Object that) {
127        return ReflectionUtils.equals(this, that);
128    }
129
130    @Override
131    public String toString() {
132        return ReflectionUtils.toString(this);
133    }
134
135    @JsonMapper.JsonMappingCompleted
136    void convertExpires() {
137        if (rawExpires != null) {
138            expires = new Date(new Date().getTime() + 1000L * rawExpires);
139        }
140    }
141
142    /**
143     * The access token's value.
144     *
145     * @return The access token's value.
146     */
147    @java.lang.SuppressWarnings("all")
148    public String getAccessToken() {
149        return this.accessToken;
150    }
151
152    /**
153     * The date on which the access token expires.
154     *
155     * @return The date on which the access token expires.
156     */
157    @java.lang.SuppressWarnings("all")
158    public Date getExpires() {
159        return this.expires;
160    }
161
162    /**
163     * The token type of this access token provided by Facebook
164     *
165     * @return the access token type
166     */
167    @java.lang.SuppressWarnings("all")
168    public String getTokenType() {
169        return this.tokenType;
170    }
171
172    @java.lang.SuppressWarnings("all")
173    public String getUserId() {
174        return this.userId;
175    }
176
177    @java.lang.SuppressWarnings("all")
178    public void setUserId(final String userId) {
179        this.userId = userId;
180    }
181}