Skip to content

Commit 83cabd1

Browse files
committed
Adding extra verification method to support validating requests that may contain arrays, solves #33
1 parent f79187c commit 83cabd1

File tree

4 files changed

+84
-16
lines changed

4 files changed

+84
-16
lines changed

src/main/java/org/imsglobal/lti/launch/LtiLaunch.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.imsglobal.lti.launch;
22

33
import javax.servlet.http.HttpServletRequest;
4+
import java.util.Collection;
45
import java.util.Map;
56

67
/**
@@ -38,6 +39,16 @@ public LtiLaunch(Map<String, String> parameters) {
3839
this.toolConsumerInstanceGuid = parameters.get("tool_consumer_instance_guid");
3940
}
4041

42+
public LtiLaunch(Collection<? extends Map.Entry> parameters) {
43+
this.user = new LtiUser(parameters);
44+
this.version = LtiOauthVerifier.getKey(parameters, "lti_version");
45+
this.messageType = LtiOauthVerifier.getKey(parameters, "lti_message_type");
46+
this.resourceLinkId = LtiOauthVerifier.getKey(parameters, "resource_link_id");
47+
this.contextId = LtiOauthVerifier.getKey(parameters, "context_id");
48+
this.launchPresentationReturnUrl = LtiOauthVerifier.getKey(parameters, "launch_presentation_return_url");
49+
this.toolConsumerInstanceGuid = LtiOauthVerifier.getKey(parameters, "tool_consumer_instance_guid");
50+
}
51+
4152
public LtiUser getUser() {
4253
return user;
4354
}

src/main/java/org/imsglobal/lti/launch/LtiOauthVerifier.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import net.oauth.server.OAuthServlet;
55

66
import javax.servlet.http.HttpServletRequest;
7-
import java.util.Arrays;
8-
import java.util.Map;
7+
import java.util.*;
98
import java.util.logging.Logger;
109

1110
/**
@@ -15,7 +14,7 @@
1514
*/
1615
public class LtiOauthVerifier implements LtiVerifier {
1716

18-
public static final String OAUTH_KEY_PARAMETER= "oauth_consumer_key";
17+
public static final String OAUTH_KEY_PARAMETER = "oauth_consumer_key";
1918

2019
private final static Logger logger = Logger.getLogger(LtiOauthVerifier.class.getName());
2120

@@ -60,16 +59,39 @@ public LtiVerificationResult verify(HttpServletRequest request, String secret) t
6059
*/
6160
@Override
6261
public LtiVerificationResult verifyParameters(Map<String, String> parameters, String url, String method, String secret) throws LtiVerificationException {
63-
OAuthMessage oam = new OAuthMessage(method, url, parameters.entrySet());
64-
OAuthConsumer cons = new OAuthConsumer(null, parameters.get(OAUTH_KEY_PARAMETER), secret, null);
65-
OAuthValidator oav = new SimpleOAuthValidator();
66-
OAuthAccessor acc = new OAuthAccessor(cons);
62+
return verifyParameters(parameters.entrySet(), url, method, secret);
63+
}
6764

68-
try {
69-
oav.validateMessage(oam, acc);
70-
} catch (Exception e) {
71-
return new LtiVerificationResult(false, LtiError.BAD_REQUEST, "Failed to validate: " + e.getLocalizedMessage() + ", Parameters: " + Arrays.toString(parameters.entrySet().toArray()));
65+
@Override
66+
public LtiVerificationResult verifyParameters(Collection<? extends Map.Entry> parameters, String url, String method, String secret) throws LtiVerificationException {
67+
OAuthMessage oam = new OAuthMessage(method, url, parameters);
68+
String key = getKey(parameters, OAUTH_KEY_PARAMETER);
69+
if(key == null) {
70+
return new LtiVerificationResult(false, LtiError.BAD_REQUEST, "No key found in LTI request with parameters: " + Arrays.toString(parameters.toArray()));
71+
} else {
72+
OAuthConsumer cons = new OAuthConsumer(null, key, secret, null);
73+
OAuthValidator oav = new SimpleOAuthValidator();
74+
OAuthAccessor acc = new OAuthAccessor(cons);
75+
76+
try {
77+
oav.validateMessage(oam, acc);
78+
} catch (Exception e) {
79+
return new LtiVerificationResult(false, LtiError.BAD_REQUEST, "Failed to validate: " + e.getLocalizedMessage() + ", Parameters: " + Arrays.toString(parameters.toArray()));
80+
}
81+
return new LtiVerificationResult(true, new LtiLaunch(parameters));
82+
}
83+
}
84+
85+
/**
86+
* Given a collection of parameters, return the first value for the given key.
87+
* returns null if no entry is found with the given key.
88+
*/
89+
public static String getKey(Collection<? extends Map.Entry> parameters, String parameterName) {
90+
for(Map.Entry<String, String> entry: parameters) {
91+
if(entry.getKey().equals(parameterName)) {
92+
return entry.getValue();
93+
}
7294
}
73-
return new LtiVerificationResult(true, new LtiLaunch(parameters));
95+
return null;
7496
}
7597
}

src/main/java/org/imsglobal/lti/launch/LtiUser.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.imsglobal.lti.launch;
22

33
import javax.servlet.http.HttpServletRequest;
4+
import java.util.Collection;
45
import java.util.LinkedList;
56
import java.util.List;
67
import java.util.Map;
@@ -33,6 +34,22 @@ public LtiUser(Map<String, String> parameters) {
3334
}
3435
}
3536

