Skip to content

Commit 996f452

Browse files
committed
Big round of cleaning + javadocs
1 parent d397873 commit 996f452

57 files changed

Lines changed: 439 additions & 149 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/main/java/org/oidc/msg/AbstractMessage.java

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,14 @@ public AbstractMessage(Map<String, Object> claims) {
8686
}
8787

8888
/**
89-
* Constructs message from urlEncoded String representation of a message.
89+
* Constructs a message from the URL string.
9090
*
91-
* @param input
92-
* the urlEncoded String representation of a message
91+
* @param input The urlEncoded String representation of a message.
92+
* @throws MalformedURLException Thrown if the message cannot be parsed from the input.
93+
* @throws InvalidClaimException Thrown if the message content is invalid.
9394
*/
9495
public void fromUrlEncoded(String input)
95-
throws MalformedURLException, IOException, InvalidClaimException {
96+
throws MalformedURLException, DeserializationException {
9697
if (Strings.isNullOrEmpty(input)) {
9798
return;
9899
}
@@ -101,8 +102,15 @@ public void fromUrlEncoded(String input)
101102
while (paramTokenizer.hasMoreTokens()) {
102103
String pair = paramTokenizer.nextToken();
103104
StringTokenizer pairTokenizer = new StringTokenizer(pair, "=");
104-
String key = URLDecoder.decode(pairTokenizer.nextToken(), "UTF-8");
105-
String value = URLDecoder.decode(pairTokenizer.nextToken(), "UTF-8");
105+
106+
String key;
107+
String value;
108+
try {
109+
key = URLDecoder.decode(pairTokenizer.nextToken(), "UTF-8");
110+
value = URLDecoder.decode(pairTokenizer.nextToken(), "UTF-8");
111+
} catch (UnsupportedEncodingException e) {
112+
throw new MalformedURLException("The parameters cannot be decoded using UTF-8");
113+
}
106114
jsonBuilder.append("\"" + key + "\" : ");
107115
jsonBuilder.append(
108116
value.startsWith("{") || value.startsWith("[") ? "\"" + value.replace("\"", "\\\"") + "\""
@@ -119,11 +127,10 @@ public void fromUrlEncoded(String input)
119127
* urlEncoded string.
120128
*
121129
* @return an urlEncoded string
122-
* @throws InvalidClaimException
123-
* if the message is invalid
130+
* @throws SerializationException Thrown if message cannot be serialized.
124131
*/
125132
public String toUrlEncoded()
126-
throws SerializationException, JsonProcessingException, InvalidClaimException {
133+
throws SerializationException {
127134
if (claims.size() == 0) {
128135
return "";
129136
}
@@ -154,18 +161,18 @@ public String toUrlEncoded()
154161
}
155162

156163
/**
157-
* Constructs message from JSON string values.
164+
* Constructs a message from the JSON string.
158165
*
159-
* @param input
160-
* The JSON String representation of a message
166+
* @param input The JSON String representation of a message
167+
* @throws InvalidClaimException Thrown if the message content is invalid.
161168
*/
162-
public void fromJson(String input) throws InvalidClaimException {
169+
public void fromJson(String input) throws DeserializationException {
163170
Map<String, Object> newClaims;
164171
try {
165172
newClaims = mapper.readValue(input, new TypeReference<Map<String, Object>>() {
166173
});
167174
} catch (IOException e) {
168-
throw new InvalidClaimException(String.format("Unable to parse message from '%s'", input));
175+
throw new DeserializationException(String.format("Unable to parse message from '%s'", input));
169176
}
170177
this.claims = newClaims;
171178
verified = false;
@@ -176,15 +183,17 @@ public void fromJson(String input) throws InvalidClaimException {
176183
* json string.
177184
*
178185
* @return a JSON String representation in the form of a hashMap mapping string -> string
179-
* @throws InvalidClaimException
180-
* thrown if message parameters do not match the message requirements.
186+
* @throws SerializationException Thrown if message cannot be serialized.
181187
*/
182-
public String toJson() throws JsonProcessingException, InvalidClaimException {
188+
public String toJson() throws SerializationException {
183189
SimpleModule module = new SimpleModule();
184190
module.addSerializer(AbstractMessage.class, new MessageSerializer());
185191
mapper.registerModule(module);
186-
String jsonMsg = mapper.writeValueAsString(this);
187-
return jsonMsg;
192+
try {
193+
return mapper.writeValueAsString(this);
194+
} catch (JsonProcessingException e) {
195+
throw new SerializationException("Could not serialize to JSON", e);
196+
}
188197
}
189198

190199
/**
@@ -200,7 +209,7 @@ public String toJson() throws JsonProcessingException, InvalidClaimException {
200209
* @throws IOException
201210
* thrown if message parameters do not match the message requirements.
202211
*/
203-
public void fromJwt(String jwt, KeyJar keyJar, String keyOwner) throws IOException {
212+
public void fromJwt(String jwt, KeyJar keyJar, String keyOwner) throws DeserializationException {
204213
fromJwt(jwt, keyJar, keyOwner, null, true, true);
205214
}
206215

@@ -220,13 +229,13 @@ public void fromJwt(String jwt, KeyJar keyJar, String keyOwner) throws IOExcepti
220229
* If jwt is missing kid, try any of the owners keys to verify jwt.
221230
* @param trustJKU
222231
* Whether extending keyjar by JKU is allowed or not.
223-
* @throws JWTDecodeException
224-
* thrown if message parameters do not match the message requirements.
232+
* @throws DeserializationException Thrown if the message content is invalid.
233+
* @throws JWTDecodeException Thrown if the JWT cannot be decoded.
225234
*/
226235
@SuppressWarnings("unchecked")
227236
public void fromJwt(String jwt, KeyJar keyJar, String keyOwner,
228237
Map<String, List<String>> noKidIssuers, boolean allowMissingKid, boolean trustJKU)
229-
throws IOException {
238+
throws DeserializationException, JWTDecodeException {
230239
String[] parts = MessageUtil.splitToken(jwt);
231240
String headerJson;
232241
String payloadJson;
@@ -237,8 +246,12 @@ public void fromJwt(String jwt, KeyJar keyJar, String keyOwner,
237246
throw new JWTDecodeException("The UTF-8 Charset isn't initialized.", e);
238247
}
239248

240-
this.header = mapper.readValue(headerJson, Map.class);
241-
this.claims = mapper.readValue(payloadJson, Map.class);
249+
try {
250+
this.header = mapper.readValue(headerJson, Map.class);
251+
this.claims = mapper.readValue(payloadJson, Map.class);
252+
} catch (IOException e) {
253+
throw new DeserializationException("Could not read the JWT contents", e);
254+
}
242255
verified = false;
243256

244257
if (keyJar == null) {
@@ -259,9 +272,7 @@ public void fromJwt(String jwt, KeyJar keyJar, String keyOwner,
259272
List<java.security.Key> keys;
260273
try {
261274
keys = keyJar.getJWTVerifyKeys(jwt, keyOwner, noKidIssuers, allowMissingKid, trustJKU);
262-
} catch (JWKException | ValueError e) {
263-
// TODO: Replace JWTDecodeException with better exception. Generic note to this class and this
264-
// method.
275+
} catch (JWKException | ValueError | IOException e) {
265276
throw new JWTDecodeException(
266277
String.format("Not able to locate keys to verify JWT, '%s'", e.getMessage()));
267278
}
@@ -322,7 +333,7 @@ public void fromJwt(String jwt, KeyJar keyJar, String keyOwner,
322333
* signing algorithm
323334
* @return message as jwt string.
324335
*/
325-
public String toJwt(Key key, String alg) {
336+
public String toJwt(Key key, String alg) throws SerializationException {
326337

327338
header = new HashMap<String, Object>();
328339
header.put("alg", alg);
@@ -360,13 +371,11 @@ public String toJwt(Key key, String alg) {
360371
// TODO: HMAC algorithms, are getting client secret also from key jar?
361372
}
362373
} catch (IllegalArgumentException | ValueError e) {
363-
// TODO: This is not Decoding exception, replace it.
364-
throw new JWTDecodeException(String
374+
throw new SerializationException(String
365375
.format("Not able to initialize algorithm '%s' to sign JWT, '%s'", alg, e.getMessage()));
366376
}
367377
if (algorithm == null) {
368-
// TODO: This is not Decoding exception, replace it.
369-
throw new JWTDecodeException(
378+
throw new SerializationException(
370379
String.format("Not able to initialize algorithm '%s' to sign JWT", alg));
371380
}
372381
JWTCreator.Builder newBuilder = JWT.create().withHeader(this.header);

src/main/java/org/oidc/msg/DataLocation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.oidc.msg;
1818

1919
/**
20-
* This enum specifies whether the data will be placed in a fragment or in a query part
20+
* This enum specifies whether the data will be placed in a fragment or in a query part.
2121
*/
2222
public enum DataLocation {
2323
FRAGMENT, QUERY_STRING, FORM_POST

src/main/java/org/oidc/msg/DeserializationException.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,24 @@
1717
package org.oidc.msg;
1818

1919
/**
20-
* An exception that is thrown when there is an issue with deserialization of the Message type
20+
* An exception that is thrown when there is an issue with deserialization of the Message type.
2121
*/
22+
@SuppressWarnings("serial")
2223
public class DeserializationException extends Exception {
2324

25+
/**
26+
* Constructor.
27+
* @param message The message describing the exception.
28+
*/
2429
public DeserializationException(String message) {
2530
this(message, null);
2631
}
2732

33+
/**
34+
* Constructor.
35+
* @param message The message describing the exception.
36+
* @param cause The cause for this exception.
37+
*/
2838
public DeserializationException(String message, Throwable cause) {
2939
super(message, cause);
3040
}

src/main/java/org/oidc/msg/Error.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
/** Error class for message verification failures. */
2323
public class Error {
24+
25+
/** The message failure details. */
2426
private List<ErrorDetails> details;
2527

2628
/**

src/main/java/org/oidc/msg/ErrorDetails.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
*/
2222
public class ErrorDetails {
2323

24-
/** The parameter name whose verification failed. */
24+
/** T * @param message The message describing the exception.
25+
* @param cause The cause for this exception.
26+
*/
2527
private String parameterName;
2628

2729
/** The verification error type. */
@@ -33,18 +35,42 @@ public class ErrorDetails {
3335
/** The optional exception related to the verification failure. */
3436
private Throwable errorCause;
3537

38+
/**
39+
* Constructor.
40+
* @param parameter The parameter name whose verification failed.
41+
* @param type The verification error type.
42+
*/
3643
public ErrorDetails(String parameter, ErrorType type) {
3744
this(parameter, type, null, null);
3845
}
39-
46+
47+
/**
48+
* Constructor.
49+
* @param parameter The parameter name whose verification failed.
50+
* @param type The verification error type.
51+
* @param message The optional message describing why verification failed.
52+
*/
4053
public ErrorDetails(String parameter, ErrorType type, String message) {
4154
this(parameter, type, message, null);
4255
}
4356

57+
/**
58+
* Constructor.
59+
* @param parameter The parameter name whose verification failed.
60+
* @param type The verification error type.
61+
* @param cause The optional exception related to the verification failure.
62+
*/
4463
public ErrorDetails(String parameter, ErrorType type, Throwable cause) {
4564
this(parameter, type, null, cause);
4665
}
4766

67+
/**
68+
* Constructor.
69+
* @param parameter The parameter name whose verification failed.
70+
* @param type The verification error type.
71+
* @param message The optional message describing why verification failed.
72+
* @param cause The optional exception related to the verification failure.
73+
*/
4874
public ErrorDetails(String parameter, ErrorType type, String message, Throwable cause) {
4975
parameterName = parameter;
5076
errorType = type;
@@ -68,6 +94,7 @@ public Throwable getErrorCause() {
6894
return errorCause;
6995
}
7096

97+
@Override
7198
public String toString() {
7299
StringBuilder builder = new StringBuilder("parameterName=" + parameterName);
73100
builder.append(", errorType=" + errorType);

src/main/java/org/oidc/msg/InvalidClaimException.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,24 @@
1717
package org.oidc.msg;
1818

1919
/**
20-
* An exception that is thrown when there is an invalid claim in a Message object type
20+
* An exception that is thrown when there is an invalid claim in a Message object type.
2121
*/
22+
@SuppressWarnings("serial")
2223
public class InvalidClaimException extends Exception {
24+
25+
/**
26+
* Constructor.
27+
* @param message The message describing the exception.
28+
*/
2329
public InvalidClaimException(String message) {
2430
this(message, null);
2531
}
2632

33+
/**
34+
* Constructor.
35+
* @param message The message describing the exception.
36+
* @param cause The cause for this exception.
37+
*/
2738
public InvalidClaimException(String message, Throwable cause) {
2839
super(message, cause);
2940
}

0 commit comments

Comments
 (0)