Search in sources :

Example 1 with AuthenticationException

use of com.owlplug.auth.utils.AuthenticationException 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)

Aggregations

Credential (com.google.api.client.auth.oauth2.Credential)1 AuthorizationCodeInstalledApp (com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp)1 LocalServerReceiver (com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver)1 GoogleAuthorizationCodeFlow (com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow)1 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)1 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)1 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)1 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1 DataStoreFactory (com.google.api.client.util.store.DataStoreFactory)1 Oauth2 (com.google.api.services.oauth2.Oauth2)1 Userinfoplus (com.google.api.services.oauth2.model.Userinfoplus)1 JPADataStoreFactory (com.owlplug.auth.JPADataStoreFactory)1 UserAccount (com.owlplug.auth.model.UserAccount)1 AuthenticationException (com.owlplug.auth.utils.AuthenticationException)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 ArrayList (java.util.ArrayList)1