1 /*
2 * Copyright (c) 2007, Fraunhofer-Gesellschaft
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * (1) Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the disclaimer at the end.
11 * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * (2) Neither the name of Fraunhofer nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * DISCLAIMER
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
35 package org.ogf.graap.wsag.api.security;
36
37 import java.net.URL;
38 import java.net.URLDecoder;
39
40 import javax.security.auth.Subject;
41 import javax.security.auth.login.LoginContext;
42 import javax.security.auth.login.LoginException;
43
44 import org.apache.log4j.Logger;
45 import org.ogf.graap.wsag.api.WsagConstants;
46 import org.ogf.graap.wsag.api.logging.LogMessage;
47 import org.ogf.graap.wsag4j.types.configuration.WSRFEngineConfigurationType;
48
49 /**
50 * KeystoreLoginContext
51 *
52 * @author Oliver Waeldrich
53 *
54 */
55 public class KeystoreLoginContext extends LoginContext
56 {
57
58 /**
59 * JAAS default configuration
60 */
61 private static final String JAAS_DEFAULT_CONFIGURATION =
62 "/META-INF/org.ogf.graap.wsag.api.security.KeystoreLoginContext.properties";
63
64 private static final Logger LOG = Logger.getLogger( KeystoreLoginContext.class );
65
66 static
67 {
68 try
69 {
70 if ( System.getProperties().contains( "java.security.auth.login.config" ) )
71 {
72 LOG.warn( "java.security.auth.login.config is already set - this may corrupt WSAG4J configuration" );
73 }
74 else
75 {
76 //
77 // read application provided configuration
78 //
79 URL authconf = KeystoreLoginContext.class.getResource( WsagConstants.WSAG4J_JAAS_CONFIG_FILE );
80
81 //
82 // if null read client implementation default configuration
83 //
84 if ( authconf == null )
85 {
86 authconf =
87 KeystoreLoginContext.class.getResource( WsagConstants.WSAG4J_JAAS_CONFIG_FILE_DEFAULT );
88 }
89
90 //
91 // if still null use default API configuration
92 //
93 if ( authconf == null )
94 {
95 authconf = KeystoreLoginContext.class.getResource( JAAS_DEFAULT_CONFIGURATION );
96 }
97
98 String p = URLDecoder.decode( authconf.toExternalForm(), "UTF-8" );
99 LOG.info( LogMessage.getMessage( "WSAG4J JAAS configuration: {0}", p ) );
100
101 System.setProperty( "java.security.auth.login.config", p );
102 }
103 }
104 catch ( Exception e )
105 {
106 LOG.equals( "Could not read JAAS configuration." );
107 }
108 }
109
110 /**
111 * Creates a new login context using the specified keystore properties.
112 *
113 * @param properties
114 * the keystore properties to use
115 *
116 * @throws LoginException
117 * failed to login
118 */
119 public KeystoreLoginContext( KeystoreProperties properties )
120 throws LoginException
121 {
122 this( new KeystoreCallbackHandler( properties ), new KeystoreConfiguration( properties ) );
123 }
124
125 /**
126 * Creates a new login context using the specified WSRF engine configuration.
127 *
128 * @param configuration
129 * the WSRF engine configuration to use
130 *
131 * @throws LoginException
132 * failed to login
133 */
134 public KeystoreLoginContext( WSRFEngineConfigurationType configuration )
135 throws LoginException
136 {
137 this( new KeystoreProperties( configuration ) );
138 }
139
140 private KeystoreLoginContext( KeystoreCallbackHandler cbHandler, KeystoreConfiguration configuration )
141 throws LoginException
142 {
143 super( "KEYSTORE_CLIENT", new Subject(), cbHandler, configuration );
144 }
145
146 }