001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2021, 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 net.jcip.annotations.Immutable;
022import net.minidev.json.JSONObject;
023
024import com.nimbusds.oauth2.sdk.ParseException;
025import com.nimbusds.oauth2.sdk.Scope;
026
027
028/**
029 * Access token of type not applicable (N/A), intended for use in OAuth 2.0
030 * token exchange scenarios.
031 *
032 * <p>Related specifications:
033 *
034 * <ul>
035 *     <li>OAuth 2.0 Token Exchange (RFC 8693), section 2.2.1.
036 * </ul>
037 */
038@Immutable
039public class NAAccessToken extends AccessToken {
040        
041        
042        private static final long serialVersionUID = 268047904352224888L;
043        
044        
045        /**
046         * Creates a new N/A access token with the specified value.
047         *
048         * @param value           The access token value. Must not be
049         *                        {@code null} or empty string.
050         * @param lifetime        The lifetime in seconds, 0 if not specified.
051         * @param scope           The scope, {@code null} if not specified.
052         * @param issuedTokenType The token type URI, {@code null} if not
053         *                        specified.
054         */
055        public NAAccessToken(final String value, final long lifetime, final Scope scope, final TokenTypeURI issuedTokenType) {
056                
057                super(AccessTokenType.N_A, value, lifetime, scope, issuedTokenType);
058        }
059        
060        
061        @Override
062        public String toAuthorizationHeader() {
063                throw new UnsupportedOperationException();
064        }
065        
066        
067        /**
068         * Parses a N/A access token from a JSON object access token response.
069         *
070         * @param jsonObject The JSON object to parse. Must not be
071         *                   {@code null}.
072         *
073         * @return The N/A access token.
074         *
075         * @throws ParseException If the JSON object couldn't be parsed to a
076         *                        N/A access token.
077         */
078        public static NAAccessToken parse(final JSONObject jsonObject)
079                throws ParseException {
080                
081                AccessTokenUtils.parseAndEnsureType(jsonObject, AccessTokenType.N_A);
082                String accessTokenValue = AccessTokenUtils.parseValue(jsonObject);
083                long lifetime = AccessTokenUtils.parseLifetime(jsonObject);
084                Scope scope = AccessTokenUtils.parseScope(jsonObject);
085                TokenTypeURI issuedTokenType = AccessTokenUtils.parseIssuedTokenType(jsonObject);
086                return new NAAccessToken(accessTokenValue, lifetime, scope, issuedTokenType);
087        }
088}