use of com.google.api.services.oauth2.model.Userinfo in project OwlPlug by DropSnorz.
the class AuthenticationService method createAccountAndAuth.
/**
* Creates a new account by starting the Authentication flow.
*
* @throws AuthenticationException if an error occurs during Authentication
* flow.
*/
public void createAccountAndAuth() throws AuthenticationException {
String clientId = owlPlugCredentials.getGoogleAppId();
String clientSecret = owlPlugCredentials.getGoogleSecret();
ArrayList<String> scopes = new ArrayList<>();
scopes.add("https://www.googleapis.com/auth/drive");
scopes.add("https://www.googleapis.com/auth/userinfo.profile");
try {
NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
DataStoreFactory dataStore = new JPADataStoreFactory(googleCredentialDAO);
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientId, clientSecret, scopes).setDataStoreFactory(dataStore).setAccessType("offline").setApprovalPrompt("force").build();
UserAccount userAccount = new UserAccount();
userAccountDAO.save(userAccount);
receiver = new LocalServerReceiver();
AuthorizationCodeInstalledApp authCodeAccess = new AuthorizationCodeInstalledApp(flow, receiver);
Credential credential = authCodeAccess.authorize(userAccount.getKey());
Oauth2 oauth2 = new Oauth2.Builder(new NetHttpTransport(), new JacksonFactory(), credential).setApplicationName("OwlPlug").build();
Userinfoplus userinfo = oauth2.userinfo().get().execute();
userAccount.setName(userinfo.getName());
userAccount.setIconUrl(userinfo.getPicture());
userAccount.setAccountProvider(UserAccountProvider.GOOGLE);
userAccount.setCredential(googleCredentialDAO.findByKey(userAccount.getKey()));
userAccountDAO.save(userAccount);
this.getPreferences().putLong(ApplicationDefaults.SELECTED_ACCOUNT_KEY, userAccount.getId());
} catch (GeneralSecurityException | IOException e) {
log.error("Error during authentication", e);
throw new AuthenticationException(e);
} finally {
// Delete accounts without complete setup
userAccountDAO.deleteInvalidAccounts();
}
}
use of com.google.api.services.oauth2.model.Userinfo in project ezyfox-examples by tvd12.
the class LoginController method loginWithGoogle.
@DoGet("/google-login-callback")
public Object loginWithGoogle(@RequestParam String code) {
String googleAccessToken = googleService.getAccessToken(code);
if (googleAccessToken == null) {
return Redirect.to("/login-error");
}
Userinfo googleUserInfo = googleService.getUserInfoByAccessToken(googleAccessToken);
if (googleUserInfo == null) {
return Redirect.to("/login-error");
}
boolean userExisted = true;
User user = userService.getUserInfoByEmail(googleUserInfo.getEmail());
if (user == null) {
userExisted = false;
user = userService.saveGoogleUserInfo(googleUserInfo);
userDataService.saveGoogleToken(user.getId(), googleAccessToken);
}
String accessToken = authenticationService.generateAccessToken(user.getId());
return userExisted && user.getStatus() == UserStatus.UPDATED ? Redirect.builder().uri("/home").addCookie("accessToken", accessToken).build() : Redirect.builder().uri("/user/update").addCookie("accessToken", accessToken).build();
}
use of com.google.api.services.oauth2.model.Userinfo in project hale by halestudio.
the class HaleConnectServiceImpl method login.
/**
* @see eu.esdihumboldt.hale.io.haleconnect.HaleConnectService#login(java.lang.String,
* java.lang.String)
*/
@Override
public boolean login(String username, String password) throws HaleConnectException {
LoginApi loginApi = UserServiceHelper.getLoginApi(this);
Credentials credentials = UserServiceHelper.buildCredentials(username, password);
try {
Token token = loginApi.login(credentials);
if (token != null) {
UsersApi usersApi = UserServiceHelper.getUsersApi(this, token.getToken());
// First get the current user's profile to obtain the user ID
// required to fetch the extended profile (including the user's
// roles/organisations) in the next step
UserInfo shortProfile = usersApi.getProfileOfCurrentUser();
session = new HaleConnectSessionImpl(username, token.getToken(), usersApi.getProfile(shortProfile.getId()));
notifyLoginStateChanged();
} else {
clearSession();
}
} catch (ApiException e) {
if (e.getCode() == 401) {
clearSession();
} else {
throw new HaleConnectException(e.getMessage(), e);
}
}
return isLoggedIn();
}
use of com.google.api.services.oauth2.model.Userinfo in project be5 by DevelopmentOnTheEdge.
the class AuthenticationPropagationListener method requestInitialized.
@Override
public void requestInitialized(ServletRequestEvent event) {
HttpServletRequest request = (HttpServletRequest) event.getServletRequest();
HttpSession session = request.getSession(false);
if (session == null) {
return;
}
UserInfo user = (UserInfo) session.getAttribute(SessionConstants.USER_INFO);
UserInfoHolder.setUserInfo(user);
}
use of com.google.api.services.oauth2.model.Userinfo in project workbench by all-of-us.
the class AuthInterceptorTest method preHandleGet_userInfoSuccess.
@Test
public void preHandleGet_userInfoSuccess() throws Exception {
when(handler.getMethod()).thenReturn(getProfileApiMethod("getBillingProjects"));
when(request.getMethod()).thenReturn(HttpMethods.GET);
when(request.getHeader(HttpHeaders.AUTHORIZATION)).thenReturn("Bearer foo");
Userinfoplus userInfo = new Userinfoplus();
userInfo.setEmail("bob@fake-domain.org");
when(userInfoService.getUserInfo("foo")).thenReturn(userInfo);
when(userDao.findUserByEmail("bob@fake-domain.org")).thenReturn(user);
assertThat(interceptor.preHandle(request, response, handler)).isTrue();
}
Aggregations