Skip to content
This repository was archived by the owner on Apr 5, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.springframework.social.connect.ConnectionKey;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.connect.DuplicateConnectionException;
import org.springframework.social.connect.NoSuchConnectionException;
import org.springframework.social.connect.support.OAuth1ConnectionFactory;
import org.springframework.social.connect.support.OAuth2ConnectionFactory;
import org.springframework.stereotype.Controller;
Expand Down Expand Up @@ -216,7 +217,7 @@ public RedirectView oauth1Callback(@PathVariable String providerId, NativeWebReq
try {
OAuth1ConnectionFactory<?> connectionFactory = (OAuth1ConnectionFactory<?>) connectionFactoryLocator.getConnectionFactory(providerId);
Connection<?> connection = webSupport.completeConnection(connectionFactory, request);
addConnection(connection, connectionFactory, request);
addOrUpdateConnection(connection, connectionFactory, request);
} catch (Exception e) {
request.setAttribute(PROVIDER_ERROR_ATTRIBUTE, e, RequestAttributes.SCOPE_SESSION);
logger.warn("Exception while handling OAuth1 callback (" + e.getMessage() + "). Redirecting to " + providerId +" connection status page.");
Expand All @@ -234,7 +235,7 @@ public RedirectView oauth2Callback(@PathVariable String providerId, NativeWebReq
try {
OAuth2ConnectionFactory<?> connectionFactory = (OAuth2ConnectionFactory<?>) connectionFactoryLocator.getConnectionFactory(providerId);
Connection<?> connection = webSupport.completeConnection(connectionFactory, request);
addConnection(connection, connectionFactory, request);
addOrUpdateConnection(connection, connectionFactory, request);
} catch (Exception e) {
request.setAttribute(PROVIDER_ERROR_ATTRIBUTE, e, RequestAttributes.SCOPE_SESSION);
logger.warn("Exception while handling OAuth2 callback (" + e.getMessage() + "). Redirecting to " + providerId +" connection status page.");
Expand Down Expand Up @@ -336,13 +337,19 @@ private String getViewPath() {
return "connect/";
}

private void addConnection(Connection<?> connection, ConnectionFactory<?> connectionFactory, WebRequest request) {
try {
connectionRepository.addConnection(connection);
postConnect(connectionFactory, connection, request);
} catch (DuplicateConnectionException e) {
request.setAttribute(DUPLICATE_CONNECTION_ATTRIBUTE, e, RequestAttributes.SCOPE_SESSION);
}
private void addOrUpdateConnection(Connection<?> connection, ConnectionFactory<?> connectionFactory, WebRequest request) {
try {
connectionRepository.getConnection(connection.getKey());
connectionRepository.updateConnection(connection);
postConnect(connectionFactory, connection, request);
} catch (NoSuchConnectionException ex) {
try {
connectionRepository.addConnection(connection);
postConnect(connectionFactory, connection, request);
} catch (DuplicateConnectionException e) {
request.setAttribute(DUPLICATE_CONNECTION_ATTRIBUTE, e, RequestAttributes.SCOPE_SESSION);
}
}
}

@SuppressWarnings({ "rawtypes", "unchecked" })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionKey;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.connect.NoSuchConnectionException;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

Expand All @@ -47,7 +48,7 @@ public MultiValueMap<String, Connection<?>> findConnectionsToUsers(MultiValueMap
}

public Connection<?> getConnection(ConnectionKey connectionKey) {
return null;
throw new NoSuchConnectionException(connectionKey);
}

public <A> Connection<A> getConnection(Class<A> apiType, String providerUserId) {
Expand Down