001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015: package org.acegisecurity.ui.openid.consumers;
016:
017: import org.acegisecurity.providers.openid.OpenIDAuthenticationStatus;
018: import org.acegisecurity.providers.openid.OpenIDAuthenticationToken;
019:
020: import org.acegisecurity.ui.openid.OpenIDConsumer;
021: import org.acegisecurity.ui.openid.OpenIDConsumerException;
022:
023: import org.openid4java.association.AssociationException;
024:
025: import org.openid4java.consumer.ConsumerException;
026: import org.openid4java.consumer.ConsumerManager;
027: import org.openid4java.consumer.VerificationResult;
028:
029: import org.openid4java.discovery.DiscoveryException;
030: import org.openid4java.discovery.DiscoveryInformation;
031: import org.openid4java.discovery.Identifier;
032:
033: import org.openid4java.message.AuthRequest;
034: import org.openid4java.message.MessageException;
035: import org.openid4java.message.ParameterList;
036:
037: import java.util.List;
038:
039: import javax.servlet.http.HttpServletRequest;
040: import javax.servlet.http.HttpSession;
041:
042: /**
043: * DOCUMENT ME!
044: *
045: * @author Ray Krueger
046: */
047: public class OpenId4JavaConsumer implements OpenIDConsumer {
048: //~ Instance fields ================================================================================================
049:
050: private final ConsumerManager consumerManager;
051:
052: //~ Constructors ===================================================================================================
053:
054: public OpenId4JavaConsumer(ConsumerManager consumerManager) {
055: this .consumerManager = consumerManager;
056: }
057:
058: public OpenId4JavaConsumer() throws ConsumerException {
059: this (new ConsumerManager());
060: }
061:
062: //~ Methods ========================================================================================================
063:
064: public String beginConsumption(HttpServletRequest req,
065: String identityUrl, String returnToUrl)
066: throws OpenIDConsumerException {
067: List discoveries;
068:
069: try {
070: discoveries = consumerManager.discover(identityUrl);
071: } catch (DiscoveryException e) {
072: throw new OpenIDConsumerException("Error during discovery",
073: e);
074: }
075:
076: DiscoveryInformation information = consumerManager
077: .associate(discoveries);
078: HttpSession session = req.getSession(true);
079: session.setAttribute(DiscoveryInformation.class.getName(),
080: information);
081:
082: AuthRequest authReq;
083:
084: try {
085: authReq = consumerManager.authenticate(information,
086: returnToUrl);
087: } catch (MessageException e) {
088: throw new OpenIDConsumerException(
089: "Error processing ConumerManager authentication", e);
090: } catch (ConsumerException e) {
091: throw new OpenIDConsumerException(
092: "Error processing ConumerManager authentication", e);
093: }
094:
095: return authReq.getDestinationUrl(true);
096: }
097:
098: public OpenIDAuthenticationToken endConsumption(
099: HttpServletRequest request) throws OpenIDConsumerException {
100: // extract the parameters from the authentication response
101: // (which comes in as a HTTP request from the OpenID provider)
102: ParameterList openidResp = new ParameterList(request
103: .getParameterMap());
104:
105: // retrieve the previously stored discovery information
106: DiscoveryInformation discovered = (DiscoveryInformation) request
107: .getSession().getAttribute(
108: DiscoveryInformation.class.getName());
109:
110: // extract the receiving URL from the HTTP request
111: StringBuffer receivingURL = request.getRequestURL();
112: String queryString = request.getQueryString();
113:
114: if ((queryString != null) && (queryString.length() > 0)) {
115: receivingURL.append("?").append(request.getQueryString());
116: }
117:
118: // verify the response
119: VerificationResult verification;
120:
121: try {
122: verification = consumerManager.verify(receivingURL
123: .toString(), openidResp, discovered);
124: } catch (MessageException e) {
125: throw new OpenIDConsumerException(
126: "Error verifying openid response", e);
127: } catch (DiscoveryException e) {
128: throw new OpenIDConsumerException(
129: "Error verifying openid response", e);
130: } catch (AssociationException e) {
131: throw new OpenIDConsumerException(
132: "Error verifying openid response", e);
133: }
134:
135: // examine the verification result and extract the verified identifier
136: Identifier verified = verification.getVerifiedId();
137:
138: if (verified != null) {
139: return new OpenIDAuthenticationToken(
140: OpenIDAuthenticationStatus.SUCCESS, verified
141: .getIdentifier(), "some message");
142: } else {
143: return new OpenIDAuthenticationToken(
144: OpenIDAuthenticationStatus.FAILURE, discovered
145: .getClaimedIdentifier().getIdentifier(),
146: "Verification status message: ["
147: + verification.getStatusMsg() + "]");
148: }
149: }
150: }
|