44
44
import java .util .concurrent .ConcurrentHashMap ;
45
45
import java .util .concurrent .ExecutorService ;
46
46
import java .util .concurrent .atomic .AtomicReference ;
47
+ import java .util .function .Function ;
47
48
import java .util .stream .Collectors ;
48
49
49
50
/**
54
55
* @author Graeme Rocher
55
56
* @since 1.0
56
57
* @param <T> Request Context Type
57
- * @param <I> Authentication Request Identity Type
58
- * @param <S> Authentication Request Secret Type
59
58
*/
60
59
@ Singleton
61
- public class Authenticator <T , I , S > {
60
+ public class Authenticator <T > {
62
61
63
62
private static final Logger LOG = LoggerFactory .getLogger (Authenticator .class );
64
63
@@ -69,10 +68,10 @@ public class Authenticator<T, I, S> {
69
68
@ Deprecated (forRemoval = true , since = "4.5.0" )
70
69
protected final Collection <io .micronaut .security .authentication .AuthenticationProvider <T >> authenticationProviders ;
71
70
72
- private final List <ReactiveAuthenticationProvider <T , I , S >> reactiveAuthenticationProviders ;
71
+ private final List <ReactiveAuthenticationProvider <T , ?, ? >> reactiveAuthenticationProviders ;
73
72
private final BeanContext beanContext ;
74
73
75
- private final List <AuthenticationProvider <T , I , S >> imperativeAuthenticationProviders ;
74
+ private final List <AuthenticationProvider <T , ?, ? >> imperativeAuthenticationProviders ;
76
75
private final SecurityConfiguration securityConfiguration ;
77
76
78
77
private final Map <String , Scheduler > executeNameToScheduler = new ConcurrentHashMap <>();
@@ -87,15 +86,15 @@ public class Authenticator<T, I, S> {
87
86
*/
88
87
@ Inject
89
88
public Authenticator (BeanContext beanContext ,
90
- List <ReactiveAuthenticationProvider <T , I , S >> reactiveAuthenticationProviders ,
91
- List <AuthenticationProvider <T , I , S >> authenticationProviders ,
89
+ List <ReactiveAuthenticationProvider <T , ?, ? >> reactiveAuthenticationProviders ,
90
+ List <AuthenticationProvider <T , ?, ? >> authenticationProviders ,
92
91
List <io .micronaut .security .authentication .AuthenticationProvider <T >> deprecatedAuthenticationProviders ,
93
92
SecurityConfiguration securityConfiguration ) {
94
93
this .beanContext = beanContext ;
95
94
this .reactiveAuthenticationProviders = reactiveAuthenticationProviders ;
96
- this . reactiveAuthenticationProviders . addAll ( deprecatedAuthenticationProviders . stream ()
97
- . map ( ap -> ( ReactiveAuthenticationProvider < T , I , S >) new ReactiveAuthenticationProviderAdapter <T , I , S >( ap ))
98
- . toList ());
95
+ for ( io . micronaut . security . authentication . AuthenticationProvider < T > authenticationProvider : deprecatedAuthenticationProviders ) {
96
+ reactiveAuthenticationProviders . add (( new ReactiveAuthenticationProviderAdapter <>( authenticationProvider )));
97
+ }
99
98
this .securityConfiguration = securityConfiguration ;
100
99
this .imperativeAuthenticationProviders = authenticationProviders ;
101
100
this .authenticationProviders = Collections .emptyList ();
@@ -108,10 +107,9 @@ public Authenticator(BeanContext beanContext,
108
107
* @param securityConfiguration The security configuration
109
108
* @deprecated Use {@link Authenticator#Authenticator(BeanContext, List, List, List, SecurityConfiguration)} instead.
110
109
*/
111
- @ Deprecated
112
110
public Authenticator (BeanContext beanContext ,
113
- List <ReactiveAuthenticationProvider <T , I , S >> reactiveAuthenticationProviders ,
114
- List <AuthenticationProvider <T , I , S >> authenticationProviders ,
111
+ List <ReactiveAuthenticationProvider <T , ?, ? >> reactiveAuthenticationProviders ,
112
+ List <AuthenticationProvider <T , ?, ? >> authenticationProviders ,
115
113
SecurityConfiguration securityConfiguration ) {
116
114
this .beanContext = beanContext ;
117
115
this .reactiveAuthenticationProviders = reactiveAuthenticationProviders ;
@@ -121,18 +119,19 @@ public Authenticator(BeanContext beanContext,
121
119
}
122
120
123
121
/**
124
- * @param authenticationProviders A list of available authentication providers
122
+ * @param deprecatedAuthenticationProviders A list of available authentication providers
125
123
* @param securityConfiguration The security configuration
126
124
* @deprecated Use {@link Authenticator#Authenticator(BeanContext, List, List, SecurityConfiguration)} instead.
127
125
*/
128
126
@ Deprecated (forRemoval = true , since = "4.5.0" )
129
- public Authenticator (Collection <io .micronaut .security .authentication .AuthenticationProvider <T >> authenticationProviders ,
127
+ public Authenticator (Collection <io .micronaut .security .authentication .AuthenticationProvider <T >> deprecatedAuthenticationProviders ,
130
128
SecurityConfiguration securityConfiguration ) {
131
129
this .beanContext = null ;
132
- this .authenticationProviders = authenticationProviders ;
133
- this .reactiveAuthenticationProviders = authenticationProviders .stream ()
134
- .map (ap -> (ReactiveAuthenticationProvider <T , I , S >) new ReactiveAuthenticationProviderAdapter <T , I , S >(ap ))
135
- .toList ();
130
+ this .authenticationProviders = deprecatedAuthenticationProviders ;
131
+ reactiveAuthenticationProviders = new ArrayList <>();
132
+ for (io .micronaut .security .authentication .AuthenticationProvider <T > authenticationProvider : deprecatedAuthenticationProviders ) {
133
+ reactiveAuthenticationProviders .add ((new ReactiveAuthenticationProviderAdapter <>(authenticationProvider )));
134
+ }
136
135
this .securityConfiguration = securityConfiguration ;
137
136
this .imperativeAuthenticationProviders = Collections .emptyList ();
138
137
}
@@ -144,7 +143,7 @@ public Authenticator(Collection<io.micronaut.security.authentication.Authenticat
144
143
* @param authenticationRequest Represents a request to authenticate.
145
144
* @return A publisher that emits {@link AuthenticationResponse} objects
146
145
*/
147
- public Publisher <AuthenticationResponse > authenticate (T requestContext , AuthenticationRequest <I , S > authenticationRequest ) {
146
+ public Publisher <AuthenticationResponse > authenticate (T requestContext , AuthenticationRequest <?, ? > authenticationRequest ) {
148
147
if (CollectionUtils .isEmpty (reactiveAuthenticationProviders ) && CollectionUtils .isEmpty (imperativeAuthenticationProviders )) {
149
148
return Mono .empty ();
150
149
}
@@ -179,8 +178,8 @@ protected boolean isImperativeAuthenticationProviderIsBlocking(AuthenticationPro
179
178
180
179
@ NonNull
181
180
private AuthenticationResponse authenticate (@ NonNull T requestContext ,
182
- @ NonNull AuthenticationRequest <I , S > authenticationRequest ,
183
- @ NonNull List <AuthenticationProvider <T , I , S >> authenticationProviders ,
181
+ @ NonNull AuthenticationRequest <?, ? > authenticationRequest ,
182
+ @ NonNull List <AuthenticationProvider <T , ?, ? >> authenticationProviders ,
184
183
@ Nullable SecurityConfiguration securityConfiguration ) {
185
184
if (securityConfiguration != null && securityConfiguration .getAuthenticationProviderStrategy () == AuthenticationStrategy .ALL ) {
186
185
return authenticateAll (requestContext , authenticationRequest , authenticationProviders );
@@ -194,8 +193,8 @@ private AuthenticationResponse authenticate(@NonNull T requestContext,
194
193
195
194
@ NonNull
196
195
private AuthenticationResponse authenticateAll (@ NonNull T requestContext ,
197
- @ NonNull AuthenticationRequest <I , S > authenticationRequest ,
198
- @ NonNull List <AuthenticationProvider <T , I , S >> authenticationProviders ) {
196
+ @ NonNull AuthenticationRequest <?, ? > authenticationRequest ,
197
+ @ NonNull List <AuthenticationProvider <T , ?, ? >> authenticationProviders ) {
199
198
List <AuthenticationResponse > authenticationResponses = authenticationProviders .stream ()
200
199
.map (provider -> authenticationResponse (provider , requestContext , authenticationRequest ))
201
200
.toList ();
@@ -207,8 +206,8 @@ private AuthenticationResponse authenticateAll(@NonNull T requestContext,
207
206
: AuthenticationResponse .failure ();
208
207
}
209
208
210
- private List <ReactiveAuthenticationProvider <T , I , S >> everyProviderSorted () {
211
- List <ReactiveAuthenticationProvider <T , I , S >> providers = new ArrayList <>(reactiveAuthenticationProviders );
209
+ private List <ReactiveAuthenticationProvider <T , ?, ? >> everyProviderSorted () {
210
+ List <ReactiveAuthenticationProvider <T , ?, ? >> providers = new ArrayList <>(reactiveAuthenticationProviders );
212
211
if (beanContext != null ) {
213
212
providers .addAll (imperativeAuthenticationProviders .stream ()
214
213
.map (imperativeAuthenticationProvider -> {
@@ -227,8 +226,8 @@ private List<ReactiveAuthenticationProvider<T, I, S>> everyProviderSorted() {
227
226
}
228
227
229
228
private Publisher <AuthenticationResponse > authenticate (T request ,
230
- AuthenticationRequest < I , S > authenticationRequest ,
231
- List <ReactiveAuthenticationProvider <T , I , S >> providers ) {
229
+ AuthenticationRequest authenticationRequest ,
230
+ List <ReactiveAuthenticationProvider <T , ?, ? >> providers ) {
232
231
if (providers == null ) {
233
232
return Flux .empty ();
234
233
}
@@ -240,9 +239,11 @@ private Publisher<AuthenticationResponse> authenticate(T request,
240
239
241
240
return Flux .mergeDelayError (1 ,
242
241
providers .stream ()
243
- .map (provider -> Flux .from (provider .authenticate (request , authenticationRequest ))
244
- .switchMap (Authenticator ::handleResponse )
245
- .switchIfEmpty (Flux .error (() -> new AuthenticationException ("Provider did not respond. Authentication rejected" ))))
242
+ .map (provider ->
243
+ Flux .from (provider .authenticate (request , authenticationRequest ))
244
+ .switchMap (rsp -> Authenticator .handleResponse ((AuthenticationResponse ) rsp ))
245
+ .switchIfEmpty (Flux .error (() -> new AuthenticationException ("Provider did not respond. Authentication rejected" )))
246
+ )
246
247
.toList ()
247
248
.toArray (emptyArr ))
248
249
.last ()
@@ -251,12 +252,13 @@ private Publisher<AuthenticationResponse> authenticate(T request,
251
252
} else {
252
253
AtomicReference <Throwable > lastError = new AtomicReference <>();
253
254
Flux <AuthenticationResponse > authentication = Flux .mergeDelayError (1 , providers .stream ()
254
- .map (auth -> auth .authenticate (request , authenticationRequest ))
255
- .map (Flux ::from )
256
- .map (sequence -> sequence .switchMap (Authenticator ::handleResponse ).onErrorResume (t -> {
257
- lastError .set (t );
258
- return Flux .empty ();
259
- })).toList ()
255
+ .map (auth -> Flux .from (auth .authenticate (request , authenticationRequest )))
256
+ .map (sequence -> sequence .switchMap (rsp -> Authenticator .handleResponse ((AuthenticationResponse ) rsp ))
257
+ .onErrorResume ((Function <Throwable , Publisher >) t -> {
258
+ lastError .set (t );
259
+ return Flux .empty ();
260
+ })
261
+ ).toList ()
260
262
.toArray (emptyArr ));
261
263
262
264
return authentication .take (1 )
@@ -290,9 +292,9 @@ private static Mono<AuthenticationResponse> handleResponse(AuthenticationRespons
290
292
}
291
293
292
294
@ NonNull
293
- private AuthenticationResponse authenticationResponse (@ NonNull AuthenticationProvider <T , I , S > provider ,
295
+ private AuthenticationResponse authenticationResponse (@ NonNull AuthenticationProvider <T , ?, ? > provider ,
294
296
@ NonNull T requestContext ,
295
- @ NonNull AuthenticationRequest < I , S > authenticationRequest ) {
297
+ @ NonNull AuthenticationRequest authenticationRequest ) {
296
298
try {
297
299
return provider .authenticate (requestContext , authenticationRequest );
298
300
} catch (Exception t ) {
0 commit comments