001package com.nimbusds.oauth2.sdk.token;
002
003
004import net.minidev.json.JSONObject;
005
006import net.jcip.annotations.Immutable;
007
008
009/**
010 * Access and refresh token pair. This class is immutable.
011 *
012 * @author Vladimir Dzhuvinov
013 */
014@Immutable
015public final class TokenPair {
016
017
018        /**
019         * Access token.
020         */
021        private final AccessToken accessToken;
022
023
024        /**
025         * Refresh token, {@code null} if not specified.
026         */
027        private final RefreshToken refreshToken;
028
029
030        /**
031         * Creates a new access and refresh token pair.
032         *
033         * @param accessToken  The access token. Must not be {@code null}.
034         * @param refreshToken The refresh token. If none {@code null}.
035         */
036        public TokenPair(final AccessToken accessToken, final RefreshToken refreshToken) {
037
038                if (accessToken == null)
039                        throw new IllegalArgumentException("The access token must not be null");
040
041                this.accessToken = accessToken;
042
043                this.refreshToken = refreshToken;
044        }
045        
046
047        /**
048         * Returns the access token.
049         *
050         * @return The access token.
051         */
052        public AccessToken getAccessToken() {
053
054                return accessToken;
055        }
056
057
058        /**
059         * Returns the refresh token.
060         *
061         * @return The refresh token, {@code null} if none.
062         */
063        public RefreshToken getRefreshToken() {
064
065                return refreshToken;
066        }
067
068
069        /**
070         * Returns the JSON object representation of this token pair.
071         *
072         * <p>Example JSON object:
073         *
074         * <pre>
075         * {
076         *   "access_token"  : "dZdt8BlltORMTz5U",
077         *   "refresh_token" : "E87zjAoeNXaSoF1U"
078         * }
079         * </pre>
080         *
081         * @return The JSON object representation.
082         */
083        public JSONObject toJSONObject() {
084
085                JSONObject o = accessToken.toJSONObject();
086
087                if (refreshToken != null)
088                        o.putAll(refreshToken.toJSONObject());
089
090                return o;
091        }
092
093
094        @Override
095        public String toString() {
096
097                return "TokenPair [accessToken=" + accessToken + " refreshToken=" + refreshToken + "]";
098        }
099}