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); } }
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")); }
@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 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"); } }
@Override public String toString() { return getTokenForLogging(); }
@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 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 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"); } }
@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); }