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