gluu
静的公開メンバ関数 | 静的非公開変数類 | 全メンバ一覧
org.xdi.oxauth.model.util.JwtUtil クラス
org.xdi.oxauth.model.util.JwtUtil 連携図
Collaboration graph

静的公開メンバ関数

static void printAlgorithmsAndProviders ()
 
static byte [] getMessageDigestSHA256 (String data) throws NoSuchProviderException, NoSuchAlgorithmException, UnsupportedEncodingException
 
static byte [] getMessageDigestSHA384 (String data) throws NoSuchProviderException, NoSuchAlgorithmException, UnsupportedEncodingException
 
static byte [] getMessageDigestSHA512 (String data) throws NoSuchProviderException, NoSuchAlgorithmException, UnsupportedEncodingException
 
static PublicKey getPublicKey (String jwksUri, String jwks, SignatureAlgorithm signatureAlgorithm, String keyId)
 
static JSONObject getJsonKey (String jwksUri, String jwks, String keyId)
 
static JSONObject getJSONWebKeys (String jwksUri)
 

静的非公開変数類

static final Logger log = Logger.getLogger(JwtUtil.class)
 

詳解

著者
Javier Rojas Blum
Yuriy Movchan
バージョン
August 8, 2016

関数詳解

◆ getJsonKey()

static JSONObject org.xdi.oxauth.model.util.JwtUtil.getJsonKey ( String  jwksUri,
String  jwks,
String  keyId 
)
inlinestatic
161  {
162  log.debug("Retrieving JWK Key...");
163 
164  JSONObject jsonKey = null;
165  try {
166  if (StringHelper.isEmpty(jwks)) {
167  ClientRequest clientRequest = new ClientRequest(jwksUri);
168  clientRequest.setHttpMethod(HttpMethod.GET);
169  ClientResponse<String> clientResponse = clientRequest.get(String.class);
170 
171  int status = clientResponse.getStatus();
172  log.debug(String.format("Status: %n%d", status));
173 
174  if (status == 200) {
175  jwks = clientResponse.getEntity(String.class);
176  log.debug(String.format("JWK: %s", jwks));
177  }
178  }
179  if (StringHelper.isNotEmpty(jwks)) {
180  JSONObject jsonObject = new JSONObject(jwks);
181  JSONArray keys = jsonObject.getJSONArray(JSON_WEB_KEY_SET);
182  if (keys.length() > 0) {
183  if (StringHelper.isEmpty(keyId)) {
184  jsonKey = keys.getJSONObject(0);
185  } else {
186  for (int i = 0; i < keys.length(); i++) {
187  JSONObject kv = keys.getJSONObject(i);
188  if (kv.getString(KEY_ID).equals(keyId)) {
189  jsonKey = kv;
190  break;
191  }
192  }
193  }
194  }
195  }
196  } catch (Exception ex) {
197  log.error(ex.getMessage(), ex);
198  }
199 
200  return jsonKey;
201  }
static final Logger log
Definition: JwtUtil.java:40

◆ getJSONWebKeys()

static JSONObject org.xdi.oxauth.model.util.JwtUtil.getJSONWebKeys ( String  jwksUri)
inlinestatic
203  {
204  log.debug("Retrieving jwks...");
205 
206  JSONObject jwks = null;
207  try {
208  if (!StringHelper.isEmpty(jwksUri)) {
209  ClientRequest clientRequest = new ClientRequest(jwksUri);
210  clientRequest.setHttpMethod(HttpMethod.GET);
211  ClientResponse<String> clientResponse = clientRequest.get(String.class);
212 
213  int status = clientResponse.getStatus();
214  log.debug(String.format("Status: %n%d", status));
215 
216  if (status == 200) {
217  jwks = new JSONObject(clientResponse.getEntity(String.class));
218  log.debug(String.format("JWK: %s", jwks));
219  }
220  }
221  } catch (Exception ex) {
222  log.error(ex.getMessage(), ex);
223  }
224 
225  return jwks;
226  }
static final Logger log
Definition: JwtUtil.java:40

◆ getMessageDigestSHA256()

static byte [] org.xdi.oxauth.model.util.JwtUtil.getMessageDigestSHA256 ( String  data) throws NoSuchProviderException, NoSuchAlgorithmException, UnsupportedEncodingException
inlinestatic
70  {
71  MessageDigest mda = MessageDigest.getInstance("SHA-256", "BC");
72  return mda.digest(data.getBytes(Util.UTF8_STRING_ENCODING));
73  }

◆ getMessageDigestSHA384()

static byte [] org.xdi.oxauth.model.util.JwtUtil.getMessageDigestSHA384 ( String  data) throws NoSuchProviderException, NoSuchAlgorithmException, UnsupportedEncodingException
inlinestatic
76  {
77  MessageDigest mda = MessageDigest.getInstance("SHA-384", "BC");
78  return mda.digest(data.getBytes(Util.UTF8_STRING_ENCODING));
79  }

◆ getMessageDigestSHA512()

static byte [] org.xdi.oxauth.model.util.JwtUtil.getMessageDigestSHA512 ( String  data) throws NoSuchProviderException, NoSuchAlgorithmException, UnsupportedEncodingException
inlinestatic
82  {
83  MessageDigest mda = MessageDigest.getInstance("SHA-512", "BC");
84  return mda.digest(data.getBytes(Util.UTF8_STRING_ENCODING));
85  }

◆ getPublicKey()

