Skip to content

Commit 73f1a68

Browse files
committed
Adding implementations of LtiVerifier & LtiSigner
1 parent 351cda9 commit 73f1a68

File tree

7 files changed

+137
-7
lines changed

7 files changed

+137
-7
lines changed

src/main/java/org/imsglobal/aspect/LtiLaunchVerifier.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.aspectj.lang.annotation.Aspect;
1212
import org.imsglobal.basiclti.BasicLTIUtil;
1313
import org.imsglobal.basiclti.LtiVerificationResult;
14+
import org.imsglobal.basiclti.LtiVerifier;
1415

1516
import java.util.ArrayList;
1617
import java.util.List;
@@ -24,8 +25,11 @@ public class LtiLaunchVerifier {
2425

2526
public LtiKeySecretService keyService;
2627

27-
public LtiLaunchVerifier(LtiKeySecretService ltiKeySecretService) {
28-
this.keyService = ltiKeySecretService;
28+
public LtiVerifier ltiVerifier;
29+
30+
public LtiLaunchVerifier(LtiKeySecretService keyService, LtiVerifier ltiVerifier) {
31+
this.keyService = keyService;
32+
this.ltiVerifier = ltiVerifier;
2933
}
3034

3135
//@Around("@annotation(launch) && execution(* *(javax.servlet.http.HttpServletRequest+, org.imsglobal.basiclti.LtiVerificationResult, ..)) && args(request, result)")
@@ -42,7 +46,7 @@ public Object verifyLtiLaunch(ProceedingJoinPoint pjp, Lti launch) throws Throwa
4246
}
4347

4448
String oauthSecret = keyService.getSecretForKey(request.getParameter("oauth_consumer_key"));
45-
LtiVerificationResult ltiResult = BasicLTIUtil.validateMessage(request, request.getRequestURL().toString(), oauthSecret);
49+
LtiVerificationResult ltiResult = ltiVerifier.verify(request, oauthSecret);//BasicLTIUtil.validateMessage(request, request.getRequestURL().toString(), oauthSecret);
4650

4751
Boolean ltiVerificationResultExists = false;
4852
//This array will hold the arguments to the join point, so we can pass them along to the advised function.

src/main/java/org/imsglobal/basiclti/LtiLaunch.java

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

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

56
/**
67
* Created by paul on 5/28/14.
@@ -27,6 +28,16 @@ public LtiLaunch(HttpServletRequest request) {
2728
this.toolConsumerInstanceGuid = request.getParameter("tool_consumer_instance_guid");
2829
}
2930

31+
public LtiLaunch(Map<String, String> parameters) {
32+
this.user = new LtiUser(parameters);
33+
this.version = parameters.get("lti_version");
34+
this.messageType = parameters.get("lti_message_type");
35+
this.resourceLinkId = parameters.get("resource_link_id");
36+
this.contextId = parameters.get("context_id");
37+
this.launchPresentationReturnUrl = parameters.get("launch_presentation_return_url");
38+
this.toolConsumerInstanceGuid = parameters.get("tool_consumer_instance_guid");
39+
}
40+
3041
public LtiUser getUser() {
3142
return user;
3243
}

src/main/java/org/imsglobal/basiclti/LtiOauthSigner.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
package org.imsglobal.basiclti;
22

3+
import net.oauth.OAuthAccessor;
4+
import net.oauth.OAuthConsumer;
5+
import net.oauth.OAuthException;
6+
import net.oauth.OAuthMessage;
37
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
48
import oauth.signpost.exception.OAuthCommunicationException;
59
import oauth.signpost.exception.OAuthExpectationFailedException;
610
import oauth.signpost.exception.OAuthMessageSignerException;
711
import org.apache.http.HttpRequest;
812

13+
import java.io.IOException;
14+
import java.net.URISyntaxException;
15+
import java.util.HashMap;
16+
import java.util.List;
17+
import java.util.Map;
18+
19+
import static org.imsglobal.basiclti.BasicLTIConstants.*;
20+
921
/**
10-
* Created by pgray on 8/23/14.
22+
* This class <b>signs</b> LTI requests according to the Oauth 1.0 spec
23+
* @author Paul Gray
24+
* @since 1.1
1125
*/
1226
public class LtiOauthSigner implements LtiSigner {
1327

@@ -22,4 +36,22 @@ public HttpRequest sign(HttpRequest request, String key, String secret) throws L
2236
return request;
2337
}
2438

39+
@Override
40+
public Map<String, String> signParameters(Map<String, String> parameters, String key, String secret, String url, String method) throws LtiSigningException {
41+
OAuthMessage oam = new OAuthMessage(method, url, parameters.entrySet());
42+
OAuthConsumer cons = new OAuthConsumer(null, key, secret, null);
43+
OAuthAccessor acc = new OAuthAccessor(cons);
44+
try {
45+
oam.addRequiredParameters(acc);
46+
47+
Map<String, String> signedParameters = new HashMap<>();
48+
for(Map.Entry<String, String> param : oam.getParameters()){
49+
signedParameters.put(param.getKey(), param.getValue());
50+
}
51+
return signedParameters;
52+
} catch (OAuthException |IOException |URISyntaxException e) {
53+
throw new LtiSigningException("Error signing LTI request.", e);
54+
}
55+
}
56+
2557
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.imsglobal.basiclti;
2+
3+
import net.oauth.*;
4+
import net.oauth.server.OAuthServlet;
5+
import net.oauth.signature.OAuthSignatureMethod;
6+
7+
import javax.servlet.http.HttpServletRequest;
8+
import java.io.IOException;
9+
import java.net.URISyntaxException;
10+
import java.util.Arrays;
11+
import java.util.Map;
12+
import java.util.logging.Level;
13+
import java.util.logging.Logger;
14+
15+
/**
16+
* This class <b>verifies</b> LTI launches according to the Oauth 1.0 spec
17+
* @author Paul Gray
18+
* @since 1.1
19+
*/
20+
public class LtiOauthVerifier implements LtiVerifier {
21+
22+
public static final String OAUTH_KEY_PARAMETER= "oauth_consumer_key";
23+
24+
private final static Logger logger = Logger.getLogger(LtiOauthVerifier.class.getName());
25+
26+
/**
27+
* This method verifies the signed HttpServletRequest
28+
* @param request the HttpServletRequest that will be verified
29+
* @param secret the secret to verify the properties with
30+
* @return the result of the verification, along with contextual
31+
* information
32+
* @throws LtiVerificationException
33+
*/
34+
@Override
35+
public LtiVerificationResult verify(HttpServletRequest request, String secret) throws LtiVerificationException {
36+
OAuthMessage oam = OAuthServlet.getMessage(request, OAuthServlet.getRequestURL(request));
37+
String oauth_consumer_key = null;
38+
try {
39+
oauth_consumer_key = oam.getConsumerKey();
40+
} catch (Exception e) {
41+
return new LtiVerificationResult(false, LtiError.BAD_REQUEST, "Unable to find consumer key in message");
42+
}
43+
44+
OAuthValidator oav = new SimpleOAuthValidator();
45+
OAuthConsumer cons = new OAuthConsumer(null, oauth_consumer_key, secret, null);
46+
OAuthAccessor acc = new OAuthAccessor(cons);
47+
48+
try {
49+
oav.validateMessage(oam, acc);
50+
} catch (Exception e) {
51+
return new LtiVerificationResult(false, LtiError.BAD_REQUEST, "Failed to validate: " + e.getLocalizedMessage());
52+
}
53+
return new LtiVerificationResult(true, new LtiLaunch(request));
54+
}
55+
56+
@Override
57+
public LtiVerificationResult verifyParameters(Map<String, String> parameters, String url, String method, String secret) throws LtiVerificationException {
58+
OAuthMessage oam = new OAuthMessage(method, url, parameters.entrySet());
59+
OAuthConsumer cons = new OAuthConsumer(null, parameters.get(OAUTH_KEY_PARAMETER), secret, null);
60+
OAuthValidator oav = new SimpleOAuthValidator();
61+
OAuthAccessor acc = new OAuthAccessor(cons);
62+
63+
try {
64+
oav.validateMessage(oam, acc);
65+
} catch (Exception e) {
66+
return new LtiVerificationResult(false, LtiError.BAD_REQUEST, "Failed to validate: " + e.getLocalizedMessage() + ", Parameters: " + Arrays.toString(parameters.entrySet().toArray()));
67+
}
68+
return new LtiVerificationResult(true, new LtiLaunch(parameters));
69+
}
70+
}

src/main/java/org/imsglobal/basiclti/LtiSigner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ public interface LtiSigner {
4242
* @return a map of signed parameters (including the signature)
4343
* @throws LtiSigningException
4444
*/
45-
public HttpRequest signParameters(Map<String, String> parameters, String key, String secret) throws LtiSigningException;
45+
public Map<String, String> signParameters(Map<String, String> parameters, String key, String secret, String url, String method) throws LtiSigningException;
4646

4747
}

src/main/java/org/imsglobal/basiclti/LtiUser.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import javax.servlet.http.HttpServletRequest;
44
import java.util.LinkedList;
55
import java.util.List;
6+
import java.util.Map;
67

78
/**
89
* Created by paul on 5/28/14.
@@ -22,6 +23,16 @@ public LtiUser(HttpServletRequest request) {
2223
}
2324
}
2425

26+
public LtiUser(Map<String, String> parameters) {
27+
this.id = parameters.get("user_id");
28+
this.roles = new LinkedList<>();
29+
if(parameters.get("roles") != null) {
30+
for (String role : parameters.get("roles").split(",")) {
31+
this.roles.add(role.trim());
32+
}
33+
}
34+
}
35+
2536
public String getId() {
2637
return id;
2738
}

src/main/java/org/imsglobal/basiclti/LtiVerifier.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ public interface LtiVerifier {
3232
* This method will verify a list of properties (mapped
3333
* by key & value).
3434
* @param parameters the parameters that will be verified. mapped by key & value
35-
* @param secret the secret to verify the properties with
35+
* @param url the url this request was made at
36+
* @param method the method this url was requested with
37+
* @param secret the secret to verify the propertihes with
3638
* @return an LtiVerificationResult which will
3739
* contain information about the request (whether or
3840
* not it is valid, and if it is valid, contextual
3941
* information about the request).
4042
* @throws LtiVerificationException
4143
*/
42-
public LtiVerificationResult verifyParameters(Map<String, String> parameters, String secret) throws LtiVerificationException;
44+
public LtiVerificationResult verifyParameters(Map<String, String> parameters, String url, String method, String secret) throws LtiVerificationException;
4345

4446
}

0 commit comments

Comments
 (0)