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 all commits
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
189 changes: 189 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,189 @@
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 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();
}
}
14 changes: 14 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,14 @@
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.

/**
* This enum specifies the encryption and signing algorithm type
*/
public enum AlgorithmEnum {
RS256,
RS384,
RS512,
HS256,
HS384,
HS512,
ES256;
}
21 changes: 21 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,21 @@
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.

/**
* 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;
}
}
8 changes: 8 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,8 @@
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.

/**
* 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
}
14 changes: 14 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,14 @@
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

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

public class KeyBundle {
}
7 changes: 7 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,7 @@
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 {
public void addKeyBundle(String owner, KeyBundle kb) {
// TODO
}
}
110 changes: 110 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,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 {
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, 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();
}
Loading