static PublicKey org.xdi.oxauth.model.util.JwtUtil.getPublicKey ( String  jwksUri,
String  jwks,
SignatureAlgorithm  signatureAlgorithm,
String  keyId 
)
inlinestatic
88  {
89  log.debug("Retrieving JWK...");
90 
91  JSONObject jsonKeyValue = getJsonKey(jwksUri, jwks, keyId);
92 
93  if (jsonKeyValue == null) {
94  return null;
95  }
96 
97  org.xdi.oxauth.model.crypto.PublicKey publicKey = null;
98 
99  try {
100  String resultKeyId = jsonKeyValue.getString(KEY_ID);
101  if (signatureAlgorithm == null) {
102  signatureAlgorithm = SignatureAlgorithm.fromString(jsonKeyValue.getString(ALGORITHM));
103  if (signatureAlgorithm == null) {
104  log.error(String.format("Failed to determine key '%s' signature algorithm", resultKeyId));
105  return null;
106  }
107  }
108 
109  JSONObject jsonPublicKey = jsonKeyValue;
110  if (jsonKeyValue.has(PUBLIC_KEY)) {
111  // Use internal jwks.json format
112  jsonPublicKey = jsonKeyValue.getJSONObject(PUBLIC_KEY);
113  }
114 
115  if (signatureAlgorithm == SignatureAlgorithm.RS256 || signatureAlgorithm == SignatureAlgorithm.RS384 || signatureAlgorithm == SignatureAlgorithm.RS512) {
116  //String alg = jsonKeyValue.getString(ALGORITHM);
117  //String use = jsonKeyValue.getString(KEY_USE);
118  String exp = jsonPublicKey.getString(EXPONENT);
119  String mod = jsonPublicKey.getString(MODULUS);
120 
121  BigInteger publicExponent = new BigInteger(1, Base64Util.base64urldecode(exp));
122  BigInteger modulus = new BigInteger(1, Base64Util.base64urldecode(mod));
123 
124  publicKey = new RSAPublicKey(modulus, publicExponent);
125  } else if (signatureAlgorithm == SignatureAlgorithm.ES256 || signatureAlgorithm == SignatureAlgorithm.ES384 || signatureAlgorithm == SignatureAlgorithm.ES512) {
126  //String alg = jsonKeyValue.getString(ALGORITHM);
127  //String use = jsonKeyValue.getString(KEY_USE);
128  //String crv = jsonKeyValue.getString(CURVE);
129  String xx = jsonPublicKey.getString(X);
130  String yy = jsonPublicKey.getString(Y);
131 
132  BigInteger x = new BigInteger(1, Base64Util.base64urldecode(xx));
133  BigInteger y = new BigInteger(1, Base64Util.base64urldecode(yy));
134 
135  publicKey = new ECDSAPublicKey(signatureAlgorithm, x, y);
136  }
137 
138  if (publicKey != null && jsonKeyValue.has(CERTIFICATE_CHAIN)) {
139  final String BEGIN = "-----BEGIN CERTIFICATE-----";
140  final String END = "-----END CERTIFICATE-----";
141 
142  JSONArray certChain = jsonKeyValue.getJSONArray(CERTIFICATE_CHAIN);
143  String certificateString = BEGIN + "\n" + certChain.getString(0) + "\n" + END;
144  StringReader sr = new StringReader(certificateString);
145  PEMParser pemReader = new PEMParser(sr);
146  X509Certificate cert = (X509CertificateObject) pemReader.readObject();
147  Certificate certificate = new Certificate(signatureAlgorithm, cert);
148  publicKey.setCertificate(certificate);
149  }
150  if (publicKey != null) {
151  publicKey.setKeyId(resultKeyId);
152  publicKey.setSignatureAlgorithm(signatureAlgorithm);
153  }
154  } catch (Exception ex) {
155  log.error(ex.getMessage(), ex);
156  }
157 
158  return publicKey;
159  }
static final Logger log
Definition: JwtUtil.java:40
static JSONObject getJsonKey(String jwksUri, String jwks, String keyId)
Definition: JwtUtil.java:161
Definition: AbstractCryptoProvider.java:6
Definition: PublicKey.java:18

◆ printAlgorithmsAndProviders()

static void org.xdi.oxauth.model.util.JwtUtil.printAlgorithmsAndProviders ( )
inlinestatic
42  {
43  Set<String> algorithms = Security.getAlgorithms("Signature");
44  for (String algorithm : algorithms) {
45  log.trace("Algorithm (Signature): " + algorithm);
46  }
47  algorithms = Security.getAlgorithms("MessageDigest");
48  for (String algorithm : algorithms) {
49  log.trace("Algorithm (MessageDigest): " + algorithm);
50  }
51  algorithms = Security.getAlgorithms("Cipher");
52  for (String algorithm : algorithms) {
53  log.trace("Algorithm (Cipher): " + algorithm);
54  }
55  algorithms = Security.getAlgorithms("Mac");
56  for (String algorithm : algorithms) {
57  log.trace("Algorithm (Mac): " + algorithm);
58  }
59  algorithms = Security.getAlgorithms("KeyStore");
60  for (String algorithm : algorithms) {
61  log.trace("Algorithm (KeyStore): " + algorithm);
62  }
63  Provider[] providers = Security.getProviders();
64  for (Provider provider : providers) {
65  log.trace("Provider: " + provider.getName());
66  }
67  }
static final Logger log
Definition: JwtUtil.java:40

メンバ詳解

◆ log

final Logger org.xdi.oxauth.model.util.JwtUtil.log = Logger.getLogger(JwtUtil.class)
staticprivate

このクラス詳解は次のファイルから抽出されました: