This repository has been archived by the owner on Apr 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 355
/
ProviderSignInUtils.java
81 lines (70 loc) · 4.12 KB
/
ProviderSignInUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* Copyright 2015 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
*
* https://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.connect.web;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.web.context.request.RequestAttributes;
/**
* Helper methods that support provider user sign-in scenarios.
* @author Keith Donald
*/
public class ProviderSignInUtils {
private SessionStrategy sessionStrategy;
private ConnectionFactoryLocator connectionFactoryLocator;
private UsersConnectionRepository connectionRepository;
public ProviderSignInUtils( ConnectionFactoryLocator connectionFactoryLocator,UsersConnectionRepository connectionRepository) {
this(new HttpSessionSessionStrategy(),connectionFactoryLocator,connectionRepository);
}
public ProviderSignInUtils(SessionStrategy sessionStrategy,ConnectionFactoryLocator connectionFactoryLocator,UsersConnectionRepository connectionRepository) {
this.sessionStrategy = sessionStrategy;
this.connectionFactoryLocator = connectionFactoryLocator;
this.connectionRepository = connectionRepository;
}
/**
* Get the connection to the provider user the client attempted to sign-in as.
* Using this connection you may fetch a {@link Connection#fetchUserProfile() provider user profile} and use that to pre-populate a local user registration/signup form.
* You can also lookup the id of the provider and use that to display a provider-specific user-sign-in-attempt flash message e.g. "Your Facebook Account is not connected to a Local account. Please sign up."
* Must be called before handlePostSignUp() or else the sign-in attempt will have been cleared from the session.
* Returns null if no provider sign-in has been attempted for the current user session.
* @param request the current request attributes, used to extract sign-in attempt information from the current user session
* @return the connection
*/
public Connection<?> getConnectionFromSession(RequestAttributes request) {
ProviderSignInAttempt signInAttempt = getProviderUserSignInAttempt(request);
return signInAttempt != null ? signInAttempt.getConnection(connectionFactoryLocator) : null;
}
/**
* Add the connection to the provider user the client attempted to sign-in with to the new local user's set of connections.
* Should be called after signing-up a new user in the context of a provider sign-in attempt.
* In this context, the user did not yet have a local account but attempted to sign-in using one of his or her existing provider accounts.
* Ensures provider sign-in attempt session context is cleaned up.
* Does nothing if no provider sign-in was attempted for the current user session (is safe to call in that case).
* @param userId the local application's user ID
* @param request the current request attributes, used to extract sign-in attempt information from the current user session
*/
public void doPostSignUp(String userId, RequestAttributes request) {
ProviderSignInAttempt signInAttempt = (ProviderSignInAttempt) sessionStrategy.getAttribute(request, ProviderSignInAttempt.SESSION_ATTRIBUTE);
if (signInAttempt != null) {
signInAttempt.addConnection(userId,connectionFactoryLocator,connectionRepository);
sessionStrategy.removeAttribute(request,ProviderSignInAttempt.SESSION_ATTRIBUTE);
}
}
// internal helpers
private ProviderSignInAttempt getProviderUserSignInAttempt(RequestAttributes request) {
return (ProviderSignInAttempt)sessionStrategy.getAttribute(request, ProviderSignInAttempt.SESSION_ATTRIBUTE);
}
}