-
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 all commits
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,189 @@ | ||
package com.auth0.msg; | ||
|
||
import com.auth0.jwt.algorithms.Algorithm; | ||
import com.fasterxml.jackson.core.JsonGenerationException; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* This abstract class provides basic processing of messages | ||
*/ | ||
public abstract class AbstractMessage implements Message { | ||
private Map<ClaimType, Object> claims; | ||
private String input; | ||
private Error error = null; | ||
private boolean verified = false; | ||
ObjectMapper mapper = new ObjectMapper(); | ||
|
||
protected AbstractMessage(Map<ClaimType, Object> claims) { | ||
this.claims = claims; | ||
} | ||
|
||
/** | ||
* @param input the urlEncoded String representation of a message | ||
* @return a Message representation of the UrlEncoded string | ||
*/ | ||
public Message fromUrlEncoded(String input) throws MalformedURLException, IOException { | ||
AbstractMessage msg = mapper.readValue(new URL(input), AbstractMessage.class); | ||
return msg; | ||
} | ||
|
||
/** | ||
* Takes the claims of this instance of the AbstractMessage class and serializes them | ||
* to an urlEncoded string | ||
* | ||
* @return an urlEncoded string | ||
*/ | ||
public String toUrlEncoded() throws SerializationException { | ||
// TODO | ||
// Serialize the content of this instance (the claims map) into an UrlEncoded string | ||
return ""; | ||
} | ||
|
||
/** | ||
* Logic to extract from the JSON string the values | ||
* | ||
* @param input The JSON String representation of a message | ||
* @return a Message representation of the Json | ||
*/ | ||
public Message fromJson(String input) { | ||
this.input = input; | ||
try { | ||
// Convert JSON string to Object | ||
AbstractMessage msg = mapper.readValue(input, AbstractMessage.class); | ||
return msg; | ||
} catch (JsonGenerationException e) { | ||
e.printStackTrace(); | ||
} catch (JsonMappingException e) { | ||
e.printStackTrace(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* 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() throws SerializationException { | ||
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 Message representation of the JWT | ||
*/ | ||
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, SerializationException { | ||
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, SerializationException { | ||
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 String 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 claims for this messsage | ||
*/ | ||
public Map<ClaimType, Object> getClaims(){ | ||
return this.claims; | ||
} | ||
|
||
/** | ||
* @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,14 @@ | ||
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. |
||
/** | ||
* This enum specifies the encryption and signing algorithm type | ||
*/ | ||
public enum AlgorithmEnum { | ||
RS256, | ||
RS384, | ||
RS512, | ||
HS256, | ||
HS384, | ||
HS512, | ||
ES256; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
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. |
||
/** | ||
* This enum specifies the claims and their allowed values to allow for validation of messages | ||
*/ | ||
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,8 @@ | ||
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. |
||
/** | ||
* This enum specifies whether the data will be placed in a fragment or in a query part | ||
*/ | ||
public enum DataLocation { | ||
FRAGMENT, QUERY_STRING, FORM_POST | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
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 |
||
/** | ||
* A runtime exception that is thrown when there is an invalid claim in a Message object type | ||
*/ | ||
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,8 @@ | ||
package com.auth0.msg; | ||
|
||
public class Jwk { | ||
public Key importPrivateRsaKeyFromFile(String filename){ | ||
// TODO | ||
return new Key(); | ||
} | ||
} |
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; | ||
|
||
public class KeyBundle { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
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 { | ||
public void addKeyBundle(String owner, KeyBundle kb) { | ||
// TODO | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package com.auth0.msg; | ||
|
||
import com.auth0.jwt.algorithms.Algorithm; | ||
|
||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
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, SerializationException; | ||
|
||
/** | ||
* 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, SerializationException; | ||
|
||
/** | ||
* 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) throws MalformedURLException, IOException; | ||
|
||
/** | ||
* | ||
* @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(); | ||
} |
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