private Bundle getAuthToken(Account account) throws AccountsException, IOException { if (Build.VERSION.SDK_INT >= 14) { return getAuthTokenApi14(account); } else { return getAuthTokenPreApi14(account); } }
@Override public String toString() { return getTokenForLogging(); }
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); } }
@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"); }
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); } }
@Test public void testTokenForLogging() throws Exception { OAuth2Token token = new OAuth2Token("secret", "type", "secret", 100, "Test"); assertThat(token.getTokenForLogging()).doesNotContain("secret"); assertThat(token.toString()).doesNotContain("secret"); } }
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 refreshOAuth2Token() throws TokenRefreshException{ final String token = authPreferences.getOauth2Token(); final String refreshToken = authPreferences.getOauth2RefreshToken(); final String name = authPreferences.getOauth2Username(); if (isEmpty(token)) { throw new TokenRefreshException("no current token set"); } if (!isEmpty(refreshToken)) { // user authenticated using webflow refreshUsingOAuth2Client(name, refreshToken); } else { refreshUsingAccountManager(token, name); } }
private OAuth2Token parseResponse(InputStream inputStream) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buffer = new byte[8192]; int n; while ((n = inputStream.read(buffer)) != -1) { bos.write(buffer, 0, n); } inputStream.close(); return OAuth2Token.fromJSON(bos.toString("UTF-8")); }
private BackupState handleAuthError(BackupConfig config, XOAuth2AuthenticationFailedException e) { if (e.getStatus() == 400) { appLogDebug("need to perform xoauth2 token refresh"); if (config.currentTry < 1) { try { tokenRefresher.refreshOAuth2Token(); // we got a new token, let's handleAuthError one more time - we need to pass in a new store object // since the auth params on it are immutable appLogDebug("token refreshed, retrying"); return fetchAndBackupItems(config.retryWithStore(service.getBackupImapStore())); } catch (MessagingException ignored) { Log.w(TAG, ignored); } catch (TokenRefreshException refreshException) { appLogDebug("error refreshing token: "+refreshException+", cause="+refreshException.getCause()); } } else { appLogDebug("no new token obtained, giving up"); } } else { appLogDebug("unexpected xoauth status code " + e.getStatus()); } return transition(ERROR, e); }
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); } }
private String extractEmail(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException { final XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); final FeedHandler feedHandler = new FeedHandler(); xmlReader.setContentHandler(feedHandler); xmlReader.parse(new InputSource(inputStream)); return feedHandler.getEmail(); }
private static OAuth2Token fromJSON(JSONObject object) throws IOException { try { return new OAuth2Token( object.getString("access_token"), object.optString("token_type", null), object.optString("refresh_token", null), object.optInt("expires_in", -1), null); } catch (JSONException e) { Log.w(TAG, "JSON parse error", e); throw new IOException("parse error"); } }
@Before public void before() { initMocks(this); refresher = new TokenRefresher(accountManager, oauth2Client, authPreferences); }
@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; }
@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"); } }
public static OAuth2Token fromJSON(String string) throws IOException { try { Object value = new JSONTokener(string).nextValue(); if (value instanceof JSONObject) { return fromJSON((JSONObject) value); } else { throw new IOException("Invalid JSON data: "+value); } } catch (JSONException e) { Log.w(TAG, "JSON parse error", e); throw new IOException("Error parsing data: "+e.getMessage()); } }
@Test public void testFromJSONWithMissingFields() throws Exception { final String response = "{\n" + " \"access_token\":\"1/fFAGRNJru1FTz70BzhT3Zg\"\n" + "}"; final OAuth2Token token = OAuth2Token.fromJSON(response); assertThat(token.accessToken).isEqualTo("1/fFAGRNJru1FTz70BzhT3Zg"); assertThat(token.tokenType).isNull(); assertThat(token.refreshToken).isNull(); assertThat(token.expiresIn).isEqualTo(-1); }
@Test public void testFromJSONWithoutRefreshToken() throws Exception { final String response = "{\n" + " \"access_token\":\"1/fFAGRNJru1FTz70BzhT3Zg\",\n" + " \"expires_in\":3920,\n" + " \"token_type\":\"Bearer\"\n" + "}"; final OAuth2Token token = OAuth2Token.fromJSON(response); assertThat(token.accessToken).isEqualTo("1/fFAGRNJru1FTz70BzhT3Zg"); assertThat(token.tokenType).isEqualTo("Bearer"); assertThat(token.refreshToken).isNull(); assertThat(token.expiresIn).isEqualTo(3920); }
@Test public void testFromJSON() throws Exception { final String response = "{\n" + " \"access_token\":\"1/fFAGRNJru1FTz70BzhT3Zg\",\n" + " \"expires_in\":3920,\n" + " \"token_type\":\"Bearer\",\n" + " \"refresh_token\":\"1/xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI\"\n" + "}"; final OAuth2Token token = OAuth2Token.fromJSON(response); assertThat(token.accessToken).isEqualTo("1/fFAGRNJru1FTz70BzhT3Zg"); assertThat(token.tokenType).isEqualTo("Bearer"); assertThat(token.refreshToken).isEqualTo("1/xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI"); assertThat(token.expiresIn).isEqualTo(3920); }