/src/main/java/com/google/ie/common/openid/OAuthConsumerUtil.java

http://thoughtsite.googlecode.com/ · Java · 73 lines · 41 code · 13 blank · 19 comment · 0 complexity · a7fbf2879f8792bab052c43b9b795dbe MD5 · raw file

  1. /* Copyright 2010 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS.
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License
  14. */
  15. package com.google.ie.common.openid;
  16. import com.google.inject.Inject;
  17. import com.google.step2.Step2OAuthClient;
  18. import org.apache.log4j.Logger;
  19. import net.oauth.OAuth;
  20. import net.oauth.OAuthAccessor;
  21. import net.oauth.OAuthException;
  22. import net.oauth.OAuthMessage;
  23. import java.io.IOException;
  24. import java.net.URISyntaxException;
  25. /**
  26. * Utility class for requesting OAuth tokens from accessors
  27. *
  28. * @author sweis@google.com (Steve Weis)
  29. */
  30. public class OAuthConsumerUtil {
  31. private Logger log = Logger.getLogger(OAuthConsumerUtil.class);
  32. private final Step2OAuthClient client;
  33. @Inject
  34. public OAuthConsumerUtil(Step2OAuthClient client) {
  35. this.client = client;
  36. }
  37. public OAuthAccessor getRequestToken(OAuthAccessor accessor)
  38. throws IOException, OAuthException, URISyntaxException {
  39. OAuthAccessor accessorCopy = new OAuthAccessor(accessor.consumer);
  40. OAuthMessage response = client.invoke(accessor,
  41. accessor.consumer.serviceProvider.requestTokenURL,
  42. OAuth.newList("scope", accessor.getProperty("scope").toString()));
  43. log.info("Successfully got OAuth request token");
  44. accessorCopy.requestToken = response.getParameter("oauth_token");
  45. accessorCopy.tokenSecret = response.getParameter("oauth_token_secret");
  46. return accessor;
  47. }
  48. public OAuthAccessor getAccessToken(OAuthAccessor accessor)
  49. throws IOException, OAuthException, URISyntaxException {
  50. OAuthAccessor accessorCopy = new OAuthAccessor(accessor.consumer);
  51. OAuthMessage response = client.invoke(accessor,
  52. accessor.consumer.serviceProvider.accessTokenURL,
  53. OAuth.newList("oauth_token", accessor.requestToken,
  54. "scope", accessor.getProperty("scope").toString()));
  55. log.info("Successfully got OAuth access token");
  56. accessorCopy.accessToken = response.getParameter("oauth_token");
  57. accessorCopy.tokenSecret = response.getParameter("oauth_token_secret");
  58. return accessorCopy;
  59. }
  60. }