|
4 | 4 | import net.oauth.server.OAuthServlet;
|
5 | 5 |
|
6 | 6 | import javax.servlet.http.HttpServletRequest;
|
7 |
| -import java.util.Arrays; |
8 |
| -import java.util.Map; |
| 7 | +import java.util.*; |
9 | 8 | import java.util.logging.Logger;
|
10 | 9 |
|
11 | 10 | /**
|
|
15 | 14 | */
|
16 | 15 | public class LtiOauthVerifier implements LtiVerifier {
|
17 | 16 |
|
18 |
| - public static final String OAUTH_KEY_PARAMETER= "oauth_consumer_key"; |
| 17 | + public static final String OAUTH_KEY_PARAMETER = "oauth_consumer_key"; |
19 | 18 |
|
20 | 19 | private final static Logger logger = Logger.getLogger(LtiOauthVerifier.class.getName());
|
21 | 20 |
|
@@ -60,16 +59,39 @@ public LtiVerificationResult verify(HttpServletRequest request, String secret) t
|
60 | 59 | */
|
61 | 60 | @Override
|
62 | 61 | 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 | + } |
67 | 64 |
|
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 | + } |
72 | 94 | }
|
73 |
| - return new LtiVerificationResult(true, new LtiLaunch(parameters)); |
| 95 | + return null; |
74 | 96 | }
|
75 | 97 | }
|
0 commit comments