37+
public LtiUser(Collection<? extends Map.Entry> parameters) {
38+
this.id = LtiOauthVerifier.getKey(parameters, "user_id");
39+
this.roles = new LinkedList<>();
40+
String parameterRoles = LtiOauthVerifier.getKey(parameters, "roles");
41+
if(parameterRoles != null) {
42+
for (String role : parameterRoles.split(",")) {
43+
this.roles.add(role.trim());
44+
}
45+
}
46+
}
47+
48+
public LtiUser(String id, List<String> roles) {
49+
this.id = id;
50+
this.roles = roles;
51+
}
52+
3653
public String getId() {
3754
return id;
3855
}

src/main/java/org/imsglobal/lti/launch/LtiVerifier.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import javax.servlet.http.HttpServletRequest;
5+
import java.util.Collection;
56
import java.util.Map;
67

78
/**
@@ -24,13 +25,13 @@ public interface LtiVerifier {
2425
* information about the request).
2526
* @throws LtiVerificationException
2627
*/
27-
public LtiVerificationResult verify(HttpServletRequest request, String secret) throws LtiVerificationException;
28+
LtiVerificationResult verify(HttpServletRequest request, String secret) throws LtiVerificationException;
2829

2930
/**
3031
* This method will verify a list of properties (mapped
3132
* by key &amp; value).
32-
* @param parameters the parameters that will be verified. mapped by key &amp; value
33-
* @param url the url this request was made at
33+
* @param parameters the parameters that will be verified. mapped by key &amp; value. This should only include parameters explicitly included in the body (not the url).
34+
* @param url The url this request was made at. The url passed should be the same as sent for the request (along with any parameters).
3435
* @param method the method this url was requested with
3536
* @param secret the secret to verify the propertihes with
3637
* @return an LtiVerificationResult which will
@@ -39,6 +40,23 @@ public interface LtiVerifier {
3940
* information about the request).
4041
* @throws LtiVerificationException
4142
*/
42-
public LtiVerificationResult verifyParameters(Map<String, String> parameters, String url, String method, String secret) throws LtiVerificationException;
43+
LtiVerificationResult verifyParameters(Map<String, String> parameters, String url, String method, String secret) throws LtiVerificationException;
44+
45+
/**
46+
* This method will verify a list of properties (mapped
47+
* by key &amp; value).
48+
* @param parameters the parameters that will be verified. mapped by key &amp; value. This should only include parameters explicitly included in the body (not the url).
49+
* The entries must be of type `Entry<String,String>`. If a specific key has multiple values (i.e. an array), each value must be in its own entry, each
50+
* with the same key.
51+
* @param url The url this request was made at. The url passed should be the same as sent for the request (along with any parameters).
52+
* @param method the method this url was requested with
53+
* @param secret the secret to verify the propertihes with
54+
* @return an LtiVerificationResult which will
55+
* contain information about the request (whether or
56+
* not it is valid, and if it is valid, contextual
57+
* information about the request).
58+
* @throws LtiVerificationException
59+
*/
60+
LtiVerificationResult verifyParameters(Collection<? extends Map.Entry> parameters, String url, String method, String secret) throws LtiVerificationException;
4361

4462
}

0 commit comments

Comments
 (0)