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