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.client;
019
020
021import java.net.URI;
022import java.util.Date;
023
024import com.nimbusds.oauth2.sdk.ParseException;
025import com.nimbusds.oauth2.sdk.auth.Secret;
026import com.nimbusds.oauth2.sdk.id.ClientID;
027import com.nimbusds.oauth2.sdk.token.BearerAccessToken;
028import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
029import net.minidev.json.JSONObject;
030
031
032/**
033 * Client credentials parser.
034 */
035public class ClientCredentialsParser {
036
037
038        /**
039         * Parses a client identifier from the specified JSON object.
040         *
041         * @param jsonObject The JSON object. Must not be {@code null}.
042         *
043         * @return The client identifier.
044         *
045         * @throws ParseException If parsing failed.
046         */
047        public static ClientID parseID(final JSONObject jsonObject)
048                throws ParseException {
049
050                return new ClientID(JSONObjectUtils.getString(jsonObject, "client_id"));
051        }
052
053
054        /**
055         * Parses a client identifier issue date from the specified JSON
056         * object.
057         *
058         * @param jsonObject The JSON object. Must not be {@code null}.
059         *
060         * @return The client identifier issue date, {@code null} if not
061         *         specified.
062         *
063         * @throws ParseException If parsing failed.
064         */
065        public static Date parseIDIssueDate(final JSONObject jsonObject)
066                throws ParseException {
067
068                if (jsonObject.containsKey("client_id_issued_at")) {
069
070                        return new Date(JSONObjectUtils.getLong(jsonObject, "client_id_issued_at") * 1000);
071                } else {
072                        return null;
073                }
074        }
075
076
077        /**
078         * Parses a client secret from the specified JSON object.
079         *
080         * @param jsonObject The JSON object. Must not be {@code null}.
081         *
082         * @return The client secret, {@code null} if not specified.
083         *
084         * @throws ParseException If parsing failed.
085         */
086        public static Secret parseSecret(final JSONObject jsonObject)
087                throws ParseException {
088
089                if (jsonObject.containsKey("client_secret")) {
090
091                        String value = JSONObjectUtils.getString(jsonObject, "client_secret");
092
093                        Date exp = null;
094
095                        if (jsonObject.containsKey("client_secret_expires_at")) {
096
097                                final long t = JSONObjectUtils.getLong(jsonObject, "client_secret_expires_at");
098
099                                if (t > 0) {
100                                        exp = new Date(t * 1000);
101                                }
102                        }
103
104                        return new Secret(value, exp);
105                } else {
106                        return null;
107                }
108        }
109
110
111        /**
112         * Parses a client registration URI from the specified JSON object.
113         *
114         * @param jsonObject The JSON object. Must not be {@code null}.
115         *
116         * @return The client registration URI, {@code null} if not specified.
117         *
118         * @throws ParseException If parsing failed.
119         */
120        public static URI parseRegistrationURI(final JSONObject jsonObject)
121                throws ParseException {
122
123                return JSONObjectUtils.getURI(jsonObject, "registration_client_uri", null);
124        }
125
126
127        /**
128         * Parses a client registration access token from the specified JSON
129         * object.
130         *
131         * @param jsonObject The JSON object. Must not be {@code null}.
132         *
133         * @return The client registration access token, {@code null} if not
134         *         specified.
135         *
136         * @throws ParseException If parsing failed.
137         */
138        public static BearerAccessToken parseRegistrationAccessToken(final JSONObject jsonObject)
139                throws ParseException {
140
141                if (jsonObject.containsKey("registration_access_token")) {
142
143                        return new BearerAccessToken(
144                                JSONObjectUtils.getString(jsonObject, "registration_access_token"));
145                } else {
146                        return null;
147                }
148        }
149}