com.nimbusds.oauth2.sdk.token.Tokens Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of oauth2-oidc-sdk Show documentation
Show all versions of oauth2-oidc-sdk Show documentation
OAuth 2.0 SDK with OpenID Connection extensions for developing client
and server applications.
/*
* oauth2-oidc-sdk
*
* Copyright 2012-2016, Connect2id Ltd and contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.nimbusds.oauth2.sdk.token;
import java.util.Set;
import net.jcip.annotations.Immutable;
import net.minidev.json.JSONObject;
import com.nimbusds.oauth2.sdk.ParseException;
/**
* Access and optional refresh token.
*/
@Immutable
public class Tokens {
/**
* Access token.
*/
private final AccessToken accessToken;
/**
* Refresh token, {@code null} if not specified.
*/
private final RefreshToken refreshToken;
/**
* Creates a new tokens instance.
*
* @param accessToken The access token. Must not be {@code null}.
* @param refreshToken The refresh token. If none {@code null}.
*/
public Tokens(final AccessToken accessToken, final RefreshToken refreshToken) {
if (accessToken == null)
throw new IllegalArgumentException("The access token must not be null");
this.accessToken = accessToken;
this.refreshToken = refreshToken;
}
/**
* Returns the access token.
*
* @return The access token.
*/
public AccessToken getAccessToken() {
return accessToken;
}
/**
* Returns the access token as type bearer.
*
* @return The bearer access token, {@code null} if the type is
* different.
*/
public BearerAccessToken getBearerAccessToken() {
if (accessToken instanceof BearerAccessToken) {
return (BearerAccessToken) accessToken;
}
return null;
}
/**
* Returns the optional refresh token.
*
* @return The refresh token, {@code null} if none.
*/
public RefreshToken getRefreshToken() {
return refreshToken;
}
/**
* Returns the token parameter names for the included tokens.
*
* @return The token parameter names.
*/
public Set getParameterNames() {
// Get the std param names for the access + refresh token
Set paramNames = accessToken.getParameterNames();
if (refreshToken != null)
paramNames.addAll(refreshToken.getParameterNames());
return paramNames;
}
/**
* Returns the JSON object representation of this token pair.
*
* Example JSON object:
*
*
* {
* "access_token" : "dZdt8BlltORMTz5U",
* "refresh_token" : "E87zjAoeNXaSoF1U"
* }
*
*
* @return The JSON object representation.
*/
public JSONObject toJSONObject() {
JSONObject o = accessToken.toJSONObject();
if (refreshToken != null)
o.putAll(refreshToken.toJSONObject());
return o;
}
@Override
public String toString() {
return toJSONObject().toJSONString();
}
/**
* Parses an access and optional refresh token from the specified JSON
* object.
*
* @param jsonObject The JSON object to parse. Must not be {@code null}.
*
* @return The tokens.
*
* @throws ParseException If the JSON object couldn't be parsed to a
* tokens instance.
*/
public static Tokens parse(final JSONObject jsonObject)
throws ParseException {
return new Tokens(AccessToken.parse(jsonObject), RefreshToken.parse(jsonObject));
}
}