@Test public void testNoArgConstructorDefaults() throws Exception { OAuthArguments args = new OAuthArguments(); assertEquals(UseToken.ALWAYS, args.getUseToken()); assertEquals("", args.getServiceName()); assertEquals("", args.getTokenName()); Assert.assertNull(args.getRequestToken()); Assert.assertNull(args.getRequestTokenSecret()); Assert.assertNull(args.getOrigClientState()); Assert.assertFalse(args.getBypassSpecCache()); Assert.assertFalse(args.getSignOwner()); Assert.assertFalse(args.getSignViewer()); }
@Test public void testGetAndSet() throws Exception { OAuthArguments args = new OAuthArguments(); args.setBypassSpecCache(true); Assert.assertTrue(args.getBypassSpecCache()); args.setOrigClientState("thestate"); assertEquals("thestate", args.getOrigClientState()); args.setRequestToken("rt"); assertEquals("rt", args.getRequestToken()); args.setRequestTokenSecret("rts"); assertEquals("rts", args.getRequestTokenSecret()); args.setServiceName("s"); assertEquals("s", args.getServiceName()); args.setSignOwner(true); Assert.assertTrue(args.getSignOwner()); args.setSignViewer(true); Assert.assertTrue(args.getSignViewer()); args.setUseToken(UseToken.IF_AVAILABLE); assertEquals(UseToken.IF_AVAILABLE, args.getUseToken()); args.setRequestOption("foo", "bar"); assertEquals("bar", args.getRequestOption("foo")); args.removeRequestOption("foo"); assertNull(args.getRequestOption("foo")); }
/** {@inheritDoc} */ private void addOAuthConfig(Map<String, String> xhrWrapperConfig, View view) { Map<String, String> oAuthConfig = Maps.newHashMapWithExpectedSize(3); try { OAuthArguments oAuthArguments = new OAuthArguments(view); oAuthConfig.put("authorization", "oauth"); oAuthConfig.put("oauthService", oAuthArguments.getServiceName()); if (!"".equals(oAuthArguments.getTokenName())) { oAuthConfig.put("oauthTokenName", oAuthArguments.getTokenName()); } xhrWrapperConfig.putAll(oAuthConfig); } catch (GadgetException e) { // Do not add any OAuth configuration if an exception was thrown } }
/** * Create arguments simulating authz=OAUTH. */ public OAuthArguments makeNonSocialOAuthArguments() { OAuthArguments params = new OAuthArguments(); params.setUseToken(UseToken.ALWAYS); params.setServiceName(serviceName); params.setSignOwner(false); params.setSignViewer(false); return params; }
/** * Track state (see gadgets.io.makeRequest handling of the oauthState and received callback * parameters. */ private OAuthArguments recallState() { OAuthArguments params = new OAuthArguments(baseArgs); params.setOrigClientState(oauthState); params.setReceivedCallbackUrl(receivedCallbackUrl); receivedCallbackUrl = null; return params; }
@Test public void copyCtorCopiesAllFields() { OAuthArguments oauthArguments = new OAuthArguments(); oauthArguments.setSignOwner(false); oauthArguments.setSignViewer(true); HttpRequest request = new HttpRequest(DEFAULT_URI) .setCacheTtl(100) assertEquals(request.getRewriteMimeType(), request2.getRewriteMimeType()); assertEquals(request.getSecurityToken(), request2.getSecurityToken()); assertEquals(request.getOAuthArguments().getSignOwner(), request2.getOAuthArguments().getSignOwner()); assertEquals(request.getOAuthArguments().getSignViewer(), request2.getOAuthArguments().getSignViewer()); assertEquals(AuthType.OAUTH, request.getAuthType()); assertFalse(request.getFollowRedirects());
try { tokenInfo = store.getTokenInfo(securityToken, consumerInfo, arguments.getServiceName(), arguments.getTokenName()); } catch (GadgetException e) { throw new OAuthRequestException(OAuthError.UNKNOWN_PROBLEM, accessorBuilder.setRequestToken(arguments.getRequestToken()); accessorBuilder.setTokenSecret(arguments.getRequestTokenSecret());
if (!realRequest.getOAuthArguments().getSignOwner() && !realRequest.getOAuthArguments().getSignViewer()) { return; if (owner != null && realRequest.getOAuthArguments().getSignOwner()) { params.add(new Parameter(OPENSOCIAL_OWNERID, owner)); if (viewer != null && realRequest.getOAuthArguments().getSignViewer()) { params.add(new Parameter(OPENSOCIAL_VIEWERID, viewer)); if (realRequest.getOAuthArguments().isProxiedContentRequest()) { params.add(new Parameter(OPENSOCIAL_PROXIED_CONTENT, "1"));
/** * Remove an access token for the given user/gadget/service/token name */ public void removeToken(SecurityToken securityToken, ConsumerInfo consumerInfo, OAuthArguments arguments, OAuthResponseParams responseParams) throws OAuthRequestException { try { store.removeToken(securityToken, consumerInfo, arguments.getServiceName(), arguments.getTokenName()); } catch (GadgetException e) { throw new OAuthRequestException(OAuthError.UNKNOWN_PROBLEM, "Unable to remove access token", e); } } }
/** * Calculate the invalidation mark for a request */ private String getInvalidationMark(HttpRequest request) { StringBuilder currentInvalidation = new StringBuilder(); Long ownerStamp = null; if (request.getOAuthArguments().getSignOwner()) { String ownerKey = getKey(request.getSecurityToken().getOwnerId(), request.getSecurityToken()); ownerStamp = invalidationEntries.getElement(ownerKey); } Long viewerStamp = null; if (request.getOAuthArguments().getSignViewer()) { if (ownerStamp != null && request.getSecurityToken().getOwnerId().equals( request.getSecurityToken().getViewerId())) { viewerStamp = ownerStamp; } else { String viewerKey = getKey(request.getSecurityToken().getViewerId(), request.getSecurityToken()); viewerStamp = invalidationEntries.getElement(viewerKey); } } if (ownerStamp != null) { currentInvalidation.append("o=").append(ownerStamp).append(';'); } if (viewerStamp != null) { currentInvalidation.append("v=").append(viewerStamp).append(';'); } return currentInvalidation.toString(); }
@Test public void testCopyConstructor_options() throws Exception { HttpServletRequest req = makeDummyRequest(); OAuthArguments args = new OAuthArguments(AuthType.OAUTH, req); args = new OAuthArguments(args); args.setRequestOption("foo", "bar"); args.setRequestOption("quux", "baz"); assertEquals("bar", args.getRequestOption("foo")); assertEquals("baz", args.getRequestOption("quux")); }
@Test public void testRequestTokenFromClient_noTokenInStorage() throws Exception { OAuthArguments arguments = new OAuthArguments(); arguments.setServiceName("testservice"); arguments.setUseToken(UseToken.ALWAYS); arguments.setRequestToken("preapproved"); arguments.setRequestTokenSecret("preapprovedsecret"); AccessorInfo info = store.getOAuthAccessor(privateToken, arguments, clientState, responseParams, fetcherConfig); assertNull(info.getAccessor().accessToken); assertEquals("preapproved", info.getAccessor().requestToken); assertEquals("preapprovedsecret", info.getAccessor().tokenSecret); }
@Test public void testGetOAuthAccessor_serviceNotFound() throws Exception { OAuthArguments arguments = new OAuthArguments(); arguments.setServiceName("no such service"); arguments.setUseToken(UseToken.ALWAYS); try { store.getOAuthAccessor(socialToken, arguments, clientState, responseParams, fetcherConfig); fail(); } catch (OAuthRequestException e) { assertEquals("BAD_OAUTH_CONFIGURATION", e.getError()); } }
if (arguments.programmaticConfig()) { provider = loadProgrammaticConfig(arguments, accessorBuilder, responseParams); } else if (arguments.mayUseToken()) { provider = lookupSpecInfo(securityToken, arguments, accessorBuilder, responseParams); } else { try { consumer = store.getConsumerKeyAndSecret( securityToken, arguments.getServiceName(), provider); accessorBuilder.setConsumer(consumer); } catch (GadgetException e) { if (arguments.mayUseToken() && securityToken.getViewerId() != null) { if ((fetcherConfig != null && fetcherConfig.isViewerAccessTokensEnabled()) || securityToken.getViewerId().equals(securityToken.getOwnerId())) {
@Test public void testGetOAuthAccessor_useToken_noOAuthInSpec() throws Exception { OAuthArguments arguments = new OAuthArguments(); arguments.setUseToken(UseToken.IF_AVAILABLE); try { store.getOAuthAccessor(socialToken, arguments, clientState, responseParams, fetcherConfig); fail(); } catch (OAuthRequestException e) { assertEquals("BAD_OAUTH_CONFIGURATION", e.getError()); } }
@Test public void testInitFromRequest_oauthDefaults() throws Exception { FakeHttpServletRequest req = new FakeHttpServletRequest(); OAuthArguments args = new OAuthArguments(AuthType.OAUTH, req); assertEquals(UseToken.ALWAYS, args.getUseToken()); }
public OAuthGadgetContext(SecurityToken securityToken, OAuthArguments arguments) { this.securityToken = securityToken; this.container = securityToken.getContainer(); this.appUrl = Uri.parse(securityToken.getAppUrl()); this.bypassSpecCache = arguments.getBypassSpecCache(); }
protected static String getServiceName(HttpRequest request) { if (request.getAuthType() != AuthType.NONE) { return request.getOAuthArguments().getServiceName(); } // Requests that don't use authentication can share the result. return null; }
@Test(expected = IllegalArgumentException.class) public void createKeyWithoutSecurityToken() throws Exception { RequestAuthenticationInfo authInfo = newMockAuthInfo( true /* isSignOwner */, false /* isSignViewer */, ImmutableMap.<String, String>of()); replay(authInfo); HttpRequest request = new HttpRequest(DEFAULT_URI) .setAuthType(AuthType.SIGNED) .setOAuthArguments(new OAuthArguments(authInfo)); cache.createKey(request); }
protected static String getOwnerId(HttpRequest request) { if (request.getAuthType() != AuthType.NONE && request.getOAuthArguments().getSignOwner()) { Preconditions.checkState(request.getSecurityToken() != null, "No Security Token set for request"); String ownerId = request.getSecurityToken().getOwnerId(); return Objects.firstNonNull(ownerId, ""); } // Requests that don't use authentication can share the result. return null; }