Skip to content

Commit

Permalink
Default "kid". Serializable Authentication object. Optional "use".
Browse files Browse the repository at this point in the history
  • Loading branch information
levahim committed Aug 1, 2020
1 parent 8091a0c commit b1311d2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Serializable;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
Expand Down Expand Up @@ -208,7 +208,12 @@ public String getInfoPageURI() {
* The successful authorization information derived from the token endpoint
* response.
*/
public static final class Authorization {
public static final class Authorization implements Serializable {

/**
* Serial version id.
*/
private static final long serialVersionUID = 1L;

/**
* Issuer ID.
Expand Down Expand Up @@ -721,7 +726,7 @@ public int getHttpConnectTimeout() {
*
* @param httpConnectTimeout Timeout in milliseconds.
*
* @see URLConnection#setConnectTimeout(int)
* @see java.net.URLConnection#setConnectTimeout(int)
*/
public void setHttpConnectTimeout(final int httpConnectTimeout) {

Expand All @@ -745,7 +750,7 @@ public int getHttpReadTimeout() {
*
* @param httpReadTimeout Timeout in milliseconds.
*
* @see URLConnection#setReadTimeout(int)
* @see java.net.URLConnection#setReadTimeout(int)
*/
public void setHttpReadTimeout(final int httpReadTimeout) {

Expand Down Expand Up @@ -1525,7 +1530,7 @@ protected boolean isSignatureValid(final OPDescriptor opDesc,

final Signature sig = Signature.getInstance("SHA256withRSA");
sig.initVerify(this.ops.getOPConfiguration(opDesc.getIssuer())
.getJWKSet().getKey(header.getString("kid")));
.getJWKSet().getKey(header.optString("kid", JWKSet.DEFAULT_KID)));
sig.update(data.getBytes("ASCII"));

return sig.verify(signature);
Expand Down Expand Up @@ -1630,17 +1635,16 @@ protected TokenEndpointResponse callTokenEndpoint(final OPDescriptor opDesc,

// send POST and read response
JSONObject responseBody;
try (final OutputStream out = con.getOutputStream()) {
try (OutputStream out = con.getOutputStream()) {
out.write(postBody.getBytes(UTF8.name()));
out.flush();
try (final Reader in = new InputStreamReader(
con.getInputStream(), UTF8)) {
try (Reader in = new InputStreamReader(con.getInputStream(), UTF8)) {
responseBody = new JSONObject(new JSONTokener(in));
} catch (final IOException e) {
final InputStream errorStream = con.getErrorStream();
if (errorStream == null)
throw e;
try (final Reader in = new InputStreamReader(errorStream, UTF8)) {
try (Reader in = new InputStreamReader(errorStream, UTF8)) {
responseBody = new JSONObject(new JSONTokener(in));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ protected void loadDocument()

// read and parse the document
final JSONObject document;
try (final Reader in = new InputStreamReader(
con.getInputStream(), UTF8)) {
try (Reader in = new InputStreamReader(con.getInputStream(), UTF8)) {
document = new JSONObject(new JSONTokener(in));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
*/
class JWKSet {

/**
* Key id ("kid") for the default key in the set.
*/
static final String DEFAULT_KID = "default";

/**
* ASCII charset.
*/
Expand Down Expand Up @@ -54,8 +59,8 @@ class JWKSet {
for (int n = keysProp.length() - 1; n >= 0; n--) {
final JSONObject keyDef = keysProp.getJSONObject(n);
if (keyDef.optString("kty").equals("RSA")
&& keyDef.optString("use").equals("sig"))
this.keys.put(keyDef.getString("kid"),
&& keyDef.optString("use", "sig").equals("sig"))
this.keys.put(keyDef.optString("kid", DEFAULT_KID),
keyFactory.generatePublic(new RSAPublicKeySpec(
new BigInteger(1, base64.decode(
keyDef.getString("n").getBytes(
Expand Down

0 comments on commit b1311d2

Please sign in to comment.