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();
}
}
Aggregations