Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 180 additions & 0 deletions lib/src/main/java/com/auth0/msg/AbstractMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package com.auth0.msg;

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


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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add class level javadoc

Copy link
Author

Choose a reason for hiding this comment

The 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 {
Copy link

@manu-sinha manu-sinha Apr 16, 2018

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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 "";

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

/**
* Logic to extract from the string the values

Choose a reason for hiding this comment

The 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() {
Copy link

@manu-sinha manu-sinha Apr 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the 'to' methods toJson, toJwt and toUrlEncoded, show have throws SerializationException so that caller will know that it can happen and can handle it.

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

Choose a reason for hiding this comment

The 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();
}
}
11 changes: 11 additions & 0 deletions lib/src/main/java/com/auth0/msg/AlgorithmEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.auth0.msg;

Choose a reason for hiding this comment

The 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.
@lccodes FYI

Copy link
Author

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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;
}
18 changes: 18 additions & 0 deletions lib/src/main/java/com/auth0/msg/ClaimType.java
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;

Choose a reason for hiding this comment

The 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;
}
}
5 changes: 5 additions & 0 deletions lib/src/main/java/com/auth0/msg/DataLocation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.auth0.msg;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add javadoc.
@jdahmubed I think you will be reusing this enum.

public enum DataLocation {
FRAGMENT, QUERYPART

Choose a reason for hiding this comment

The 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

}
11 changes: 11 additions & 0 deletions lib/src/main/java/com/auth0/msg/InvalidClaimsException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.auth0.msg;

Choose a reason for hiding this comment

The 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);
}
}
4 changes: 4 additions & 0 deletions lib/src/main/java/com/auth0/msg/Key.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.auth0.msg;

public class Key {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add class level Javadoc
Aren't you going to extend from auth0 class?

}
4 changes: 4 additions & 0 deletions lib/src/main/java/com/auth0/msg/KeyJar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.auth0.msg;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add class level Javadoc
Aren't you going to extend from auth0 class? If so, please add extends

public class KeyJar {
}
108 changes: 108 additions & 0 deletions lib/src/main/java/com/auth0/msg/Message.java
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 {
Copy link

@manu-sinha manu-sinha Apr 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add throws SerializationException to all the to methods. I have explained the reason in the abstract class.


/**
* 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();
}
11 changes: 11 additions & 0 deletions lib/src/main/java/com/auth0/msg/SerializationException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.auth0.msg;

Choose a reason for hiding this comment

The 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);
}
}