-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OICMSG interfaces, abstract class, enums #1
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
package com.auth0.msg; | ||
|
||
import com.auth0.jwt.algorithms.Algorithm; | ||
import org.apache.commons.codec.binary.Hex; | ||
|
||
import java.net.URLDecoder; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add class level javadoc There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
public abstract class AbstractMessage implements Message { | ||
private Map<String, Object> claims; | ||
private String input; | ||
private Error error = null; | ||
private boolean verified = false; | ||
|
||
protected AbstractMessage(Map<String, Object> claims) { | ||
this.claims = claims; | ||
} | ||
|
||
/** | ||
* Returns a hashmap representation of the contents of the urlEncoded string | ||
* which is passed in as a parameter | ||
* | ||
* @param urlEncoded the urlEncoded String representation of a message | ||
* @return a map of the key value pairs encoded in the string parameter | ||
*/ | ||
private static Map<String, Object> claimsFromUrlEncoded(String urlEncoded) throws Exception { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having a private static method in an abstract class doesn't make sense. No other class will see this method at all. If it is an utility method - create a new class MessageUtil and there you add it as a public static method. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
//Logic to extract from the string the values | ||
Map<String, Object> values = new HashMap<String, Object>(); | ||
return values; | ||
} | ||
|
||
/** | ||
* @param input the urlEncoded String representation of a message | ||
* @return a Message representation of the UrlEncoded string | ||
*/ | ||
public Message fromUrlEncoded(String input) { | ||
this.input = input; | ||
//This will have logic to parse urlencoded to claims | ||
return this; | ||
} | ||
|
||
/** | ||
* Takes the claims of this instance of the AbstractMessage class and serializes them | ||
* to an urlEncoded string | ||
* | ||
* @return an urlEncoded string | ||
*/ | ||
public String toUrlEncoded() { | ||
// Serialize the content of this instance (the claims map) into an UrlEncoded string | ||
return ""; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add TODOs - so that we don't we don't miss out anything. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
|
||
/** | ||
* Logic to extract from the string the values | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please update the javadoc to mention json string? |
||
* | ||
* @param input The JSON String representation of a message | ||
* @return a Message representation of the Json | ||
*/ | ||
public Message fromJson(String input) { | ||
this.input = input; | ||
//This will have logic to parse json to claims | ||
return this; | ||
} | ||
|
||
/** | ||
* Takes the claims of this instance of the AbstractMessage class and serializes them | ||
* to a json string | ||
* | ||
* @return a JSON String representation in the form of a hashMap mapping string -> string | ||
*/ | ||
public String toJson() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All the 'to' methods toJson, toJwt and toUrlEncoded, show have |
||
if (this.error != null) { | ||
//This should be custom exception | ||
throw new InvalidClaimsException("Error present cannot serialize message"); | ||
} | ||
return ""; | ||
} | ||
|
||
/** | ||
* @param input the jwt String representation of a message | ||
* @param Key that might contain the necessary key | ||
* @return a ResponseMessage representation of the JWT | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please update ResponseMessage to Message everywhere in this class |
||
*/ | ||
public Message fromJwt(String input, Key key) { | ||
this.input = input; | ||
//This will have logic to parse Jwt to claims | ||
return this; | ||
} | ||
|
||
/** | ||
* @param input the jwt String representation of a message | ||
* @param KeyJar that might contain the necessary key | ||
* @return a Message representation of the JWT | ||
*/ | ||
public Message fromJwt(String input, KeyJar jar) { | ||
this.input = input; | ||
//This will have logic to parse Jwt to claims | ||
return this; | ||
} | ||
|
||
/** | ||
* Serialize the content of this instance (the claims map) into a jwt string | ||
* @param KeyJar the signing keyjar | ||
* @param String the algorithm to use in signing the JWT | ||
* @return a jwt String | ||
* @throws InvalidClaimsException | ||
*/ | ||
public String toJwt(KeyJar keyjar, Algorithm algorithm) throws | ||
InvalidClaimsException { | ||
return null; | ||
} | ||
|
||
/** | ||
* Serialize the content of this instance (the claims map) into a jwt string | ||
* @param Key the signing key | ||
* @param String the algorithm to use in signing the JWT | ||
* @return a jwt String | ||
* @throws InvalidClaimsException | ||
*/ | ||
public String toJwt(Key key, Algorithm algorithm) throws InvalidClaimsException { | ||
return null; | ||
} | ||
|
||
/** | ||
* verify that the required claims are present | ||
* @return whether the verification passed | ||
*/ | ||
public boolean verify() { | ||
//This method will set error if verification fails | ||
return true; | ||
} | ||
|
||
/** | ||
* add the claim to this instance of message | ||
* @param ClaimType the name of the claim | ||
* @param Object the value of the claim to add to this instance of Message | ||
* @return a Message representation of the Json | ||
*/ | ||
public void addClaim(ClaimType name, Object value) { | ||
// verify 'name’ is a valid claim and then check the type is valid before adding | ||
} | ||
|
||
/** | ||
* @param endpoint to base the request url on | ||
* @return a String for the representation of the formatted request | ||
*/ | ||
public String getRequestWithEndpoint(String authorizationEndpoint) { | ||
return null; | ||
} | ||
|
||
/** | ||
* @return Error an object representing the error status of claims verification | ||
*/ | ||
public Error getError() { | ||
return error; | ||
} | ||
|
||
|
||
/** | ||
* @return List of the list of standard optional claims for this messsage type | ||
*/ | ||
protected List<ClaimType> getOptionalClaims(){ | ||
return Collections.emptyList(); | ||
} | ||
|
||
/** | ||
* @return List of the list of standard required claims for this messsage type | ||
*/ | ||
abstract protected List<ClaimType> getRequiredClaims(); | ||
|
||
@Override | ||
public String toString() { | ||
//Override to return user friendly value | ||
return super.toString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.auth0.msg; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add javadoc. Also Justin and you should share the enum Algorithm enum. @jdahmubed - please use just one enum for algorithms. At this time, you an Leo have two different copies. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jdahmubed if you can import and use my AlgorithmEnum we should just go with that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i dont agree w including "Enum" in the enum name. mine leaves it out. |
||
public enum AlgorithmEnum { | ||
RS256, | ||
RS384, | ||
RS512, | ||
HS256, | ||
HS384, | ||
HS512, | ||
ES256; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.auth0.msg; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add Javadocs. As we discussed previously - we shoould look at how we will use this enum to capture the type of the claim. I am talking about Claim type that you have mentioned here https://docs.google.com/document/d/1-N0n7UopFaIhzA5X-j1fhBgAR-kImbKoqgSVTUmixEI/edit?ts=5aac5b78#bookmark=id.u4q83iah8gx9 If we need to validate the type, we should have type information in the enum itself. |
||
public enum ClaimType { | ||
|
||
GRANT_TYPE("grant_type", Arrays.asList("refresh_token")), | ||
ERROR("error", Arrays.asList("invalid_request", "unauthorized_client")); | ||
|
||
private final String name; | ||
private final List<String> allowedValues; | ||
|
||
ClaimType(String name, List<String> allowedValues) { | ||
this.name = name; | ||
this.allowedValues = allowedValues; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.auth0.msg; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add javadoc. |
||
public enum DataLocation { | ||
FRAGMENT, QUERYPART | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please change QUERYPART to QUERY_STRING (more explicit). Also add one more value FORM_POST |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.auth0.msg; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add Javadoc |
||
public class InvalidClaimsException extends RuntimeException { | ||
public InvalidClaimsException(String message) { | ||
this(message, null); | ||
} | ||
|
||
public InvalidClaimsException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.auth0.msg; | ||
|
||
public class Key { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add class level Javadoc |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.auth0.msg; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add class level Javadoc |
||
public class KeyJar { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package com.auth0.msg; | ||
|
||
import com.auth0.jwt.algorithms.Algorithm; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* This interface all the methods related to message processing. | ||
*/ | ||
public interface Message { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add |
||
|
||
/** | ||
* Serialize the content of this instance (the claims map) into a JSON object | ||
* @return a JSON String representation of the message | ||
* @throws SerializationException | ||
*/ | ||
String toJson() throws SerializationException; | ||
|
||
/** | ||
* Serialize the content of the claims map into an UrlEncoded string | ||
* @return a urlEncoded string | ||
* @throws SerializationException | ||
*/ | ||
String toUrlEncoded() throws SerializationException; | ||
|
||
/** | ||
* Serialize the content of this instance (the claims map) into a jwt string | ||
* @param Key the signing key | ||
* @param String the algorithm to use in signing the JWT | ||
* @return a jwt String | ||
* @throws InvalidClaimsException | ||
*/ | ||
String toJwt(Key key, Algorithm algorithm) throws InvalidClaimsException; | ||
|
||
/** | ||
* Serialize the content of this instance (the claims map) into a jwt string | ||
* @param KeyJar the signing keyjar | ||
* @param String the algorithm to use in signing the JWT | ||
* @return a jwt String | ||
* @throws InvalidClaimsException | ||
*/ | ||
String toJwt(KeyJar jar, Algorithm algorithm) throws InvalidClaimsException; | ||
|
||
/** | ||
* Logic to extract from the string the values | ||
* @param input The JSON String representation of a message | ||
* @return a Message representation of the Json | ||
*/ | ||
Message fromJson(String input); | ||
|
||
/** | ||
* @param input the urlEncoded String representation of a message | ||
* @return a Message representation of the UrlEncoded string | ||
*/ | ||
Message fromUrlEncoded(String input); | ||
|
||
/** | ||
* | ||
* @param input the jwt String representation of a message | ||
* @param key that might contain the necessary key | ||
* @return a Message representation of the JWT | ||
*/ | ||
Message fromJwt(String input, Key key); | ||
|
||
/** | ||
* | ||
* @param input the jwt String representation of a message | ||
* @param KeyJar that might contain the necessary key | ||
* @return a Message representation of the JWT | ||
*/ | ||
Message fromJwt(String input, KeyJar jar); | ||
|
||
/** | ||
* verify that the required claims are present | ||
* @return whether the verification passed | ||
*/ | ||
boolean verify(); | ||
|
||
/** | ||
* | ||
* @param name of the claim | ||
* @param value of the claim | ||
*/ | ||
void addClaim(ClaimType name, Object value); | ||
|
||
/** | ||
* | ||
* @return Map of claims | ||
* @throws InvalidClaimsException | ||
*/ | ||
Map<ClaimType, Object> getClaims() throws InvalidClaimsException; | ||
|
||
/** | ||
* | ||
* @param String authorization endpoint | ||
*/ | ||
String getRequestWithEndpoint(String authorizationEndpoint, DataLocation location); | ||
|
||
/** | ||
* @return the error object representing an error in verification | ||
*/ | ||
Error getError(); | ||
|
||
/** | ||
* @return boolean for whether there is an error in verification | ||
*/ | ||
boolean hasError(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.auth0.msg; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add javadoc |
||
public class SerializationException extends RuntimeException { | ||
public SerializationException(String message) { | ||
this(message, null); | ||
} | ||
|
||
public SerializationException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am going to check with Lee what package name we should use.. I don't think we will use com.auth0