PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/app/com/atlassian/connect/play/java/oauth/OAuthRequestValidator.java

https://bitbucket.org/awei/ac-play-java
Java | 110 lines | 83 code | 13 blank | 14 comment | 5 complexity | d9cc37f80ac739f5aa10d96b6c15ba62 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.connect.play.java.oauth;
  2. import com.atlassian.connect.play.java.BaseUrl;
  3. import com.atlassian.connect.play.java.PublicKeyStore;
  4. import com.atlassian.connect.play.java.util.Utils;
  5. import com.atlassian.fugue.Option;
  6. import com.google.common.collect.ImmutableMultimap;
  7. import com.google.common.collect.Iterables;
  8. import com.google.common.collect.Multimap;
  9. import net.oauth.*;
  10. import net.oauth.signature.RSA_SHA1;
  11. import play.Logger;
  12. import java.io.IOException;
  13. import java.net.URISyntaxException;
  14. import java.util.Collection;
  15. import static com.atlassian.connect.play.java.util.Utils.LOGGER;
  16. import static com.google.common.base.Preconditions.checkNotNull;
  17. import static com.google.common.base.Preconditions.checkState;
  18. import static java.lang.String.format;
  19. final class OAuthRequestValidator<R>
  20. {
  21. private final RequestHelper<R> requestHelper;
  22. private final PublicKeyStore publicKeyStore;
  23. private final BaseUrl baseUrl;
  24. /**
  25. * @param requestHelper the helper to extract information from the given type of request.
  26. * @param publicKeyStore the store to the public key, used to check the OAuth signature.
  27. * @param baseUrl the base URL of the remote app, this should return the same URL as the one found in the
  28. * {@code atlassian-remote-app.xml} descriptor.
  29. */
  30. public OAuthRequestValidator(RequestHelper<R> requestHelper, PublicKeyStore publicKeyStore, BaseUrl baseUrl)
  31. {
  32. this.requestHelper = checkNotNull(requestHelper);
  33. this.publicKeyStore = checkNotNull(publicKeyStore);
  34. this.baseUrl = checkNotNull(baseUrl);
  35. }
  36. /**
  37. * Validate the given request as an OAuth request. This method will return normally if the request is valid, it will
  38. * throw an exception otherwise.
  39. *
  40. * @param request the request to validate
  41. * @return the OAuth consumer key set in the request.
  42. * @throws InvalidOAuthRequestException if the request is invalid.
  43. */
  44. public String validate(R request)
  45. {
  46. final Multimap<String, String> parameters = getParameters(request);
  47. final String consumerKey = getConsumerKey(parameters);
  48. final OAuthMessage message = new OAuthMessage(
  49. requestHelper.getHttpMethod(request),
  50. requestHelper.getUrl(request, baseUrl),
  51. parameters.entries());
  52. try
  53. {
  54. final OAuthConsumer host = new OAuthConsumer(null, consumerKey, null, null);
  55. final String publicKey = publicKeyStore.getPublicKey(consumerKey);
  56. if (publicKey == null)
  57. {
  58. throw new UnknownAcHostException(consumerKey);
  59. }
  60. host.setProperty(RSA_SHA1.PUBLIC_KEY, publicKey);
  61. message.validateMessage(new OAuthAccessor(host), new SimpleOAuthValidator());
  62. return consumerKey;
  63. }
  64. catch (OAuthProblemException e)
  65. {
  66. LOGGER.warn("The request is not a valid OAuth request", e);
  67. throw new UnauthorisedOAuthRequestException(format("Validation failed: \nproblem: %s\nparameters: %s\n", e.getProblem(), e.getParameters()), e);
  68. }
  69. catch (OAuthException | IOException | URISyntaxException e)
  70. {
  71. LOGGER.error("An error happened validating the OAuth request.", e);
  72. throw new RuntimeException(e);
  73. }
  74. }
  75. private Multimap<String, String> getParameters(R request)
  76. {
  77. final ImmutableMultimap.Builder<String, String> parameters =
  78. ImmutableMultimap.<String, String>builder().putAll(requestHelper.getParameters(request));
  79. final Option<String> authorization = requestHelper.getHeader(request, "Authorization");
  80. if (authorization.isDefined())
  81. {
  82. for (OAuth.Parameter param : OAuthMessage.decodeAuthorization(authorization.get()))
  83. {
  84. parameters.put(param.getKey(), param.getValue());
  85. }
  86. }
  87. return parameters.build();
  88. }
  89. private String getConsumerKey(Multimap<String, String> parameters)
  90. {
  91. final Collection<String> consumerKeys = parameters.get("oauth_consumer_key");
  92. checkState(consumerKeys.size() == 1, "There should be only one value for the consumer key");
  93. String consumerKey = Iterables.getFirst(consumerKeys, null);
  94. LOGGER.debug("Found consumer key '" + consumerKey + "'.");
  95. return consumerKey;
  96. }
  97. }