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