use of com.google.api.client.auth.oauth2.TokenResponse in project data-transfer-project by google.
the class AuthorizationCodeInstalledAppSecureOverride method authorize.
/**
* Authorizes the installed application to access user's protected data.
*
* @param userId user ID or {@code null} if not using a persisted credential store
* @return credential
*/
public Credential authorize(String userId) throws Exception {
try {
System.out.println("loadCredential for: " + userId);
Credential credential = flow.loadCredential(userId);
if (credential != null && (credential.getRefreshToken() != null || credential.getExpiresInSeconds() > 60)) {
return credential;
}
// Ensure redirect http uri's are https
String redirectUri = receiver.getRedirectUri();
if (redirectUri.startsWith("http:")) {
redirectUri = redirectUri.replace("http:", "https:");
}
// open in browser
AuthorizationCodeRequestUrl authorizationUrl = flow.newAuthorizationUrl().setRedirectUri(redirectUri);
System.out.println("authorizationUrl: " + authorizationUrl);
onAuthorization(authorizationUrl);
// receive authorization code and exchange it for an access token
System.out.println("receiver.waitForCode()");
String code = receiver.waitForCode();
System.out.println("Code received from receiver: " + code);
TokenResponse response = flow.newTokenRequest(code).setRedirectUri(redirectUri).execute();
System.out.println("TokenResponse: " + response);
// store credential and return it
return flow.createAndStoreCredential(response, userId);
} finally {
receiver.stop();
}
}
use of com.google.api.client.auth.oauth2.TokenResponse in project hub-alert by blackducksoftware.
the class AzureBoardsProperties method requestTokens.
public Optional<Credential> requestTokens(AuthorizationCodeFlow authorizationCodeFlow, String authorizationCode) throws IOException {
AuthorizationCodeTokenRequest tokenRequest = authorizationCodeFlow.newTokenRequest(authorizationCode);
TokenResponse tokenResponse = tokenRequest.execute();
Credential credential = authorizationCodeFlow.createAndStoreCredential(tokenResponse, oauthUserId);
return Optional.ofNullable(credential);
}
use of com.google.api.client.auth.oauth2.TokenResponse in project hub-alert by blackducksoftware.
the class AzureCredential method executeRefreshToken.
@Override
protected TokenResponse executeRefreshToken() throws IOException {
String refreshToken = getRefreshToken();
if (refreshToken == null) {
if (cachedRefreshToken == null) {
return null;
}
refreshToken = cachedRefreshToken;
}
RefreshTokenRequest request = new RefreshTokenRequest(getTransport(), getJsonFactory(), new GenericUrl(getTokenServerEncodedUrl()), refreshToken);
request.setClientAuthentication(getClientAuthentication());
request.setRequestInitializer(getRequestInitializer());
request.setResponseClass(AzureTokenResponse.class);
request.put(AzureOAuthConstants.REQUEST_BODY_FIELD_ASSERTION, refreshToken);
request.put(AzureOAuthConstants.REQUEST_BODY_FIELD_CLIENT_ASSERTION_TYPE, AzureOAuthConstants.DEFAULT_CLIENT_ASSERTION_TYPE);
request.put(AzureOAuthConstants.REQUEST_BODY_FIELD_CLIENT_ASSERTION, clientSecret);
request.put(AzureOAuthConstants.REQUEST_BODY_FIELD_REDIRECT_URI, redirectUri);
return request.execute();
}
use of com.google.api.client.auth.oauth2.TokenResponse in project getting-started-java by GoogleCloudPlatform.
the class Oauth2CallbackServlet method doGet.
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
// sending us this connect request is the user that was supposed to.
if (req.getSession().getAttribute("state") == null || !req.getParameter("state").equals((String) req.getSession().getAttribute("state"))) {
resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
logger.log(Level.WARNING, "Invalid state parameter, expected " + (String) req.getSession().getAttribute("state") + " got " + req.getParameter("state"));
resp.sendRedirect("/books");
return;
}
// Remove one-time use state.
req.getSession().removeAttribute("state");
flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, getServletContext().getInitParameter("bookshelf.clientID"), getServletContext().getInitParameter("bookshelf.clientSecret"), SCOPES).build();
final TokenResponse tokenResponse = flow.newTokenRequest(req.getParameter("code")).setRedirectUri(getServletContext().getInitParameter("bookshelf.callback")).execute();
// Keep track of the token.
req.getSession().setAttribute("token", tokenResponse.toString());
final Credential credential = flow.createAndStoreCredential(tokenResponse, null);
final HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(credential);
// Make an authenticated request.
final GenericUrl url = new GenericUrl(USERINFO_ENDPOINT);
final HttpRequest request = requestFactory.buildGetRequest(url);
request.getHeaders().setContentType("application/json");
final String jsonIdentity = request.execute().parseAsString();
@SuppressWarnings("unchecked") HashMap<String, String> userIdResult = new ObjectMapper().readValue(jsonIdentity, HashMap.class);
// From this map, extract the relevant profile info and store it in the session.
req.getSession().setAttribute("userEmail", userIdResult.get("email"));
req.getSession().setAttribute("userId", userIdResult.get("sub"));
req.getSession().setAttribute("userImageUrl", userIdResult.get("picture"));
logger.log(Level.INFO, "Login successful, redirecting to " + (String) req.getSession().getAttribute("loginDestination"));
resp.sendRedirect((String) req.getSession().getAttribute("loginDestination"));
}
Aggregations