return new EmptyRepresentation();
}
@Get("html")
public Representation represent() {
Form params = getQuery();
log.info("OpenIDResource : " + params);
String rc = params.getFirstValue("return");
if (rc != null && rc.length() > 0) {
Map<String, String> axRequired = new HashMap<String, String>();
Map<String, String> axOptional = new HashMap<String, String>();
Identifier i = verifyResponse(axRequired, axOptional);
if (i == null) {
log.info("Authentication Failed");
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
return new StringRepresentation("Authentication Failed");
}
log.info("Identifier = " + i.getIdentifier());
String id = i.getIdentifier();
if (id != null) {
// New Code, always return JSON and let filter handle any
// callback.
// TODO maybe move it to use Principal.
JSONObject obj = new JSONObject();
try {
obj.put("id", i.getIdentifier());
for (String s : axRequired.keySet()) {
obj.put(s, axRequired.get(s));
}
for (String s : axOptional.keySet()) {
obj.put(s, axOptional.get(s));
}
} catch (JSONException e) {
log.log(Level.WARNING, "Failed to get the ID!", e);
}
getResponse().setEntity(new JsonRepresentation(obj));
}
// cleanup of cookie
getResponse().getCookieSettings().remove(DESCRIPTOR_COOKIE);
CookieSetting disc = new CookieSetting(DESCRIPTOR_COOKIE, "");
disc.setMaxAge(0);
getResponse().getCookieSettings().add(disc);
// TODO save the identifier // send back to OAuth
return getResponse().getEntity();
}
String target = params.getFirstValue("openid_identifier");
if (target == null || target.length() == 0) {
// No target - might be Yadis discovery
String location = setXRDSHeader();
StringBuilder html = new StringBuilder();
html.append("<html><head><meta http-equiv=\"X-XRDS-Location\" content=\"");
html.append(location);
html.append("\"/></head></html>");
return new StringRepresentation(html.toString(),
MediaType.TEXT_HTML);
}
try {
StringBuilder returnToUrl = new StringBuilder();
returnToUrl.append(getReference().getBaseRef());
returnToUrl.append("?return=true");
// --- Forward proxy setup (only if needed) ---
// ProxyProperties proxyProps = new ProxyProperties();
// proxyProps.setProxyName("proxy.example.com");
// proxyProps.setProxyPort(8080);
// HttpClientFactory.setProxyProperties(proxyProps);
// perform discovery on the user-supplied identifier
List<?> discoveries = null;
discoveries = discovery.discover(target);
for (Object o : discoveries) {
if (o instanceof DiscoveryInformation) {
DiscoveryInformation di = (DiscoveryInformation) o;
log.info("Found - " + di.getOPEndpoint());
target = di.getOPEndpoint().toString();
}
}
ConsumerManager manager = getManager(target);
// try {
// discoveries = manager.discover(target);
// } catch (YadisException e) {
// log.info("Could not connect in time!!!!!!!!!!!!!!!!!!!!!!");
// return new
// StringRepresentation("Could not connect to Identity Server in time.",MediaType.TEXT_HTML);
// }
// attempt to associate with the OpenID provider
// and retrieve one service endpoint for authentication
DiscoveryInformation discovered = manager.associate(discoveries);
// store the discovery information in the user's session
// getContext().getAttributes().put("openid-disc", discovered);
String sessionId = String.valueOf(System
.identityHashCode(discovered));
session.put(sessionId, discovered);
getResponse().getCookieSettings().add(
new CookieSetting(DESCRIPTOR_COOKIE, sessionId));
log.info("Setting DESCRIPTOR COOKIE");
// obtain a AuthRequest message to be sent to the OpenID provider
AuthRequest authReq = manager.authenticate(discovered,
returnToUrl.toString()); // TODO maybe add TIMESTAMP?
// Domain wide realm add meta to main page
// http://localhost:8080/oauth/xrds?returnTo=http://localhost:8080/oauth/openid_login\r\n
// log.info("OpenID - REALM = " +
// getReference().getHostIdentifier());
// authReq.setRealm(getReference().getHostIdentifier().toString());
log.info("OpenID - REALM = " + getReference().getBaseRef());
authReq.setRealm(getReference().getBaseRef().toString());
// Attribute Exchange - getting optional and required
FetchRequest fetch = null;
String[] optional = params.getValuesArray("ax_optional", true);
for (String o : optional) {
if (!ax.containsKey(o)) {
log.warning("Not supported AX extension : " + o);
continue;
}
if (fetch == null)
fetch = FetchRequest.createFetchRequest();
fetch.addAttribute(o, ax.get(o), false);
}
String[] required = params.getValuesArray("ax_required", true);
for (String r : required) {
if (!ax.containsKey(r)) {
log.warning("Not supported AX extension : " + r);
continue;
}
if (fetch == null)
fetch = FetchRequest.createFetchRequest();
fetch.addAttribute(r, ax.get(r), true);
}
if (fetch != null) {
authReq.addExtension(fetch);
}
if (!discovered.isVersion2()) {
log.info("REDIRECTING TEMPORARY");
// Option 1: GET HTTP-redirect to the OpenID Provider endpoint
// The only method supported in OpenID 1.x
// redirect-URL usually limited ~2048 bytes
redirectTemporary(authReq.getDestinationUrl(true));
return null;
} else {
// Option 2: HTML FORM Redirection (Allows payloads >2048 bytes)
Form msg = new Form();
for (Object key : authReq.getParameterMap().keySet()) {
msg.add(key.toString(),
authReq.getParameterValue(key.toString()));
log.info("Adding to form - key " + key.toString()
+ " : value"
+ authReq.getParameterValue(key.toString()));
}