@Test public void testValuesSet() throws Exception { OAuthClientState state = new OAuthClientState(crypter); state.setAccessToken("atoken"); state.setAccessTokenSecret("atokensecret"); state.setOwner("owner"); state.setRequestToken("reqtoken"); state.setRequestTokenSecret("reqtokensecret"); String encrypted = state.getEncryptedState(); state = new OAuthClientState(crypter, encrypted); assertEquals("atoken", state.getAccessToken()); assertEquals("atokensecret", state.getAccessTokenSecret()); assertEquals("owner", state.getOwner()); assertEquals("reqtoken", state.getRequestToken()); assertEquals("reqtokensecret", state.getRequestTokenSecret()); }
private void assertEmpty(OAuthClientState state) { assertTrue(state.isEmpty()); assertNull(state.getRequestToken()); assertNull(state.getRequestTokenSecret()); assertNull(state.getAccessToken()); assertNull(state.getAccessTokenSecret()); assertNull(state.getOwner()); }
@Test public void testNullValue() throws Exception { OAuthClientState state = new OAuthClientState(crypter); state.setRequestToken("reqtoken"); state.setRequestToken(null); state.setOwner("owner"); String encrypted = state.getEncryptedState(); state = new OAuthClientState(crypter, encrypted); assertNull(state.getRequestToken()); assertEquals("owner", state.getOwner()); } }
/** * Builds the data we'll cache on the client while we make requests. */ private void buildClientAccessState() { OAuthAccessor accessor = accessorInfo.getAccessor(); responseParams.getNewClientState().setAccessToken(accessor.accessToken); responseParams.getNewClientState().setAccessTokenSecret(accessor.tokenSecret); responseParams.getNewClientState().setOwner(realRequest.getSecurityToken().getOwnerId()); responseParams.getNewClientState().setSessionHandle(accessorInfo.getSessionHandle()); responseParams.getNewClientState().setTokenExpireMillis(accessorInfo.getTokenExpireMillis()); }
@Test public void testExpired() throws Exception { OAuthClientState state = new OAuthClientState(crypter); timeSource.incrementSeconds(-1 * (3600 + 180 + 1)); // expiry time + skew. state.setTimeSource(timeSource); state.setRequestToken("reqtoken"); String encrypted = state.getEncryptedState(); state = new OAuthClientState(crypter, encrypted); assertNull(state.getRequestToken()); }
OAuthResponseParams responseParams) throws OAuthRequestException { if (clientState.getRequestToken() != null) { accessorBuilder.setRequestToken(clientState.getRequestToken()); accessorBuilder.setTokenSecret(clientState.getRequestTokenSecret()); } else if (clientState.getAccessToken() != null) { accessorBuilder.setAccessToken(clientState.getAccessToken()); accessorBuilder.setTokenSecret(clientState.getAccessTokenSecret()); accessorBuilder.setSessionHandle(clientState.getSessionHandle()); accessorBuilder.setTokenExpireMillis(clientState.getTokenExpireMillis()); } else {
/** * Create response parameters. */ public OAuthResponseParams(SecurityToken securityToken, HttpRequest originalRequest, BlobCrypter stateCrypter) { this.securityToken = securityToken; this.originalRequest = originalRequest; newClientState = new OAuthClientState(stateCrypter); }
@Test public void testEncryptEmpty() throws Exception { OAuthClientState state = new OAuthClientState(crypter); assertEmpty(state); String encrypted = state.getEncryptedState(); state = new OAuthClientState(crypter, encrypted); assertEmpty(state); }
@Test public void testSetAndGet() { params.getNewClientState().setAccessToken("access"); params.setAznUrl("aznurl"); assertFalse(params.sendTraceToClient()); params.setSendTraceToClient(true); assertTrue(params.sendTraceToClient()); assertEquals("access", params.getNewClientState().getAccessToken()); assertEquals("aznurl", params.getAznUrl()); }
/** * Update a response with additional data to be returned to the application. */ public void addToResponse(HttpResponseBuilder response, OAuthRequestException e) { if (!newClientState.isEmpty()) { try { response.setMetadata(CLIENT_STATE, newClientState.getEncryptedState()); } catch (BlobCrypterException cryptException) { // Configuration error somewhere, this should never happen. throw new RuntimeException(cryptException); } } if (aznUrl != null) { response.setMetadata(APPROVAL_URL, aznUrl); } if (e != null || sendTraceToClient) { StringBuilder verboseError = new StringBuilder(); if (e != null) { response.setMetadata(ERROR_CODE, e.getError()); verboseError.append(e.getErrorText()); } if (sendTraceToClient) { verboseError.append('\n'); verboseError.append(getRequestTrace()); } response.setMetadata(ERROR_TEXT, verboseError.toString()); } }
@Test public void testAddParams() { params.getNewClientState().setAccessToken("access"); params.setAznUrl("aznurl"); OAuthRequestException e = new OAuthRequestException(OAuthError.BAD_OAUTH_CONFIGURATION, "whoa there cowboy"); HttpResponseBuilder responseBuilder = new HttpResponseBuilder(); params.addToResponse(responseBuilder, e); HttpResponse response = responseBuilder.create(); assertEquals("BAD_OAUTH_CONFIGURATION", response.getMetadata().get("oauthError")); String errorText = response.getMetadata().get("oauthErrorText"); checkStringContains("error text returned", errorText, "whoa there cowboy"); assertEquals("aznurl", response.getMetadata().get("oauthApprovalUrl")); assertNotNull(response.getMetadata().get("oauthState")); assertTrue(response.getMetadata().get("oauthState").length() > 10); }
/** * Make sure the user is authorized to approve access tokens. At the moment * we restrict this to page owner's viewing their own pages. */ private void checkCanApprove() throws OAuthRequestException { String pageOwner = realRequest.getSecurityToken().getOwnerId(); String pageViewer = realRequest.getSecurityToken().getViewerId(); String stateOwner = clientState.getOwner(); if (pageOwner == null || pageViewer == null) { throw new OAuthRequestException(OAuthError.UNAUTHENTICATED); } if (!fetcherConfig.isViewerAccessTokensEnabled() && !pageOwner.equals(pageViewer)) { throw new OAuthRequestException(OAuthError.NOT_OWNER); } if (stateOwner != null && !stateOwner.equals(pageViewer)) { throw new OAuthRequestException(OAuthError.UNKNOWN_PROBLEM, "Client state belongs to a different person " + "(state owner=" + stateOwner + ", pageViewer=" + pageViewer + ')'); } }
/** * Builds the data we'll cache on the client while we make requests. */ private void buildClientAccessState() { OAuthAccessor accessor = accessorInfo.getAccessor(); responseParams.getNewClientState().setAccessToken(accessor.accessToken); responseParams.getNewClientState().setAccessTokenSecret(accessor.tokenSecret); responseParams.getNewClientState().setOwner(realRequest.getSecurityToken().getOwnerId()); responseParams.getNewClientState().setSessionHandle(accessorInfo.getSessionHandle()); responseParams.getNewClientState().setTokenExpireMillis(accessorInfo.getTokenExpireMillis()); }
@Test public void testNullValue() throws Exception { OAuthClientState state = new OAuthClientState(crypter); state.setRequestToken("reqtoken"); state.setRequestToken(null); state.setOwner("owner"); String encrypted = state.getEncryptedState(); state = new OAuthClientState(crypter, encrypted); assertNull(state.getRequestToken()); assertEquals("owner", state.getOwner()); } }
OAuthResponseParams responseParams) throws OAuthRequestException { if (clientState.getRequestToken() != null) { accessorBuilder.setRequestToken(clientState.getRequestToken()); accessorBuilder.setTokenSecret(clientState.getRequestTokenSecret()); } else if (clientState.getAccessToken() != null) { accessorBuilder.setAccessToken(clientState.getAccessToken()); accessorBuilder.setTokenSecret(clientState.getAccessTokenSecret()); accessorBuilder.setSessionHandle(clientState.getSessionHandle()); accessorBuilder.setTokenExpireMillis(clientState.getTokenExpireMillis()); } else {
@Test public void testExpired() throws Exception { OAuthClientState state = new OAuthClientState(crypter); timeSource.incrementSeconds(-1 * (3600 + 180 + 1)); // expiry time + skew. state.setTimeSource(timeSource); state.setRequestToken("reqtoken"); String encrypted = state.getEncryptedState(); state = new OAuthClientState(crypter, encrypted); assertNull(state.getRequestToken()); }
/** * Create response parameters. */ public OAuthResponseParams(SecurityToken securityToken, HttpRequest originalRequest, BlobCrypter stateCrypter) { this.securityToken = securityToken; this.originalRequest = originalRequest; newClientState = new OAuthClientState(stateCrypter); }
@Test public void testEncryptEmpty() throws Exception { OAuthClientState state = new OAuthClientState(crypter); assertEmpty(state); String encrypted = state.getEncryptedState(); state = new OAuthClientState(crypter, encrypted); assertEmpty(state); }
@Test public void testSetAndGet() { params.getNewClientState().setAccessToken("access"); params.setAznUrl("aznurl"); assertFalse(params.sendTraceToClient()); params.setSendTraceToClient(true); assertTrue(params.sendTraceToClient()); assertEquals("access", params.getNewClientState().getAccessToken()); assertEquals("aznurl", params.getAznUrl()); }
/** * Update a response with additional data to be returned to the application. */ public void addToResponse(HttpResponseBuilder response, OAuthRequestException e) { if (!newClientState.isEmpty()) { try { response.setMetadata(CLIENT_STATE, newClientState.getEncryptedState()); } catch (BlobCrypterException cryptException) { // Configuration error somewhere, this should never happen. throw new RuntimeException(cryptException); } } if (aznUrl != null) { response.setMetadata(APPROVAL_URL, aznUrl); } if (e != null || sendTraceToClient) { StringBuilder verboseError = new StringBuilder(); if (e != null) { response.setMetadata(ERROR_CODE, e.getError()); verboseError.append(e.getErrorText()); } if (sendTraceToClient) { verboseError.append('\n'); verboseError.append(getRequestTrace()); } response.setMetadata(ERROR_TEXT, verboseError.toString()); } }