Permalink
Browse files

SOCIAL-352: Added ability to set ConnectionSignUp in XML and JavaConf…

…ig configuration support.
  • Loading branch information...
1 parent 306e549 commit 9a6de94236a9ee0d1a9b752441ee2eb21a050253 Craig Walls committed Feb 19, 2013
View
1 spring-social-config/infinitest.filters
@@ -0,0 +1 @@
+org.springframework.social.config.xml.SocialConfigNamespaceTest
View
9 ...ain/java/org/springframework/social/config/annotation/EnableJdbcConnectionRepository.java
@@ -23,6 +23,7 @@
import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;
+import org.springframework.social.connect.ConnectionSignUp;
import org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository;
/**
@@ -73,4 +74,12 @@
*/
String userIdSourceRef() default "userIdSource";
+
+ /**
+ * Reference to {@link ConnectionSignUp} bean to execute to create a new local user profile in the event no user id could be mapped to a connection.
+ * Allows for implicitly creating a user profile from connection data during a provider sign-in attempt.
+ * Defaults to null, indicating explicit sign-up will be required to complete the provider sign-in attempt.
+ */
+ String connectionSignUpRef() default "";
+
}
View
3 .../java/org/springframework/social/config/annotation/JdbcConnectionRepositoryRegistrar.java
@@ -41,7 +41,8 @@ public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanD
String dataSourceRef = attributes.getString("dataSourceRef");
String encryptorRef = attributes.getString("encryptorRef");
String userIdSourceRef = attributes.getString("userIdSourceRef");
- registerJdbcConnectionRepositoryBeans(registry, connectionRepositoryId, usersConnectionRepositoryId, connectionFactoryLocatorRef, dataSourceRef, encryptorRef, userIdSourceRef);
+ String connectionSignUpRef = attributes.getString("connectionSignUpRef");
+ registerJdbcConnectionRepositoryBeans(registry, connectionRepositoryId, usersConnectionRepositoryId, connectionFactoryLocatorRef, dataSourceRef, encryptorRef, userIdSourceRef, connectionSignUpRef);
}
}
View
16 ...java/org/springframework/social/config/support/JdbcConnectionRepositoryConfigSupport.java
@@ -34,24 +34,26 @@
private final static Log logger = LogFactory.getLog(JdbcConnectionRepositoryConfigSupport.class);
public BeanDefinition registerJdbcConnectionRepositoryBeans(BeanDefinitionRegistry registry, String connectionRepositoryId, String usersConnectionRepositoryId,
- String connectionFactoryLocatorRef, String dataSourceRef, String encryptorRef, String userIdSourceRef) {
- registerUsersConnectionRepositoryBeanDefinition(registry, usersConnectionRepositoryId, connectionFactoryLocatorRef, dataSourceRef, encryptorRef);
+ String connectionFactoryLocatorRef, String dataSourceRef, String encryptorRef, String userIdSourceRef, String connectionSignUpRef) {
+ registerUsersConnectionRepositoryBeanDefinition(registry, usersConnectionRepositoryId, connectionFactoryLocatorRef, dataSourceRef, encryptorRef, connectionSignUpRef);
registerUserIdBeanDefinition(registry, userIdSourceRef);
return registerConnectionRepository(registry, usersConnectionRepositoryId, connectionRepositoryId);
}
private BeanDefinition registerUsersConnectionRepositoryBeanDefinition(BeanDefinitionRegistry registry, String usersConnectionRepositoryId,
- String connectionFactoryLocatorRef, String dataSourceRef, String encryptorRef) {
+ String connectionFactoryLocatorRef, String dataSourceRef, String encryptorRef, String connectionSignUpRef) {
if (logger.isDebugEnabled()) {
logger.debug("Registering JdbcUsersConnectionRepository bean");
}
-
- BeanDefinition usersConnectionRepositoryBD = BeanDefinitionBuilder.genericBeanDefinition(JdbcUsersConnectionRepository.class)
+ BeanDefinitionBuilder usersConnectionRepositoryBeanBuilder = BeanDefinitionBuilder.genericBeanDefinition(JdbcUsersConnectionRepository.class)
.addConstructorArgReference(dataSourceRef)
.addConstructorArgReference(connectionFactoryLocatorRef)
- .addConstructorArgReference(encryptorRef)
- .getBeanDefinition();
+ .addConstructorArgReference(encryptorRef);
+ if (connectionSignUpRef != null && connectionSignUpRef.length() > 0) {
+ usersConnectionRepositoryBeanBuilder.addPropertyReference("connectionSignUp", connectionSignUpRef);
+ }
+ BeanDefinition usersConnectionRepositoryBD = usersConnectionRepositoryBeanBuilder.getBeanDefinition();
BeanDefinition scopedProxyBean = decorateWithScopedProxy(usersConnectionRepositoryId, usersConnectionRepositoryBD, registry);
registry.registerBeanDefinition(usersConnectionRepositoryId, scopedProxyBean);
return scopedProxyBean;
View
3 ...a/org/springframework/social/config/xml/JdbcConnectionRepositoryBeanDefinitionParser.java
@@ -37,7 +37,8 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {
String dataSourceRef = element.getAttribute("data-source-ref");
String encryptorRef = element.getAttribute("encryptor-ref");
String userIdSourceRef = element.getAttribute("user-id-source-ref");
- return registerJdbcConnectionRepositoryBeans(parserContext.getRegistry(), connectionRepositoryId, usersConnectionRepositoryId, connectionFactoryLocatorRef, dataSourceRef, encryptorRef, userIdSourceRef);
+ String connectionSignUpRef = element.getAttribute("connection-signup-ref");
+ return registerJdbcConnectionRepositoryBeans(parserContext.getRegistry(), connectionRepositoryId, usersConnectionRepositoryId, connectionFactoryLocatorRef, dataSourceRef, encryptorRef, userIdSourceRef, connectionSignUpRef);
}
}
View
1 ...ial-config/src/main/resources/org/springframework/social/config/xml/spring-social-1.1.xsd
@@ -11,6 +11,7 @@
<xs:attribute name="data-source-ref" use="optional" type="xs:string" default="dataSource" />
<xs:attribute name="encryptor-ref" use="optional" type="xs:string" default="textEncryptor" />
<xs:attribute name="user-id-source-ref" use="optional" type="xs:string" default="userIdSource" />
+ <xs:attribute name="connection-signup-ref" use="optional" type="xs:string" />
</xs:complexType>
</xs:element>
View
96 spring-social-config/src/test/java/org/springframework/social/config/DummyConnection.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright 2013 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+ package org.springframework.social.config;
+
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+import org.springframework.social.connect.Connection;
+import org.springframework.social.connect.ConnectionData;
+import org.springframework.social.connect.ConnectionKey;
+import org.springframework.social.connect.UserProfile;
+import org.springframework.social.connect.UserProfileBuilder;
+
+public class DummyConnection<T> implements Connection<T> {
+
+ private final ConnectionKey _key;
+ private final T _api;
+
+ public static DummyConnection<Object> dummy(String provider, String user) {
+ return new DummyConnection<Object>(provider, user, new Object());
+ }
+
+ public DummyConnection(String provider, String user, T api) {
+ _key = new ConnectionKey(provider, user);
+ _api = api;
+ }
+
+ public ConnectionKey getKey() {
+ return _key;
+ }
+
+ public String getDisplayName() {
+ return _key.getProviderUserId();
+ }
+
+ public String getProfileUrl() {
+ return "http://www.example.com/" + _key.getProviderUserId();
+ }
+
+ public String getImageUrl() {
+ return "http://www.example.com/img/" + _key.getProviderUserId() + ".jpg";
+ }
+
+ public void sync() {
+ }
+
+ public boolean test() {
+ return true;
+ }
+
+ public boolean hasExpired() {
+ return true;
+ }
+
+ public void refresh() {
+ }
+
+ public UserProfile fetchUserProfile() {
+ return new UserProfileBuilder().setEmail(_key.getProviderUserId() + "@example.com").build();
+ }
+
+ public void updateStatus(String message) {
+ }
+
+ public T getApi() {
+ return _api;
+ }
+
+ public ConnectionData createData() {
+ return new ConnectionData(_key.getProviderId(), _key.getProviderUserId(), getDisplayName(),
+ getProfileUrl(), getImageUrl(), "access_token", "secret", "refresh_token", System.currentTimeMillis() + 10000);
+ }
+
+ public static Answer<DummyConnection<Object>> answer() {
+ return new Answer<DummyConnection<Object>>() {
+
+ public DummyConnection<Object> answer(InvocationOnMock invocation) throws Throwable {
+ ConnectionData data = (ConnectionData) invocation.getArguments()[0];
+ return dummy(data.getProviderId(), data.getProviderUserId());
+ }
+ };
+ }
+
+}
View
27 ...g-social-config/src/test/java/org/springframework/social/config/FakeConnectionSignUp.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2013 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.social.config;
+
+import org.springframework.social.connect.Connection;
+import org.springframework.social.connect.ConnectionSignUp;
+
+public class FakeConnectionSignUp implements ConnectionSignUp {
+
+ public String execute(Connection<?> connection) {
+ return "fakeuser";
+ }
+
+}
View
7 ...ocial-config/src/test/java/org/springframework/social/config/annotation/SocialConfig.java
@@ -25,12 +25,14 @@
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import org.springframework.social.UserIdSource;
+import org.springframework.social.config.FakeConnectionSignUp;
import org.springframework.social.config.SimpleUserIdSource;
+import org.springframework.social.connect.ConnectionSignUp;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:/org/springframework/social/config/fake.properties")
-@EnableJdbcConnectionRepository
+@EnableJdbcConnectionRepository(connectionSignUpRef="connectionSignUp")
@EnableFake(appId="${fake.appId}", appSecret="${fake.appSecret}")
public class SocialConfig {
@@ -57,4 +59,7 @@ public UserIdSource userIdSource() {
return new SimpleUserIdSource();
}
+ @Bean ConnectionSignUp connectionSignUp() {
+ return new FakeConnectionSignUp();
+ }
}
View
14 ...rc/test/java/org/springframework/social/config/annotation/SocialConfigAnnotationTest.java
@@ -17,6 +17,8 @@
import static org.junit.Assert.*;
+import java.util.List;
+
import javax.inject.Inject;
import org.junit.Before;
@@ -25,8 +27,10 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.social.config.DummyConnection;
import org.springframework.social.config.Fake;
import org.springframework.social.config.FakeConnectionFactory;
+import org.springframework.social.config.FakeTemplate;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionData;
import org.springframework.social.connect.ConnectionFactory;
@@ -76,7 +80,15 @@ public void jdbcConnectionRepository() {
assertNotNull(context.getBean("usersConnectionRepository", UsersConnectionRepository.class));
assertNotNull(context.getBean("connectionRepository", ConnectionRepository.class));
}
-
+
+ @Test
+ public void jdbcConnectionRepository_connectionSignUp() {
+ UsersConnectionRepository repository = context.getBean("usersConnectionRepository", UsersConnectionRepository.class);
+ Connection<Fake> connection = new DummyConnection<Fake>("fake", "fakeuser", new FakeTemplate());
+ List<String> users = repository.findUserIdsWithConnection(connection);
+ assertEquals(1, users.size());
+ }
+
@Test
public void jdbcConnectionRepository_addAndRemoveAConnection() {
ConnectionFactoryLocator cfl = context.getBean(ConnectionFactoryLocator.class);
View
16 ...config/src/test/java/org/springframework/social/config/xml/SocialConfigNamespaceTest.java
@@ -17,17 +17,20 @@
import static org.junit.Assert.*;
+import java.util.List;
+
import javax.inject.Inject;
import org.junit.Before;
-import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.social.config.DummyConnection;
import org.springframework.social.config.Fake;
import org.springframework.social.config.FakeConnectionFactory;
+import org.springframework.social.config.FakeTemplate;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionData;
import org.springframework.social.connect.ConnectionFactory;
@@ -42,7 +45,6 @@
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
-@Ignore
public class SocialConfigNamespaceTest {
@Inject
@@ -78,7 +80,15 @@ public void jdbcConnectionRepository() {
assertNotNull(context.getBean("usersConnectionRepository", UsersConnectionRepository.class));
assertNotNull(context.getBean("connectionRepository", ConnectionRepository.class));
}
-
+
+ @Test
+ public void jdbcConnectionRepository_connectionSignUp() {
+ UsersConnectionRepository repository = context.getBean("usersConnectionRepository", UsersConnectionRepository.class);
+ Connection<Fake> connection = new DummyConnection<Fake>("fake", "fakeuser", new FakeTemplate());
+ List<String> users = repository.findUserIdsWithConnection(connection);
+ assertEquals(1, users.size());
+ }
+
@Test
public void jdbcConnectionRepository_addAndRemoveAConnection() {
ConnectionFactoryLocator cfl = context.getBean(ConnectionFactoryLocator.class);
View
7 ...est/resources/org/springframework/social/config/xml/SocialConfigNamespaceTest-context.xml
@@ -2,7 +2,7 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:social="http://www.springframework.org/schema/social"
- xmlns:socialfake="http://www.springframework.org/schema/social/fake"
+ xmlns:fake="http://www.springframework.org/schema/social/fake"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
@@ -14,9 +14,9 @@
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- SET UP SOME CONNECTION FACTORIES -->
- <social:jdbc-connection-repository />
+ <social:jdbc-connection-repository connection-signup-ref="connectionSignUp" />
- <socialfake:fake app-id="${fake.appId}" app-secret="${fake.appSecret}" />
+ <fake:fake app-id="${fake.appId}" app-secret="${fake.appSecret}" />
<!-- SUPPORTING BEANS -->
<jdbc:embedded-database id="dataSource" type="H2">
@@ -34,4 +34,5 @@
<prop key="fake.appSecret">fakeAppSecret</prop>
</util:properties>
+ <bean id="connectionSignUp" class="org.springframework.social.config.FakeConnectionSignUp" />
</beans>
View
2 ...cial-security/src/test/java/org/springframework/social/security/test/DummyConnection.java
@@ -80,7 +80,7 @@ public T getApi() {
public ConnectionData createData() {
return new ConnectionData(_key.getProviderId(), _key.getProviderUserId(), getDisplayName(),
- getProfileUrl(), getImageUrl(), null, null, null, null);
+ getProfileUrl(), getImageUrl(), "access_token", "secret", "refresh_token", System.currentTimeMillis() + 10000);
}
public static Answer<DummyConnection<Object>> answer() {

0 comments on commit 9a6de94

Please sign in to comment.