private void refreshUsingOAuth2Client(String name, String refreshToken) throws TokenRefreshException { try { final OAuth2Token token = oauth2Client.refreshToken(refreshToken); authPreferences.setOauth2Token(name, token.accessToken, isEmpty(token.refreshToken) ? refreshToken : token.refreshToken); } catch (IOException e) { throw new TokenRefreshException(e); } } }
public void clearOauth2Data() { final String oauth2token = getOauth2Token(); preferences.edit() .remove(OAUTH2_USER) .commit(); getCredentials().edit() .remove(OAUTH2_TOKEN) .remove(OAUTH2_REFRESH_TOKEN) .commit(); if (!TextUtils.isEmpty(oauth2token)) { new TokenRefresher(context, new OAuth2Client(getOAuth2ClientId()), this).invalidateToken(oauth2token); } }
public OAuth2Token getToken(String code) throws IOException { HttpsURLConnection connection = postTokenEndpoint(getAccessTokenPostData(code)); final int responseCode = connection.getResponseCode(); if (responseCode == HttpsURLConnection.HTTP_OK) { OAuth2Token token = parseResponse(connection.getInputStream()); String username = getUsernameFromContacts(token); Log.d(TAG, "got token " + token.getTokenForLogging()+ ", username="+username); return new OAuth2Token(token.accessToken, token.tokenType, token.refreshToken, token.expiresIn, username); } else { Log.e(TAG, "error: " + responseCode); throw new IOException("Invalid response from server:" + responseCode); } }
public OAuth2Token refreshToken(String refreshToken) throws IOException { HttpsURLConnection connection = postTokenEndpoint(getRefreshTokenPostData(refreshToken)); final int responseCode = connection.getResponseCode(); if (responseCode == HttpsURLConnection.HTTP_OK) { return parseResponse(connection.getInputStream()); } else { Log.e(TAG, "error: " + responseCode); throw new IOException("Invalid response from server:" + responseCode); } }
@Override public void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportFragmentManager().addOnBackStackChangedListener(this); authPreferences = new AuthPreferences(this); oauth2Client = new OAuth2Client(authPreferences.getOAuth2ClientId()); fallbackAuthIntent = new Intent(this, OAuth2WebAuthActivity.class).setData(oauth2Client.requestUrl()); changeDefaultPackageIntent = new Intent(ACTION_CHANGE_DEFAULT).putExtra(EXTRA_PACKAGE_NAME, getPackageName()); preferenceTitles = new PreferenceTitles(getResources(), R.xml.preferences); preferences = new Preferences(this); if (bundle == null) { showFragment(new MainSettings(), null); } if (preferences.shouldShowAboutDialog()) { showDialog(ABOUT); } checkDefaultSmsApp(); requestPermissionsIfNeeded(); }
@Override protected OAuth2Token doInBackground(String... code) { if (code == null || code.length == 0 || TextUtils.isEmpty(code[0])) { Log.w(TAG, "invalid input: "+ Arrays.toString(code)); return null; } try { return oauth2Client.getToken(code[0]); } catch (IOException e) { Log.w(TAG, e); } return null; }
private String getUsernameFromContacts(OAuth2Token token) { try { HttpsURLConnection connection = (HttpsURLConnection) new URL(CONTACTS_URL).openConnection(); connection.addRequestProperty("Authorization", "Bearer "+token.accessToken); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { final InputStream inputStream = connection.getInputStream(); String email = extractEmail(inputStream); inputStream.close(); return email; } else { Log.w(TAG, String.format("unexpected server response: %d (%s)", connection.getResponseCode(), connection.getResponseMessage())); return null; } } catch (SAXException e) { Log.e(TAG, ERROR, e); return null; } catch (IOException e) { Log.e(TAG, ERROR, e); return null; } catch (ParserConfigurationException e) { Log.e(TAG, ERROR, e); return null; } }
@Test public void shouldUseOAuth2ClientWhenRefreshTokenIsPresent() throws Exception { when(authPreferences.getOauth2Token()).thenReturn("token"); when(authPreferences.getOauth2RefreshToken()).thenReturn("refresh"); when(authPreferences.getOauth2Username()).thenReturn("username"); when(oauth2Client.refreshToken("refresh")).thenReturn(new OAuth2Token("newToken", "type", null, 0, null)); refresher.refreshOAuth2Token(); verify(authPreferences).setOauth2Token("username", "newToken", "refresh"); }
BackupTask(@NonNull SmsBackupService service) { final Context context = service.getApplicationContext(); this.service = service; this.authPreferences = service.getAuthPreferences(); this.preferences = service.getPreferences(); this.fetcher = new BackupItemsFetcher( context.getContentResolver(), new BackupQueryBuilder(preferences.getDataTypePreferences())); PersonLookup personLookup = new PersonLookup(service.getContentResolver()); this.contactAccessor = new ContactAccessor(); this.converter = new MessageConverter(context, service.getPreferences(), authPreferences.getUserEmail(), personLookup, contactAccessor); if (preferences.isCallLogCalendarSyncEnabled()) { calendarSyncer = new CalendarSyncer( CalendarAccessor.Get.instance(service.getContentResolver()), preferences.getCallLogCalendarId(), personLookup, new CallFormatter(context.getResources()) ); } else { calendarSyncer = null; } this.tokenRefresher = new TokenRefresher(service, new OAuth2Client(authPreferences.getOAuth2ClientId()), authPreferences); }
@Test public void shouldUpdateRefreshTokenIfPresentInResponse() throws Exception { when(authPreferences.getOauth2Token()).thenReturn("token"); when(authPreferences.getOauth2RefreshToken()).thenReturn("refresh"); when(authPreferences.getOauth2Username()).thenReturn("username"); when(oauth2Client.refreshToken("refresh")).thenReturn(new OAuth2Token("newToken", "type", "newRefresh", 0, null)); refresher.refreshOAuth2Token(); verify(authPreferences).setOauth2Token("username", "newToken", "newRefresh"); } }
new TokenRefresher(service, new OAuth2Client(authPreferences.getOAuth2ClientId()), authPreferences)).execute(config);