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.Set; 022 023import net.jcip.annotations.Immutable; 024 025import net.minidev.json.JSONObject; 026 027import com.nimbusds.oauth2.sdk.ParseException; 028import com.nimbusds.openid.connect.sdk.token.OIDCTokens; 029 030 031/** 032 * Access and optional refresh token. 033 */ 034@Immutable 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 * Creates a new tokens instance. 052 * 053 * @param accessToken The access token. Must not be {@code null}. 054 * @param refreshToken The refresh token. If none {@code null}. 055 */ 056 public Tokens(final AccessToken accessToken, final RefreshToken refreshToken) { 057 058 if (accessToken == null) 059 throw new IllegalArgumentException("The access token must not be null"); 060 061 this.accessToken = accessToken; 062 063 this.refreshToken = refreshToken; 064 } 065 066 067 /** 068 * Returns the access token. 069 * 070 * @return The access token. 071 */ 072 public AccessToken getAccessToken() { 073 074 return accessToken; 075 } 076 077 078 /** 079 * Returns the access token as type bearer. 080 * 081 * @return The bearer access token, {@code null} if the type is 082 * different. 083 */ 084 public BearerAccessToken getBearerAccessToken() { 085 086 if (accessToken instanceof BearerAccessToken) { 087 return (BearerAccessToken) accessToken; 088 } 089 090 return null; 091 } 092 093 094 /** 095 * Returns the optional refresh token. 096 * 097 * @return The refresh token, {@code null} if none. 098 */ 099 public RefreshToken getRefreshToken() { 100 101 return refreshToken; 102 } 103 104 105 /** 106 * Returns the token parameter names for the included tokens. 107 * 108 * @return The token parameter names. 109 */ 110 public Set<String> getParameterNames() { 111 112 // Get the std param names for the access + refresh token 113 Set<String> paramNames = accessToken.getParameterNames(); 114 115 if (refreshToken != null) 116 paramNames.addAll(refreshToken.getParameterNames()); 117 118 return paramNames; 119 } 120 121 122 /** 123 * Returns the JSON object representation of this token pair. 124 * 125 * <p>Example JSON object: 126 * 127 * <pre> 128 * { 129 * "access_token" : "dZdt8BlltORMTz5U", 130 * "refresh_token" : "E87zjAoeNXaSoF1U" 131 * } 132 * </pre> 133 * 134 * @return The JSON object representation. 135 */ 136 public JSONObject toJSONObject() { 137 138 JSONObject o = accessToken.toJSONObject(); 139 140 if (refreshToken != null) 141 o.putAll(refreshToken.toJSONObject()); 142 143 return o; 144 } 145 146 147 /** 148 * Casts to OpenID Connect tokens. 149 * 150 * @return The OpenID Connect tokens (including an ID token). 151 */ 152 public OIDCTokens toOIDCTokens() { 153 154 return (OIDCTokens)this; 155 } 156 157 158 @Override 159 public String toString() { 160 161 return toJSONObject().toJSONString(); 162 } 163 164 165 /** 166 * Parses an access and optional refresh token from the specified JSON 167 * object. 168 * 169 * @param jsonObject The JSON object to parse. Must not be {@code null}. 170 * 171 * @return The tokens. 172 * 173 * @throws ParseException If the JSON object couldn't be parsed to a 174 * tokens instance. 175 */ 176 public static Tokens parse(final JSONObject jsonObject) 177 throws ParseException { 178 179 return new Tokens(AccessToken.parse(jsonObject), RefreshToken.parse(jsonObject)); 180 } 181}