This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
FedoraOpenId.java
273 lines (227 loc) · 8.17 KB
/
FedoraOpenId.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* Copyright 2010, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.zanata.security;
import static org.jboss.seam.ScopeType.SESSION;
import static org.jboss.seam.annotations.Install.APPLICATION;
import javax.servlet.http.HttpServletRequest;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.core.Events;
import org.jboss.seam.log.LogProvider;
import org.jboss.seam.log.Logging;
import org.jboss.seam.security.openid.OpenIdPrincipal;
import org.openid4java.consumer.ConsumerException;
import org.openid4java.message.ParameterList;
import java.util.List;
import javax.faces.context.ExternalContext;
import org.jboss.seam.faces.FacesManager;
import org.jboss.seam.faces.Redirect;
import org.jboss.seam.security.Identity;
import org.openid4java.OpenIDException;
import org.openid4java.consumer.ConsumerManager;
import org.openid4java.consumer.VerificationResult;
import org.openid4java.discovery.DiscoveryInformation;
import org.openid4java.discovery.Identifier;
import org.openid4java.message.AuthRequest;
import org.openid4java.message.ax.FetchRequest;
import org.zanata.ApplicationConfiguration;
@Name("org.jboss.seam.security.fedoraOpenId")
@Scope(SESSION)
@Install(precedence = APPLICATION)
@BypassInterceptors
/*
* based on org.jboss.seam.security.openid.OpenId class
*/
public class FedoraOpenId
{
private static final long serialVersionUID = 1L;
private static String FEDORA_HOST = ".id.fedoraproject.org/";
private static final LogProvider log = Logging.getLogProvider(FedoraOpenId.class);
private ZanataIdentity identity;
private ApplicationConfiguration applicationConfiguration;
private String id;
private String validatedId;
private ConsumerManager manager;
private DiscoveryInformation discovered;
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public void login()
{
validatedId = null;
String returnToUrl = returnToUrl();
String url = authRequest(id, returnToUrl);
if (url != null)
{
Redirect redirect = Redirect.instance();
redirect.captureCurrentView();
FacesManager.instance().redirectToExternalURL(url);
}
}
@SuppressWarnings("rawtypes")
protected String authRequest(String userSuppliedString, String returnToUrl)
{
try
{
// perform discovery on the user-supplied identifier
List discoveries = manager.discover(userSuppliedString);
// attempt to associate with the OpenID provider
// and retrieve one service endpoint for authentication
discovered = manager.associate(discoveries);
// // store the discovery information in the user's session
// httpReq.getSession().setAttribute("openid-disc", discovered);
// obtain a AuthRequest message to be sent to the OpenID provider
AuthRequest authReq = manager.authenticate(discovered, returnToUrl);
// Attribute Exchange example: fetching the 'email' attribute
FetchRequest fetch = FetchRequest.createFetchRequest();
fetch.addAttribute("email", "http://schema.openid.net/contact/email", // type
// URI
true); // required
// attach the extension to the authentication request
authReq.addExtension(fetch);
return authReq.getDestinationUrl(true);
}
catch (OpenIDException e)
{
log.warn(e, e);
}
return null;
}
public void verify()
{
ExternalContext context = javax.faces.context.FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest request = (HttpServletRequest) context.getRequest();
validatedId = verifyResponse(request);
}
public boolean loginImmediately()
{
if (validatedId != null)
{
Identity.instance().acceptExternallyAuthenticatedPrincipal((new OpenIdPrincipal(validatedId)));
return true;
}
return false;
}
public boolean isValid()
{
return validatedId != null;
}
public String getValidatedId()
{
return validatedId;
}
public String verifyResponse(HttpServletRequest httpReq)
{
try
{
// extract the parameters from the authentication response
// (which comes in as a HTTP request from the OpenID provider)
ParameterList response = new ParameterList(httpReq.getParameterMap());
StringBuffer receivingURL = new StringBuffer(returnToUrl());
String queryString = httpReq.getQueryString();
if (queryString != null && queryString.length() > 0)
receivingURL.append("?").append(httpReq.getQueryString());
// verify the response; ConsumerManager needs to be the same
// (static) instance used to place the authentication request
VerificationResult verification = manager.verify(receivingURL.toString(), response, discovered);
// examine the verification result and extract the verified identifier
Identifier verified = verification.getVerifiedId();
if (verified != null)
{
// AuthSuccess authSuccess =
// (AuthSuccess) verification.getAuthResponse();
// if (authSuccess.hasExtension(AxMessage.OPENID_NS_AX)) {
// FetchResponse fetchResp = (FetchResponse) authSuccess
// .getExtension(AxMessage.OPENID_NS_AX);
//
// List emails = fetchResp.getAttributeValues("email");
// String email = (String) emails.get(0);
// }
return verified.getIdentifier();
}
}
catch (OpenIDException e)
{
log.warn(e, e);
}
return null;
}
public void logout() throws ConsumerException
{
init();
}
@Create
public void init()
{
try
{
manager = new ConsumerManager();
discovered = null;
id = null;
validatedId = null;
}
catch (ConsumerException e)
{
throw new RuntimeException(e);
}
identity = (ZanataIdentity) Component.getInstance(ZanataIdentity.class, ScopeType.SESSION);
applicationConfiguration = (ApplicationConfiguration) Component.getInstance(ApplicationConfiguration.class, ScopeType.APPLICATION);
}
public void loginImmediate()
{
if (loginImmediately())
{
identity.setPreAuthenticated(true);
if (Events.exists())
Events.instance().raiseEvent(Identity.EVENT_POST_AUTHENTICATE, identity);
if (Events.exists())
Events.instance().raiseEvent(Identity.EVENT_LOGIN_SUCCESSFUL);
}
}
public void login(String username)
{
try
{
String var = "http://" + username + FEDORA_HOST;
setId(var);
log.info("openid:" + getId());
login();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
public String returnToUrl()
{
return applicationConfiguration.getServerPath() + "/openid.seam";
}
}