1 /*
2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 import java.util.Map;
25 import javax.security.auth.Subject;
26 import javax.security.auth.callback.CallbackHandler;
27 import javax.security.auth.login.LoginContext;
28 import javax.security.auth.login.LoginException;
29 import javax.security.auth.spi.LoginModule;
30 
31 /**
32  * @test
33  * @bug 8048138
34  * @summary Check if shared state is passed to login module
35  * @run main/othervm SharedState
36  */
37 public class SharedState {
38 
39     static final String NAME = "name";
40     static final String VALUE = "shared";
41 
main(String[] args)42     public static void main(String[] args) throws LoginException {
43         System.setProperty("java.security.auth.login.config",
44                 System.getProperty("test.src")
45                         + System.getProperty("file.separator")
46                         + "shared.config");
47 
48         new LoginContext("SharedState").login();
49     }
50 
51     public static abstract class Module implements LoginModule {
52 
53         @Override
login()54         public boolean login() throws LoginException {
55             return true;
56         }
57 
58         @Override
commit()59         public boolean commit() throws LoginException {
60             return true;
61         }
62 
63         @Override
abort()64         public boolean abort() throws LoginException {
65             return true;
66         }
67 
68         @Override
logout()69         public boolean logout() throws LoginException {
70             return true;
71         }
72     }
73 
74     public static class FirstModule extends Module {
75 
76         @Override
initialize(Subject subject, CallbackHandler callbackHandler, Map<String,?> sharedState, Map<String,?> options)77         public void initialize(Subject subject, CallbackHandler callbackHandler,
78                             Map<String,?> sharedState, Map<String,?> options) {
79             ((Map)sharedState).put(NAME, VALUE);
80         }
81 
82     }
83 
84     public static class SecondModule extends Module {
85 
86         @Override
initialize(Subject subject, CallbackHandler callbackHandler, Map<String,?> sharedState, Map<String,?> options)87         public void initialize(Subject subject, CallbackHandler callbackHandler,
88                             Map<String,?> sharedState, Map<String,?> options) {
89             // check shared object
90             Object shared = sharedState.get(NAME);
91             if (!VALUE.equals(shared)) {
92                 throw new RuntimeException("Unexpected shared object: "
93                         + shared);
94             }
95         }
96 
97     }
98 }
99