8
8
package io .lighty .aaa ;
9
9
10
10
import io .lighty .aaa .config .AAAConfiguration ;
11
- import java .util .ArrayList ;
12
11
import io .lighty .server .LightyJettyServerProvider ;
13
12
import java .util .HashMap ;
14
- import java .util .List ;
15
13
import java .util .Map ;
16
14
import java .util .concurrent .CompletableFuture ;
17
- import org .eclipse .jetty .server .Handler ;
18
- import org .eclipse .jetty .server .handler .ContextHandlerCollection ;
19
- import org .eclipse .jetty .servlet .FilterHolder ;
20
- import org .eclipse .jetty .servlet .ServletContextHandler ;
21
- import org .eclipse .jetty .servlet .ServletHolder ;
15
+ import javax .servlet .ServletException ;
16
+ import org .apache .shiro .mgt .DefaultSecurityManager ;
17
+ import org .apache .shiro .web .session .mgt .DefaultWebSessionManager ;
22
18
import org .eclipse .jetty .servlets .CrossOriginFilter ;
23
- import org .glassfish .jersey .internal .guava .Preconditions ;
24
- import org .glassfish .jersey .server .ResourceConfig ;
25
- import org .glassfish .jersey .servlet .ServletContainer ;
26
19
import org .opendaylight .aaa .api .AuthenticationService ;
27
20
import org .opendaylight .aaa .api .ClaimCache ;
28
21
import org .opendaylight .aaa .api .CredentialAuth ;
39
32
import org .opendaylight .aaa .filterchain .configuration .impl .CustomFilterAdapterConfigurationImpl ;
40
33
import org .opendaylight .aaa .filterchain .filters .CustomFilterAdapter ;
41
34
import org .opendaylight .aaa .impl .password .service .DefaultPasswordHashService ;
42
- import org .opendaylight .aaa .shiro .filters .AAAShiroFilter ;
43
35
import org .opendaylight .aaa .shiro .idm .IdmLightApplication ;
44
36
import org .opendaylight .aaa .shiro .idm .IdmLightProxy ;
45
- import org .opendaylight .aaa .shiro .moon .MoonTokenEndpoint ;
46
37
import org .opendaylight .aaa .shiro .web .env .AAAWebEnvironment ;
38
+ import org .opendaylight .aaa .shiro .web .env .ShiroWebContextSecurer ;
47
39
import org .opendaylight .aaa .tokenauthrealm .auth .AuthenticationManager ;
48
40
import org .opendaylight .aaa .tokenauthrealm .auth .HttpBasicAuth ;
49
41
import org .opendaylight .aaa .tokenauthrealm .auth .TokenAuthenticators ;
42
+ import org .opendaylight .aaa .web .FilterDetails ;
43
+ import org .opendaylight .aaa .web .ServletDetails ;
44
+ import org .opendaylight .aaa .web .WebContext ;
50
45
import org .opendaylight .aaa .web .servlet .jersey2 .JerseyServletSupport ;
51
46
import org .opendaylight .mdsal .binding .api .DataBroker ;
52
47
import org .opendaylight .yang .gen .v1 .urn .opendaylight .aaa .app .config .rev170619 .DatastoreConfig ;
53
48
import org .opendaylight .yang .gen .v1 .urn .opendaylight .aaa .app .config .rev170619 .ShiroConfiguration ;
54
49
import org .opendaylight .yang .gen .v1 .urn .opendaylight .aaa .password .service .config .rev170619 .PasswordServiceConfig ;
55
50
import org .opendaylight .yang .gen .v1 .urn .opendaylight .aaa .password .service .config .rev170619 .PasswordServiceConfigBuilder ;
51
+ import org .opendaylight .yangtools .concepts .Registration ;
56
52
import org .slf4j .Logger ;
57
53
import org .slf4j .LoggerFactory ;
58
54
@@ -62,7 +58,6 @@ public final class AAALightyShiroProvider {
62
58
63
59
private static AAALightyShiroProvider INSTANCE ;
64
60
65
- private final List <Handler > handlers ;
66
61
private final DataBroker dataBroker ;
67
62
private final ICertificateManager certificateManager ;
68
63
private final ShiroConfiguration shiroConfiguration ;
@@ -73,6 +68,8 @@ public final class AAALightyShiroProvider {
73
68
private ClaimCache claimCache ;
74
69
private PasswordHashService passwordHashService ;
75
70
private IIDMStore iidmStore ;
71
+ private Registration registration ;
72
+ private ShiroWebContextSecurer webContextSecurer ;
76
73
77
74
private AAAWebEnvironment aaaWebEnvironment ;
78
75
@@ -84,7 +81,6 @@ private AAALightyShiroProvider(final DataBroker dataBroker,
84
81
this .certificateManager = aaaConfiguration .getCertificateManager ();
85
82
this .credentialAuth = credentialAuth ;
86
83
this .shiroConfiguration = aaaConfiguration .getShiroConf ();
87
- this .handlers = new ArrayList <>();
88
84
this .authenticationService = new AuthenticationManager ();
89
85
final DatastoreConfig datastoreConfig = aaaConfiguration .getDatastoreConf ();
90
86
@@ -121,47 +117,60 @@ private AAALightyShiroProvider(final DataBroker dataBroker,
121
117
} catch (final IDMStoreException e ) {
122
118
LOG .error ("Failed to initialize data in store" , e );
123
119
}
124
- final LocalHttpServer httpService = new LocalHttpServer (server );
125
- registerServletContexts (httpService , aaaConfiguration .getMoonEndpointPath ());
126
-
127
120
initAAAonServer (server );
128
121
}
129
122
130
123
private void initAAAonServer (final LightyJettyServerProvider server ) {
131
- final ContextHandlerCollection contexts = new ContextHandlerCollection ();
132
- final ServletContextHandler mainHandler = new ServletContextHandler (contexts , "/auth" , true , false );
133
- final IdmLightApplication idmLightApplication = new IdmLightApplication (iidmStore , claimCache );
134
- final ServletHolder idmLightServlet = new ServletHolder (new ServletContainer (ResourceConfig .forApplication (
135
- idmLightApplication )));
136
- idmLightServlet .setInitParameter ("jersey.config.server.provider.packages" ,
137
- "org.opendaylight.aaa.impl.provider" );
138
- mainHandler .addServlet (idmLightServlet , "/*" );
139
- server .addContextHandler (contexts );
140
- this .handlers .add (contexts );
141
- this .handlers .add (mainHandler );
142
- this .aaaWebEnvironment = new AAAWebEnvironment (shiroConfiguration ,
143
- dataBroker ,
144
- certificateManager ,
145
- authenticationService ,
146
- tokenAuthenticators ,
147
- passwordHashService ,
148
- new JerseyServletSupport ());
149
-
150
124
final Map <String , String > properties = new HashMap <>();
151
125
final CustomFilterAdapterConfigurationImpl customFilterAdapterConfig =
152
- new CustomFilterAdapterConfigurationImpl ();
126
+ new CustomFilterAdapterConfigurationImpl ();
153
127
customFilterAdapterConfig .update (properties );
154
- final FilterHolder customFilterAdapter = new FilterHolder (new CustomFilterAdapter (customFilterAdapterConfig ));
155
- server .addCommonFilter (customFilterAdapter , "/*" );
156
128
157
- final FilterHolder shiroFilter = new FilterHolder (new AAAShiroFilter (aaaWebEnvironment ));
158
- server .addCommonFilter (shiroFilter , "/*" );
129
+ this .aaaWebEnvironment = new AAAWebEnvironment (
130
+ shiroConfiguration ,
131
+ dataBroker ,
132
+ certificateManager ,
133
+ authenticationService ,
134
+ tokenAuthenticators ,
135
+ passwordHashService ,
136
+ new JerseyServletSupport ());
137
+
138
+ DefaultWebSessionManager sessionManager = new DefaultWebSessionManager ();
139
+ ((DefaultSecurityManager ) aaaWebEnvironment .getSecurityManager ()).setSessionManager (sessionManager );
140
+
141
+ final var webContextBuilder = WebContext .builder ()
142
+ .name ("RealmManagement" )
143
+ .contextPath ("/auth" )
144
+ .supportsSessions (true )
159
145
160
- final FilterHolder crossOriginFilter = new FilterHolder (new CrossOriginFilter ());
161
- crossOriginFilter .setInitParameter ("allowedMethods" , "GET,POST,OPTIONS,DELETE,PUT,HEAD" );
162
- crossOriginFilter .setInitParameter ("allowedHeaders" ,
163
- "origin, content-type, accept, authorization, Authorization" );
164
- server .addCommonFilter (crossOriginFilter , "/*" );
146
+ // Add servlet
147
+ .addServlet (ServletDetails .builder ()
148
+ .servlet (new JerseyServletSupport ().createHttpServletBuilder (
149
+ new IdmLightApplication (iidmStore , claimCache )).build ())
150
+ .addUrlPattern ("/*" )
151
+ .build ())
152
+
153
+ // CustomFilterAdapter
154
+ .addFilter (FilterDetails .builder ()
155
+ .filter (new CustomFilterAdapter (customFilterAdapterConfig ))
156
+ .addUrlPattern ("/*" )
157
+ .build ())
158
+
159
+ // CORS filter
160
+ .addFilter (FilterDetails .builder ()
161
+ .filter (new CrossOriginFilter ())
162
+ .addUrlPattern ("/*" )
163
+ .putInitParam ("allowedMethods" , "GET,POST,OPTIONS,DELETE,PUT,HEAD" )
164
+ .putInitParam ("allowedHeaders" , "origin, content-type, accept, authorization, Authorization" )
165
+ .build ());
166
+ this .webContextSecurer = new ShiroWebContextSecurer (aaaWebEnvironment );
167
+ webContextSecurer .requireAuthentication (webContextBuilder , "/*" , "/moon/*" );
168
+
169
+ try {
170
+ this .registration = server .build ().registerWebContext (webContextBuilder .build ());
171
+ } catch (ServletException e ) {
172
+ LOG .error ("Failed to register AAA web context: {}!" , server .getClass (), e );
173
+ }
165
174
}
166
175
167
176
public static CompletableFuture <AAALightyShiroProvider > newInstance (final DataBroker dataBroker ,
@@ -225,6 +234,10 @@ public static IIDMStore getIdmStore() {
225
234
return INSTANCE .iidmStore ;
226
235
}
227
236
237
+ public ShiroWebContextSecurer getWebContextSecurer () {
238
+ return webContextSecurer ;
239
+ }
240
+
228
241
/**
229
242
* Set IDM data store, only used for test.
230
243
*
@@ -236,26 +249,13 @@ public static void setIdmStore(final IIDMStore store) {
236
249
237
250
@ SuppressWarnings ("IllegalCatch" )
238
251
public void close () {
239
- this .handlers .forEach ((handler ) -> {
240
- try {
241
- handler .stop ();
242
- } catch (Exception e ) {
243
- LOG .error ("Failed to close AAA handler [{}]" , handler , e );
244
- } finally {
245
- handler .destroy ();
246
- }
247
- });
252
+ if (registration != null ) {
253
+ registration .close ();
254
+ }
248
255
}
249
256
250
257
private static TokenAuthenticators buildTokenAuthenticators (
251
258
final CredentialAuth <PasswordCredentials > credentialAuth ) {
252
259
return new TokenAuthenticators (new HttpBasicAuth (credentialAuth ));
253
260
}
254
-
255
- private void registerServletContexts (final LocalHttpServer httpService , final String moonEndpointPath ) {
256
- LOG .info ("attempting registration of AAA moon and auth servlets" );
257
-
258
- Preconditions .checkNotNull (httpService , "httpService cannot be null" );
259
- httpService .registerServlet (moonEndpointPath , new MoonTokenEndpoint (), null );
260
- }
261
261
}
0 commit comments