KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > identity > sso > JBossSingleSignOnProcessor


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.security.identity.sso;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import org.opensaml.SAMLAssertion;
30 import org.opensaml.SAMLAuthenticationQuery;
31 import org.opensaml.SAMLAuthenticationStatement;
32 import org.opensaml.SAMLException;
33 import org.opensaml.SAMLNameIdentifier;
34 import org.opensaml.SAMLRequest;
35 import org.opensaml.SAMLResponse;
36 import org.opensaml.SAMLSubject;
37 import org.opensaml.provider.SecureRandomIDProvider;
38
39 //$Id: JBossSingleSignOnProcessor.java 43627 2006-04-11 16:59:49Z asaldhana $
40

41 /**
42  * OpenSAML v1.1 based implementation
43  * @author <a HREF="mailto:Sohil.Shah@jboss.org">Sohil Shah</a>
44  * @author <a HREF="mailto:Anil.Saldhana@jboss.org">Anil Saldhana</a>
45  * @since Apr 10, 2006
46  * @version $Revision: 43627 $
47  */

48 public class JBossSingleSignOnProcessor implements SingleSignOnProcessor
49 {
50    private SecureRandomIDProvider idProvider = new SecureRandomIDProvider();
51    
52    private static final String JavaDoc LOGIN_FAILED="login_failed";
53    
54    /**
55     * @see SingleSignOnProcessor#generateAuthRequest(String, String)
56     */

57    public String JavaDoc generateAuthRequest(String JavaDoc username, String JavaDoc password)
58    throws SSOException
59    {
60       if(username == null || username.length() == 0)
61          throw new IllegalArgumentException JavaDoc("username is null or zero-length");
62       if(password == null)
63          throw new IllegalArgumentException JavaDoc("password is null");
64       try
65       {
66          String JavaDoc request = null;
67          
68          //create a SAMLSubject
69
SAMLNameIdentifier id = new SAMLNameIdentifier();
70          id.setName(username);
71          id.setNameQualifier(password);
72          id.setFormat(SAMLNameIdentifier.FORMAT_UNSPECIFIED);
73          SAMLSubject subject = new SAMLSubject();
74          subject.setNameIdentifier(id);
75          SAMLAuthenticationQuery query = new SAMLAuthenticationQuery(subject,
76                SAMLAuthenticationStatement.AuthenticationMethod_Password);
77          
78          SAMLRequest authRequest = new SAMLRequest(query);
79          request = authRequest.toString();
80          
81          return request;
82       }
83       catch(SAMLException sme)
84       {
85          throw new SSOException(sme);
86       }
87    }
88    
89    /**
90     * @see SingleSignOnProcessor#generateAuthResponse(String, String, boolean)
91     */

92    public String JavaDoc generateAuthResponse(String JavaDoc assertingParty, String JavaDoc username,
93          boolean success) throws SSOException
94    {
95       if(assertingParty == null || assertingParty.length() == 0)
96          throw new IllegalArgumentException JavaDoc("assertingParty is null or zero-length");
97       if(username == null || username.length() == 0)
98          throw new IllegalArgumentException JavaDoc("username is null or zero-length");
99       try
100       {
101          String JavaDoc response = null;
102          
103          //construct the SAML Response
104
SAMLResponse authResponse = new SAMLResponse();
105          authResponse.setId(this.idProvider.getIdentifier());
106          
107          if(success)
108          {
109             //create a successfull authenticationstatment
110
SAMLNameIdentifier id = new SAMLNameIdentifier();
111             id.setName(username);
112             id.setFormat(SAMLNameIdentifier.FORMAT_UNSPECIFIED);
113             SAMLSubject subject = new SAMLSubject();
114             subject.setNameIdentifier(id);
115             
116             String JavaDoc methodStr = SAMLAuthenticationStatement.AuthenticationMethod_Password;
117             SAMLAuthenticationStatement authStatement = new SAMLAuthenticationStatement();
118             
119             authStatement.setAuthMethod(methodStr);
120             authStatement.setSubject(subject);
121             authStatement.setAuthInstant(new Date JavaDoc());
122             
123             //create an assertion
124
SAMLAssertion authAssertion = new SAMLAssertion();
125             authAssertion.setId(this.idProvider.getIdentifier());
126             authAssertion.setIssuer(assertingParty);
127             authAssertion.addStatement(authStatement);
128             
129             //create the SAMLResponse
130
authResponse.addAssertion(authAssertion);
131          }
132          else
133          {
134             SAMLException loginFailed = new SAMLException(LOGIN_FAILED);
135             authResponse.setStatus(loginFailed);
136          }
137          
138          response = authResponse.toString();
139          
140          return response;
141       }
142       catch(SAMLException sme)
143       {
144          throw new SSOException(sme);
145       }
146    }
147    
148    /**
149     * @see SingleSignOnProcessor#parseAuthRequest(String)
150     */

151    public SSOUser parseAuthRequest(String JavaDoc request) throws SSOException
152    {
153       if(request == null || request.length() == 0)
154          throw new IllegalArgumentException JavaDoc("request is null or zero-length");
155       ByteArrayInputStream JavaDoc bis = null;
156       try
157       {
158          SSOUser user = null;
159          
160          bis = new ByteArrayInputStream JavaDoc(request.getBytes());
161          SAMLRequest authRequest = new SAMLRequest(bis);
162          
163          
164          SAMLAuthenticationQuery query = (SAMLAuthenticationQuery)authRequest.getQuery();
165          SAMLSubject subject = query.getSubject();
166          
167          //get the SAMLNameIdentifier
168
SAMLNameIdentifier id = subject.getNameIdentifier();
169          String JavaDoc username = id.getName();
170          String JavaDoc password = id.getNameQualifier();
171          
172          user = new SSOUser(username,password);
173          
174          return user;
175       }
176       catch(SAMLException sme)
177       {
178          throw new SSOException(sme);
179       }
180       finally
181       {
182          if(bis!=null)
183          {
184             try{bis.close();}catch(IOException JavaDoc e){}
185          }
186       }
187    }
188    
189    /**
190     * @see JBossSingleSignOnProcessor#parseAuthResponse(String)
191     */

192    public AuthResponse parseAuthResponse(String JavaDoc resp) throws SSOException
193    {
194       if(resp == null || resp.length() == 0)
195          throw new IllegalArgumentException JavaDoc("response is null or zero-length");
196       AuthResponse authResponse = null;
197       ByteArrayInputStream JavaDoc bis = null;
198       boolean success = false;
199       String JavaDoc assertToken = null;
200       String JavaDoc assertingParty = null;
201       String JavaDoc username = null;
202       try
203       {
204           bis = new ByteArrayInputStream JavaDoc(resp.getBytes());
205           SAMLResponse response = new SAMLResponse(bis);
206           
207           Iterator JavaDoc assertions = response.getAssertions();
208           if(assertions!=null && assertions.hasNext())
209           {
210               success = true;
211               SAMLAssertion authAssertion = (SAMLAssertion)assertions.next();
212               assertToken = authAssertion.getId();
213               assertingParty = authAssertion.getIssuer();
214               SAMLAuthenticationStatement authStatement = (SAMLAuthenticationStatement)authAssertion.getStatements().next();
215               username = authStatement.getSubject().getNameIdentifier().getName();
216               
217               SSOUser user = new SSOUser(username,null);
218               authResponse = new AuthResponse(assertingParty,assertToken,user,success);
219           }
220           
221           return authResponse;
222       }
223       catch(SAMLException sme)
224       {
225           if(sme.getMessage().equals(LOGIN_FAILED))
226           {
227               success = false;
228               authResponse = new AuthResponse(assertingParty,assertToken,null,success);
229               return authResponse;
230           }
231           else
232           {
233               throw new SSOException(sme);
234           }
235       }
236       finally
237       {
238           if(bis!=null)
239           {
240               try{bis.close();}catch(Exception JavaDoc e){}
241           }
242       }
243    }
244 }
245
Popular Tags