Advertisement
lumpynose

Untitled

Jan 6th, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.52 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.net.URI;
  3. import java.net.URISyntaxException;
  4. import java.util.Collections;
  5.  
  6. import javax.inject.Inject;
  7.  
  8. import com.google.api.client.auth.oauth2.AuthorizationCodeRequestUrl;
  9. import com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest;
  10. import com.google.api.client.auth.oauth2.BearerToken;
  11. import com.google.api.client.auth.oauth2.ClientParametersAuthentication;
  12. import com.google.api.client.auth.oauth2.Credential;
  13. import com.google.api.client.auth.oauth2.CredentialStoreRefreshListener;
  14. import com.google.api.client.auth.oauth2.TokenResponse;
  15. import com.google.api.client.extensions.appengine.auth.oauth2.AppEngineCredentialStore;
  16. import com.google.api.client.extensions.appengine.http.UrlFetchTransport;
  17. import com.google.api.client.http.GenericUrl;
  18. import com.google.api.client.json.jackson2.JacksonFactory;
  19.  
  20. import org.slf4j.Logger;
  21. import org.slf4j.LoggerFactory;
  22.  
  23. @SuppressWarnings("deprecation")
  24. public class GoogleOAuth2Utils {
  25.     private final static Logger log = LoggerFactory.getLogger(GoogleOAuth2Utils.class);
  26.     private final GoogleOAuth2Config googleOAuth2Config;
  27.  
  28.     @Inject
  29.     public GoogleOAuth2Utils(final GoogleOAuth2Config googleOAuth2Config) {
  30.         this.googleOAuth2Config = googleOAuth2Config;
  31.     }
  32.  
  33.     public Credential blankCredential(final String userId) {
  34.         Credential credential =
  35.             new Credential.Builder(BearerToken.authorizationHeaderAccessMethod())
  36.                 .addRefreshListener(new CredentialStoreRefreshListener(userId,
  37.                     new AppEngineCredentialStore()))
  38.                 .setJsonFactory(new JacksonFactory())
  39.                 .setTokenServerUrl(new GenericUrl("https://accounts.google.com/o/oauth2/token"))
  40.                 .setClientAuthentication(
  41.                     new ClientParametersAuthentication(
  42.                         this.googleOAuth2Config.getGoogleApiKey(),
  43.                         this.googleOAuth2Config.getGoogleApiSecret()))
  44.                 .setTransport(new UrlFetchTransport())
  45.                 .build();
  46.  
  47.         return (credential);
  48.     }
  49.  
  50.     public Credential getCredential(String userId, TokenResponse tokenResponse) {
  51.         Credential credential = blankCredential(userId);
  52.  
  53.         credential.setFromTokenResponse(tokenResponse);
  54.  
  55.         if (tokenResponse.getRefreshToken() != null) {
  56.             log.debug("setting refresh token from token response");
  57.  
  58.             credential.setRefreshToken(tokenResponse.getRefreshToken());
  59.         }
  60.  
  61.         AppEngineCredentialStore appEngineCredentialStore = new AppEngineCredentialStore();
  62.  
  63.         appEngineCredentialStore.store(userId, credential);
  64.  
  65.         return (credential);
  66.     }
  67.     public TokenResponse tokenResponse(final String code, final String scheme, final String serverName, final int serverPort) throws IOException, URISyntaxException {
  68.         TokenResponse tokenResponse =
  69.             new AuthorizationCodeTokenRequest(new UrlFetchTransport(),
  70.                 new JacksonFactory(),
  71.                 new GenericUrl("https://accounts.google.com/o/oauth2/token"),
  72.                 code)
  73.             .setRedirectUri(googleOJCCallbackUri(scheme, serverName, serverPort))
  74.             .setClientAuthentication(new ClientParametersAuthentication(
  75.                 this.googleOAuth2Config.getGoogleApiKey(),
  76.                 this.googleOAuth2Config.getGoogleApiSecret()))
  77.             .execute();
  78.  
  79.         return (tokenResponse);
  80.     }
  81.  
  82.     public String callbackUrl(final String scheme, final String serverName, final int serverPort, final String requestUri) throws URISyntaxException {
  83.         String url =
  84.             new AuthorizationCodeRequestUrl("https://accounts.google.com/o/oauth2/auth", this.googleOAuth2Config.getGoogleApiKey())
  85.                 .setRedirectUri(googleOJCCallbackUri(scheme, serverName, serverPort))
  86.                 .setScopes(Collections.singletonList("https://www.googleapis.com/auth/urlshortener"))
  87.                 .setState(requestUri)
  88.                 .set("approval_prompt", "force")
  89.                 .set("access_type", "offline")
  90.                 .build();
  91.  
  92.         return (url);
  93.     }
  94.  
  95.     public String googleOJCCallbackUri(String scheme, String serverName, int serverPort) throws URISyntaxException {
  96.         final URI callbackUri = new URI(scheme,
  97.                 null,
  98.                 serverName,
  99.                 serverPort,
  100.                 this.googleOAuth2Config.getGoogleOJCCallbackPath(),
  101.                 null, null);
  102.  
  103.         return (callbackUri.normalize().toString());
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement