Search in sources :

Example 1 with SharedConsumerSecretImpl

use of org.springframework.security.oauth.common.signature.SharedConsumerSecretImpl in project spring-security-oauth by spring-projects.

the class GoogleOAuthTests method testGetRequestToken.

/**
	 * tests getting a request token.
	 */
@Test
public void testGetRequestToken() throws Exception {
    CoreOAuthConsumerSupport support = new CoreOAuthConsumerSupport();
    support.setStreamHandlerFactory(new DefaultOAuthURLStreamHandlerFactory());
    InMemoryProtectedResourceDetailsService service = new InMemoryProtectedResourceDetailsService();
    HashMap<String, ProtectedResourceDetails> detailsStore = new HashMap<String, ProtectedResourceDetails>();
    BaseProtectedResourceDetails googleDetails = new BaseProtectedResourceDetails();
    googleDetails.setRequestTokenURL("https://www.google.com/accounts/OAuthGetRequestToken");
    googleDetails.setAccessTokenURL("https://www.google.com/accounts/OAuthAuthorizeToken");
    googleDetails.setConsumerKey("anonymous");
    googleDetails.setSharedSecret(new SharedConsumerSecretImpl("anonymous"));
    googleDetails.setId("google");
    googleDetails.setUse10a(true);
    googleDetails.setSignatureMethod(HMAC_SHA1SignatureMethod.SIGNATURE_NAME);
    googleDetails.setRequestTokenHttpMethod("GET");
    HashMap<String, String> additional = new HashMap<String, String>();
    additional.put("scope", "http://picasaweb.google.com/data");
    googleDetails.setAdditionalParameters(additional);
    detailsStore.put(googleDetails.getId(), googleDetails);
    service.setResourceDetailsStore(detailsStore);
    support.setProtectedResourceDetailsService(service);
// uncomment to see a request to google.
// see http://code.google.com/apis/accounts/docs/OAuth_ref.html
// and http://jira.codehaus.org/browse/OAUTHSS-37
// OAuthConsumerToken token = support.getUnauthorizedRequestToken("google", "urn:mycallback");
// System.out.println(token.getValue());
// System.out.println(token.getSecret());
}
Also used : SharedConsumerSecretImpl(org.springframework.security.oauth.common.signature.SharedConsumerSecretImpl) DefaultOAuthURLStreamHandlerFactory(org.springframework.security.oauth.consumer.net.DefaultOAuthURLStreamHandlerFactory) HashMap(java.util.HashMap) CoreOAuthConsumerSupport(org.springframework.security.oauth.consumer.client.CoreOAuthConsumerSupport) Test(org.junit.Test)

Example 2 with SharedConsumerSecretImpl

use of org.springframework.security.oauth.common.signature.SharedConsumerSecretImpl in project spring-security-oauth by spring-projects.

the class CoreOAuthConsumerSupportTests method testLoadOAuthParameters.

/**
	 * loadOAuthParameters
	 */
@Test
public void testLoadOAuthParameters() throws Exception {
    URL url = new URL("https://myhost.com/somepath?with=some&query=params&too");
    CoreOAuthConsumerSupport support = new CoreOAuthConsumerSupport() {

        @Override
        protected String getSignatureBaseString(Map<String, Set<CharSequence>> oauthParams, URL requestURL, String httpMethod) {
            return "MYSIGBASESTRING";
        }
    };
    OAuthSignatureMethodFactory sigFactory = mock(OAuthSignatureMethodFactory.class);
    support.setSignatureFactory(sigFactory);
    OAuthConsumerToken token = new OAuthConsumerToken();
    OAuthSignatureMethod sigMethod = mock(OAuthSignatureMethod.class);
    when(details.getConsumerKey()).thenReturn("my-consumer-key");
    when(details.getSignatureMethod()).thenReturn(HMAC_SHA1SignatureMethod.SIGNATURE_NAME);
    when(details.getSignatureMethod()).thenReturn(HMAC_SHA1SignatureMethod.SIGNATURE_NAME);
    SharedConsumerSecret secret = new SharedConsumerSecretImpl("shh!!!");
    when(details.getSharedSecret()).thenReturn(secret);
    when(sigFactory.getSignatureMethod(HMAC_SHA1SignatureMethod.SIGNATURE_NAME, secret, null)).thenReturn(sigMethod);
    when(sigMethod.sign("MYSIGBASESTRING")).thenReturn("MYSIGNATURE");
    Map<String, Set<CharSequence>> params = support.loadOAuthParameters(details, url, token, "POST", null);
    assertEquals("some", params.remove("with").iterator().next().toString());
    assertEquals("params", params.remove("query").iterator().next().toString());
    assertTrue(params.containsKey("too"));
    assertTrue(params.remove("too").isEmpty());
    assertNull(params.remove(OAuthConsumerParameter.oauth_token.toString()));
    assertNotNull(params.remove(OAuthConsumerParameter.oauth_nonce.toString()).iterator().next());
    assertEquals("my-consumer-key", params.remove(OAuthConsumerParameter.oauth_consumer_key.toString()).iterator().next());
    assertEquals("MYSIGNATURE", params.remove(OAuthConsumerParameter.oauth_signature.toString()).iterator().next());
    assertEquals("1.0", params.remove(OAuthConsumerParameter.oauth_version.toString()).iterator().next());
    assertEquals(HMAC_SHA1SignatureMethod.SIGNATURE_NAME, params.remove(OAuthConsumerParameter.oauth_signature_method.toString()).iterator().next());
    assertTrue(Long.parseLong(params.remove(OAuthConsumerParameter.oauth_timestamp.toString()).iterator().next().toString()) <= (System.currentTimeMillis() / 1000));
    assertTrue(params.isEmpty());
}
Also used : SharedConsumerSecretImpl(org.springframework.security.oauth.common.signature.SharedConsumerSecretImpl) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) SharedConsumerSecret(org.springframework.security.oauth.common.signature.SharedConsumerSecret) OAuthSignatureMethod(org.springframework.security.oauth.common.signature.OAuthSignatureMethod) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) URL(java.net.URL) OAuthSignatureMethodFactory(org.springframework.security.oauth.common.signature.OAuthSignatureMethodFactory) OAuthConsumerToken(org.springframework.security.oauth.consumer.OAuthConsumerToken) Test(org.junit.Test)

Example 3 with SharedConsumerSecretImpl

use of org.springframework.security.oauth.common.signature.SharedConsumerSecretImpl in project spring-security-oauth by spring-projects.

the class ConsumerDetailsFactoryBean method getObject.

public ConsumerDetails getObject() throws Exception {
    if ("rsa-cert".equals(typeOfSecret)) {
        try {
            Certificate cert = CertificateFactory.getInstance("X.509").generateCertificate(resourceLoader.getResource(secret).getInputStream());
            consumer.setSignatureSecret(new RSAKeySecret(cert.getPublicKey()));
        } catch (IOException e) {
            throw new BeanCreationException("RSA certificate not found at " + secret + ".", e);
        } catch (CertificateException e) {
            throw new BeanCreationException("Invalid RSA certificate at " + secret + ".", e);
        } catch (NullPointerException e) {
            throw new BeanCreationException("Could not load RSA certificate at " + secret + ".", e);
        }
    } else {
        consumer.setSignatureSecret(new SharedConsumerSecretImpl(secret));
    }
    return consumer;
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) SharedConsumerSecretImpl(org.springframework.security.oauth.common.signature.SharedConsumerSecretImpl) RSAKeySecret(org.springframework.security.oauth.common.signature.RSAKeySecret) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) Certificate(java.security.cert.Certificate)

Aggregations

SharedConsumerSecretImpl (org.springframework.security.oauth.common.signature.SharedConsumerSecretImpl)3 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 URL (java.net.URL)1 Certificate (java.security.cert.Certificate)1 CertificateException (java.security.cert.CertificateException)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeMap (java.util.TreeMap)1 BeanCreationException (org.springframework.beans.factory.BeanCreationException)1 OAuthSignatureMethod (org.springframework.security.oauth.common.signature.OAuthSignatureMethod)1 OAuthSignatureMethodFactory (org.springframework.security.oauth.common.signature.OAuthSignatureMethodFactory)1 RSAKeySecret (org.springframework.security.oauth.common.signature.RSAKeySecret)1 SharedConsumerSecret (org.springframework.security.oauth.common.signature.SharedConsumerSecret)1 OAuthConsumerToken (org.springframework.security.oauth.consumer.OAuthConsumerToken)1 CoreOAuthConsumerSupport (org.springframework.security.oauth.consumer.client.CoreOAuthConsumerSupport)1 DefaultOAuthURLStreamHandlerFactory (org.springframework.security.oauth.consumer.net.DefaultOAuthURLStreamHandlerFactory)1