commit | author | age
|
04a985
|
1 |
package com.gitblit.auth; |
e8b8ee
|
2 |
|
04a985
|
3 |
import com.gitblit.Constants; |
06116d
|
4 |
import com.gitblit.Constants.AccountType; |
6e3481
|
5 |
import com.gitblit.Constants.Role; |
04a985
|
6 |
import com.gitblit.Keys; |
JM |
7 |
import com.gitblit.auth.AuthenticationProvider.UsernamePasswordAuthenticationProvider; |
6e3481
|
8 |
import com.gitblit.models.TeamModel; |
e8b8ee
|
9 |
import com.gitblit.models.UserModel; |
WW |
10 |
import com.sforce.soap.partner.Connector; |
|
11 |
import com.sforce.soap.partner.GetUserInfoResult; |
|
12 |
import com.sforce.soap.partner.PartnerConnection; |
|
13 |
import com.sforce.ws.ConnectionException; |
|
14 |
import com.sforce.ws.ConnectorConfig; |
|
15 |
|
04a985
|
16 |
public class SalesforceAuthProvider extends UsernamePasswordAuthenticationProvider { |
699e71
|
17 |
|
04a985
|
18 |
public SalesforceAuthProvider() { |
JM |
19 |
super("salesforce"); |
|
20 |
} |
699e71
|
21 |
|
JM |
22 |
@Override |
8f1c9f
|
23 |
public AccountType getAccountType() { |
06116d
|
24 |
return AccountType.SALESFORCE; |
JM |
25 |
} |
699e71
|
26 |
|
e8b8ee
|
27 |
@Override |
04a985
|
28 |
public void setup() { |
e8b8ee
|
29 |
} |
WW |
30 |
|
|
31 |
@Override |
|
32 |
public UserModel authenticate(String username, char[] password) { |
|
33 |
ConnectorConfig config = new ConnectorConfig(); |
|
34 |
config.setUsername(username); |
|
35 |
config.setPassword(new String(password)); |
|
36 |
|
|
37 |
try { |
|
38 |
PartnerConnection connection = Connector.newConnection(config); |
|
39 |
|
|
40 |
GetUserInfoResult info = connection.getUserInfo(); |
|
41 |
|
|
42 |
String org = settings.getString(Keys.realm.salesforce.orgId, "0") |
|
43 |
.trim(); |
|
44 |
|
|
45 |
if (!org.equals("0")) { |
|
46 |
if (!org.equals(info.getOrganizationId())) { |
|
47 |
logger.warn("Access attempted by user of an invalid org: " |
|
48 |
+ info.getUserName() + ", org: " |
|
49 |
+ info.getOrganizationName() + "(" |
|
50 |
+ info.getOrganizationId() + ")"); |
|
51 |
|
|
52 |
return null; |
|
53 |
} |
|
54 |
} |
|
55 |
|
|
56 |
logger.info("Authenticated user " + info.getUserName() |
|
57 |
+ " using org " + info.getOrganizationName() + "(" |
|
58 |
+ info.getOrganizationId() + ")"); |
|
59 |
|
|
60 |
String simpleUsername = getSimpleUsername(info); |
|
61 |
|
|
62 |
UserModel user = null; |
|
63 |
synchronized (this) { |
04a985
|
64 |
user = userManager.getUserModel(simpleUsername); |
c1b0e4
|
65 |
if (user == null) { |
e8b8ee
|
66 |
user = new UserModel(simpleUsername); |
WW |
67 |
} |
|
68 |
|
c1b0e4
|
69 |
setCookie(user, password); |
e8b8ee
|
70 |
setUserAttributes(user, info); |
WW |
71 |
|
04a985
|
72 |
updateUser(user); |
e8b8ee
|
73 |
} |
WW |
74 |
|
|
75 |
return user; |
|
76 |
} catch (ConnectionException e) { |
|
77 |
logger.error("Failed to authenticate", e); |
|
78 |
} |
|
79 |
|
|
80 |
return null; |
|
81 |
} |
|
82 |
|
|
83 |
private void setUserAttributes(UserModel user, GetUserInfoResult info) { |
|
84 |
// Don't want visibility into the real password, make up a dummy |
de3474
|
85 |
user.password = Constants.EXTERNAL_ACCOUNT; |
e8b8ee
|
86 |
user.accountType = getAccountType(); |
WW |
87 |
|
|
88 |
// Get full name Attribute |
|
89 |
user.displayName = info.getUserFullName(); |
|
90 |
|
|
91 |
// Get email address Attribute |
|
92 |
user.emailAddress = info.getUserEmail(); |
|
93 |
} |
|
94 |
|
|
95 |
/** |
|
96 |
* Simple user name is the first part of the email address. |
|
97 |
*/ |
|
98 |
private String getSimpleUsername(GetUserInfoResult info) { |
|
99 |
String email = info.getUserEmail(); |
|
100 |
|
|
101 |
return email.split("@")[0]; |
|
102 |
} |
|
103 |
|
04a985
|
104 |
|
e8b8ee
|
105 |
@Override |
WW |
106 |
public boolean supportsCredentialChanges() { |
|
107 |
return false; |
|
108 |
} |
|
109 |
|
|
110 |
@Override |
|
111 |
public boolean supportsDisplayNameChanges() { |
|
112 |
return false; |
|
113 |
} |
|
114 |
|
|
115 |
@Override |
|
116 |
public boolean supportsEmailAddressChanges() { |
|
117 |
return false; |
|
118 |
} |
04a985
|
119 |
|
JM |
120 |
@Override |
|
121 |
public boolean supportsTeamMembershipChanges() { |
|
122 |
return true; |
|
123 |
} |
6e3481
|
124 |
|
JM |
125 |
@Override |
|
126 |
public boolean supportsRoleChanges(UserModel user, Role role) { |
|
127 |
return true; |
|
128 |
} |
|
129 |
|
|
130 |
@Override |
|
131 |
public boolean supportsRoleChanges(TeamModel team, Role role) { |
|
132 |
return true; |
|
133 |
} |
|
134 |
|
e8b8ee
|
135 |
} |