Search in sources :

Example 1 with Userinfo

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();
    }
}
Also used : Userinfoplus(com.google.api.services.oauth2.model.Userinfoplus) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) Credential(com.google.api.client.auth.oauth2.Credential) AuthenticationException(com.owlplug.auth.utils.AuthenticationException) Oauth2(com.google.api.services.oauth2.Oauth2) GeneralSecurityException(java.security.GeneralSecurityException) ArrayList(java.util.ArrayList) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) AuthorizationCodeInstalledApp(com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp) JPADataStoreFactory(com.owlplug.auth.JPADataStoreFactory) LocalServerReceiver(com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver) IOException(java.io.IOException) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) UserAccount(com.owlplug.auth.model.UserAccount) DataStoreFactory(com.google.api.client.util.store.DataStoreFactory) JPADataStoreFactory(com.owlplug.auth.JPADataStoreFactory)

Example 2 with Userinfo

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();
}
Also used : User(org.youngmonkeys.example.ezyhttp.login.entity.User) Userinfo(com.google.api.services.oauth2.model.Userinfo)

Example 3 with Userinfo

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();
}
Also used : UsersApi(com.haleconnect.api.user.v1.api.UsersApi) Token(com.haleconnect.api.user.v1.model.Token) UserInfo(com.haleconnect.api.user.v1.model.UserInfo) HaleConnectUserInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectUserInfo) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) LoginApi(com.haleconnect.api.user.v1.api.LoginApi) Credentials(com.haleconnect.api.user.v1.model.Credentials) ApiException(com.haleconnect.api.user.v1.ApiException)

Example 4 with Userinfo

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) UserInfo(com.developmentontheedge.be5.model.UserInfo)

Example 5 with Userinfo

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();
}
Also used : Userinfoplus(com.google.api.services.oauth2.model.Userinfoplus) Test(org.junit.Test)

Aggregations

Userinfo (com.google.api.services.oauth2.model.Userinfo)10 Oauth2 (com.google.api.services.oauth2.Oauth2)8 Userinfoplus (com.google.api.services.oauth2.model.Userinfoplus)6 IOException (java.io.IOException)5 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)4 UserInfo (com.developmentontheedge.be5.model.UserInfo)3 Credential (com.google.api.client.auth.oauth2.Credential)3 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)3 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)3 UserInfo (org.alfresco.repo.event.v1.model.UserInfo)3 JsonObject (com.google.gson.JsonObject)2 CustomWebApplicationException (io.dockstore.webservice.CustomWebApplicationException)2 Test (org.junit.jupiter.api.Test)2 FirecloudMe (org.pmiops.workbench.firecloud.model.FirecloudMe)2 FirecloudUserInfo (org.pmiops.workbench.firecloud.model.FirecloudUserInfo)2 Timed (com.codahale.metrics.annotation.Timed)1 Session (com.developmentontheedge.be5.api.Session)1 JsonView (com.fasterxml.jackson.annotation.JsonView)1 ForIntent (com.google.actions.api.ForIntent)1 TransactionDecision (com.google.actions.api.response.helperintent.transactions.v3.TransactionDecision)1