001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2024, 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.openid.connect.sdk.nativesso;
019
020
021import com.nimbusds.oauth2.sdk.ParseException;
022import com.nimbusds.oauth2.sdk.token.Token;
023import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
024import net.jcip.annotations.Immutable;
025import net.minidev.json.JSONObject;
026
027import java.util.HashSet;
028import java.util.Set;
029
030
031/**
032 * {@link Token} representation of a device secret.
033 *
034 * <p>Related specifications:
035 *
036 * <ul>
037 *     <li>OpenID Connect Native SSO for Mobile Apps 1.0
038 * </ul>
039 */
040@Immutable
041public final class DeviceSecretToken extends Token {
042
043
044        private static final long serialVersionUID = -5834546200975074494L;
045
046
047        /**
048         * Creates a new device secret token.
049         *
050         * @param deviceSecret The device secret. Must not be {@code null}.
051         */
052        public DeviceSecretToken(final DeviceSecret deviceSecret) {
053        
054                super(deviceSecret.getValue());
055        }
056
057
058        /**
059         * Returns the device secret.
060         *
061         * @return The device secret.
062         */
063        public DeviceSecret getDeviceSecret() {
064
065                return new DeviceSecret(getValue());
066        }
067
068
069        @Override
070        public Set<String> getParameterNames() {
071
072                Set<String> paramNames = new HashSet<>(getCustomParameters().keySet());
073                paramNames.add("device_secret");
074                return paramNames;
075        }
076
077
078        @Override
079        public JSONObject toJSONObject() {
080
081                JSONObject o = new JSONObject();
082                o.putAll(getCustomParameters());
083                o.put("device_secret", getValue());
084                return o;
085        }
086
087
088        /**
089         * Parses a device secret token from a JSON object access token
090         * response.
091         *
092         * @param jsonObject The JSON object to parse. Must not be 
093         *                   {@code null}.
094         *
095         * @return The device secret token, {@code null} if not found.
096         *
097         * @throws ParseException If the JSON object couldn't be parsed to a
098         *                        device secret token.
099         */
100        public static DeviceSecretToken parse(final JSONObject jsonObject)
101                throws ParseException {
102
103                String value = JSONObjectUtils.getString(jsonObject, "device_secret", null);
104                
105                if (value == null) return null;
106
107                try {
108                        return new DeviceSecretToken(new DeviceSecret(value));
109                } catch (Exception e) {
110                        throw new ParseException("Illegal device secret", e);
111                }
112        }
113
114
115        @Override
116        public boolean equals(final Object object) {
117        
118                return object instanceof DeviceSecretToken &&
119                       this.toString().equals(object.toString());
120        }
121}