001/* 002 * oauth2-oidc-sdk 003 * 004 * Copyright 2012-2016, Connect2id Ltd and contributors. 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use 007 * this file except in compliance with the License. You may obtain a copy of the 008 * License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software distributed 013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 015 * specific language governing permissions and limitations under the License. 016 */ 017 018package com.nimbusds.oauth2.sdk.token; 019 020 021import java.util.Collections; 022import java.util.HashMap; 023import java.util.Map; 024import java.util.Set; 025 026import net.minidev.json.JSONObject; 027 028import com.nimbusds.oauth2.sdk.ParseException; 029import com.nimbusds.openid.connect.sdk.token.OIDCTokens; 030 031 032/** 033 * Access and optional refresh token. 034 */ 035public class Tokens { 036 037 038 /** 039 * Access token. 040 */ 041 private final AccessToken accessToken; 042 043 044 /** 045 * Refresh token, {@code null} if not specified. 046 */ 047 private final RefreshToken refreshToken; 048 049 050 /** 051 * Optional token metadata, intended for server environments. 052 */ 053 private final Map<String,Object> metadata = new HashMap<>(); 054 055 056 /** 057 * Creates a new tokens instance. 058 * 059 * @param accessToken The access token. Must not be {@code null}. 060 * @param refreshToken The refresh token. If none {@code null}. 061 */ 062 public Tokens(final AccessToken accessToken, final RefreshToken refreshToken) { 063 064 if (accessToken == null) 065 throw new IllegalArgumentException("The access token must not be null"); 066 067 this.accessToken = accessToken; 068 069 this.refreshToken = refreshToken; 070 } 071 072 073 /** 074 * Returns the access token. 075 * 076 * @return The access token. 077 */ 078 public AccessToken getAccessToken() { 079 080 return accessToken; 081 } 082 083 084 /** 085 * Returns the access token as type bearer. 086 * 087 * @return The bearer access token, {@code null} if the type is 088 * different. 089 */ 090 public BearerAccessToken getBearerAccessToken() { 091 092 if (accessToken instanceof BearerAccessToken) { 093 return (BearerAccessToken) accessToken; 094 } 095 096 return null; 097 } 098 099 100 /** 101 * Returns the optional refresh token. 102 * 103 * @return The refresh token, {@code null} if none. 104 */ 105 public RefreshToken getRefreshToken() { 106 107 return refreshToken; 108 } 109 110 111 /** 112 * Returns the optional modifiable token metadata. Intended for server 113 * environments. 114 * 115 * @return The token metadata. 116 */ 117 public Map<String, Object> getMetadata() { 118 return metadata; 119 } 120 121 122 /** 123 * Returns the token parameter names for the included tokens. 124 * 125 * @return The token parameter names. 126 */ 127 public Set<String> getParameterNames() { 128 129 // Get the std param names for the access + refresh token 130 Set<String> paramNames = accessToken.getParameterNames(); 131 132 if (refreshToken != null) 133 paramNames.addAll(refreshToken.getParameterNames()); 134 135 return Collections.unmodifiableSet(paramNames); 136 } 137 138 139 /** 140 * Returns the JSON object representation of this token pair. 141 * 142 * <p>Example JSON object: 143 * 144 * <pre> 145 * { 146 * "access_token" : "dZdt8BlltORMTz5U", 147 * "refresh_token" : "E87zjAoeNXaSoF1U" 148 * } 149 * </pre> 150 * 151 * @return The JSON object representation. 152 */ 153 public JSONObject toJSONObject() { 154 155 JSONObject o = accessToken.toJSONObject(); 156 157 if (refreshToken != null) 158 o.putAll(refreshToken.toJSONObject()); 159 160 return o; 161 } 162 163 164 /** 165 * Casts to OpenID Connect tokens. 166 * 167 * @return The OpenID Connect tokens (including an ID token). 168 */ 169 public OIDCTokens toOIDCTokens() { 170 171 return (OIDCTokens)this; 172 } 173 174 175 @Override 176 public String toString() { 177 178 return toJSONObject().toJSONString(); 179 } 180 181 182 /** 183 * Parses an access and optional refresh token from the specified JSON 184 * object. 185 * 186 * @param jsonObject The JSON object to parse. Must not be {@code null}. 187 * 188 * @return The tokens. 189 * 190 * @throws ParseException If the JSON object couldn't be parsed to a 191 * tokens instance. 192 */ 193 public static Tokens parse(final JSONObject jsonObject) 194 throws ParseException { 195 196 return new Tokens(AccessToken.parse(jsonObject), RefreshToken.parse(jsonObject)); 197 } 198}