|
| 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 | +} |
0 commit comments