Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5003b2fdf09be284678ba659e3e7443bb767cb16 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*******************************************************************************
 * Copyright (c) 2014 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.jaxrs.server.internal.security.oauth2.provider;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.ws.rs.core.Application;
import org.apache.cxf.rs.security.oauth2.grants.AbstractGrantHandler;
import org.apache.cxf.rs.security.oauth2.grants.clientcred.ClientCredentialsGrantHandler;
import org.apache.cxf.rs.security.oauth2.grants.code.AuthorizationCodeGrantHandler;
import org.apache.cxf.rs.security.oauth2.grants.code.CodeVerifierTransformer;
import org.apache.cxf.rs.security.oauth2.grants.code.DigestCodeVerifier;
import org.apache.cxf.rs.security.oauth2.grants.owner.ResourceOwnerGrantHandler;
import org.apache.cxf.rs.security.oauth2.grants.owner.ResourceOwnerLoginHandler;
import org.apache.cxf.rs.security.oauth2.grants.refresh.RefreshTokenGrantHandler;
import org.apache.cxf.rs.security.oauth2.provider.AccessTokenGrantHandler;
import org.apache.cxf.rs.security.oauth2.provider.AccessTokenValidator;
import org.apache.cxf.rs.security.oauth2.provider.OAuthDataProvider;
import org.apache.cxf.rs.security.oauth2.services.AbstractAccessTokenValidator;
import org.apache.cxf.rs.security.oauth2.services.AbstractOAuthService;
import org.apache.cxf.rs.security.oauth2.services.AbstractTokenService;
import org.apache.cxf.rs.security.oauth2.services.AccessTokenService;
import org.apache.cxf.rs.security.oauth2.services.AccessTokenValidatorService;
import org.apache.cxf.rs.security.oauth2.services.AuthorizationCodeGrantService;
import org.apache.cxf.rs.security.oauth2.services.ImplicitGrantService;
import org.apache.cxf.rs.security.oauth2.services.RedirectionBasedGrantService;
import org.apache.cxf.rs.security.oauth2.services.TokenRevocationService;
import org.apache.cxf.rs.security.oauth2.tokens.hawk.HawkAccessTokenValidator;
import org.apache.cxf.rs.security.oauth2.tokens.hawk.NonceStore;
import org.apache.cxf.rs.security.oauth2.tokens.hawk.NonceVerifier;
import org.apache.cxf.rs.security.oauth2.tokens.hawk.NonceVerifierImpl;
import org.eclipse.osee.jaxrs.server.internal.JaxRsConstants;
import org.eclipse.osee.jaxrs.server.internal.JaxRsResourceManager;
import org.eclipse.osee.jaxrs.server.internal.applications.JaxRsApplicationRegistry;
import org.eclipse.osee.jaxrs.server.internal.security.oauth2.provider.adapters.ClientProviderImpl;
import org.eclipse.osee.jaxrs.server.internal.security.oauth2.provider.adapters.OAuthEncryption;
import org.eclipse.osee.jaxrs.server.internal.security.oauth2.provider.adapters.SubjectProviderImpl;
import org.eclipse.osee.jaxrs.server.internal.security.oauth2.provider.endpoints.AbstractClientService;
import org.eclipse.osee.jaxrs.server.internal.security.oauth2.provider.endpoints.AuthorizationCodeEndpoint;
import org.eclipse.osee.jaxrs.server.internal.security.oauth2.provider.endpoints.ClientEndpoint;
import org.eclipse.osee.jaxrs.server.internal.security.oauth2.provider.endpoints.ClientRegistrationEndpoint;
import org.eclipse.osee.jaxrs.server.internal.security.oauth2.provider.endpoints.ImplicitGrantEndpoint;
import org.eclipse.osee.jaxrs.server.internal.security.oauth2.provider.writers.AuthorizationDataHtmlWriter;
import org.eclipse.osee.jaxrs.server.internal.security.oauth2.provider.writers.ClientRegistrationDataHtmlWriter;
import org.eclipse.osee.jaxrs.server.internal.security.oauth2.provider.writers.ClientRegistrationResponseHtmlWriter;
import org.eclipse.osee.jaxrs.server.internal.security.oauth2.provider.writers.OOBAuthorizationResponseHtmlWriter;
import org.eclipse.osee.jaxrs.server.security.JaxRsAuthenticator;
import org.eclipse.osee.jaxrs.server.security.JaxRsOAuth;
import org.eclipse.osee.jaxrs.server.security.JaxRsOAuthStorage;
import org.eclipse.osee.jaxrs.server.security.JaxRsSessionProvider;
import org.eclipse.osee.logger.Log;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

/**
 * @author Roberto E. Escobar
 */
public class OAuth2ServerProvider {

   private static final String OAUTH2_APPLICATION_COMPONENT_NAME = qualify("application");

   private final Set<String> registeredProviders = new HashSet<String>();
   private List<String> audiences;
   private OAuth2DataProvider dataProvider;
   private NonceVerifier nonceVerifier;

   private OAuth2RequestFilter filter;
   private List<AccessTokenGrantHandler> grantHandlers;
   private List<AccessTokenValidator> tokenValidators;
   private Set<Object> endpoints;
   private Application application;

   private Log logger;
   private JaxRsApplicationRegistry registry;
   private JaxRsResourceManager resourceManager;
   private JaxRsAuthenticator authenticator;
   private JaxRsSessionProvider sessionProvider;
   private JaxRsOAuthStorage storage;

   private final AtomicBoolean wasRegistered = new AtomicBoolean();

   public void setLogger(Log logger) {
      this.logger = logger;
   }

   public void setJaxRsApplicationRegistry(JaxRsApplicationRegistry registry) {
      this.registry = registry;
   }

   public void setJaxRsResourceManager(JaxRsResourceManager resourceManager) {
      this.resourceManager = resourceManager;
   }

   public void setJaxRsAuthenticator(JaxRsAuthenticator authenticator) {
      this.authenticator = authenticator;
   }

   public void setJaxRsSessionProvider(JaxRsSessionProvider sessionProvider) {
      this.sessionProvider = sessionProvider;
   }

   public void setJaxRsOAuthStorage(JaxRsOAuthStorage storage) {
      this.storage = storage;
   }

   private static String qualify(String name) {
      return String.format("%s.security.oauth2.%s", JaxRsConstants.NAMESPACE, name);
   }

   private Bundle bundle;

   public void start(BundleContext bundleContext, Map<String, Object> props) {
      bundle = bundleContext.getBundle();
      update(props);
   }

   public void stop() {
      if (wasRegistered.getAndSet(false)) {
         deregister(registry);

         audiences = null;
         dataProvider = null;
         nonceVerifier = null;
         filter = null;
         grantHandlers = null;
         tokenValidators = null;
         endpoints = null;
         application = null;
      }
   }

   private void initialize(OAuth2Configuration config) {
      SubjectProvider subjectProvider = new SubjectProviderImpl(logger, sessionProvider, authenticator);
      ClientProvider clientProvider = new ClientProviderImpl(subjectProvider, storage);

      audiences = Collections.emptyList();

      OAuthEncryption serializer = new OAuthEncryption();
      dataProvider = new OAuth2DataProvider(clientProvider, subjectProvider, serializer, storage);

      filter = new OAuth2RequestFilter(logger, resourceManager, subjectProvider);
      bind(filter, dataProvider);

      endpoints = new HashSet<Object>();
      endpoints.add(bind(new AccessTokenService(), dataProvider));
      endpoints.add(bind(new TokenRevocationService(), dataProvider));

      //@formatter:off
      endpoints.add(bind(new AuthorizationCodeEndpoint(clientProvider), dataProvider, subjectProvider));
      endpoints.add(bind(new ImplicitGrantEndpoint(clientProvider), dataProvider, subjectProvider));
      //@formatter:on

      endpoints.add(bind(new AccessTokenValidatorService(), dataProvider));
      endpoints.add(bind(new ClientRegistrationEndpoint(logger), clientProvider, subjectProvider));
      endpoints.add(bind(new ClientEndpoint(logger), clientProvider, subjectProvider));

      // Add OAuth2 application local Writers
      endpoints.add(new AuthorizationDataHtmlWriter());
      endpoints.add(new OOBAuthorizationResponseHtmlWriter());
      endpoints.add(new ClientRegistrationDataHtmlWriter());
      endpoints.add(new ClientRegistrationResponseHtmlWriter());

      application = new OAuth2Application(endpoints);

      grantHandlers = new ArrayList<AccessTokenGrantHandler>();
      grantHandlers.add(bind(new AuthorizationCodeGrantHandler(), dataProvider, new DigestCodeVerifier()));
      grantHandlers.add(bind(new ClientCredentialsGrantHandler(), dataProvider));
      grantHandlers.add(bind(new ResourceOwnerGrantHandler(), dataProvider, subjectProvider));
      grantHandlers.add(bind(new RefreshTokenGrantHandler(), dataProvider));

      tokenValidators = new ArrayList<AccessTokenValidator>();
      if (config.isHawkTokenSupported()) {
         NonceVerifierImpl nonceVerifier = new NonceVerifierImpl();
         NonceStore nonceStore = null;
         nonceVerifier.setNonceStore(nonceStore);
         HawkAccessTokenValidator validator = new HawkAccessTokenValidator();
         validator.setDataProvider(dataProvider);
         validator.setNonceVerifier(nonceVerifier);
         tokenValidators.add(validator);
         this.nonceVerifier = nonceVerifier;
      }
   }

   public void update(Map<String, Object> props) {
      OAuth2Configuration config = OAuth2Configuration.fromProperties(props);
      if (config.isEnabled()) {
         if (!wasRegistered.getAndSet(true)) {
            initialize(config);
            register(registry, bundle);
         }
         configure(config);
      } else {
         stop();
      }
   }

   private void register(JaxRsApplicationRegistry registry, Bundle bundle) {
      for (Object object : JaxRsOAuth.getOAuthProviders()) {
         addProvider(registry, bundle, qualify(object.getClass().getSimpleName()), object);
      }
      registry.register(OAUTH2_APPLICATION_COMPONENT_NAME, bundle, application);
      addProvider(registry, bundle, qualify("filter"), filter);
   }

   private void addProvider(JaxRsApplicationRegistry registry, Bundle bundle, String name, Object object) {
      registeredProviders.add(name);
      registry.registerProvider(name, bundle, object);
   }

   private void deregister(JaxRsApplicationRegistry registry) {
      registry.deregister(OAUTH2_APPLICATION_COMPONENT_NAME);
      for (String componentName : registeredProviders) {
         registry.deregisterProvider(componentName);
      }
      registeredProviders.clear();
   }

   private void configure(OAuth2Configuration config) {
      configure(config, filter);
      configure(config, dataProvider);
      configure(config, nonceVerifier);

      for (AccessTokenValidator validator : tokenValidators) {
         configureObject(config, validator);
      }
      for (AccessTokenGrantHandler handler : grantHandlers) {
         configureObject(config, handler);
      }
      for (Object endpoint : endpoints) {
         configureObject(config, endpoint);
      }
   }

   private void configure(OAuth2Configuration config, OAuth2DataProvider provider) {
      provider.setRefreshTokenAllowed(config.isRefreshTokenAllowed());
      provider.setCodeGrantExpiration(config.getCodeGrantExpiration());
      provider.setAccessTokenExpiration(config.getAccessTokenExpiration());
      provider.setRefreshTokenExpiration(config.getRefreshTokenExpiration());

      provider.setSecretKeyAlgorithm(config.getSecretKeyAlgorithm());
      provider.setSecretKeyEncoded(config.getEncodedSecretKey());

      configureObject(config, provider);
   }

   private void configure(OAuth2Configuration config, NonceVerifier object) {
      if (object instanceof NonceVerifierImpl) {
         NonceVerifierImpl nonceVerifier = (NonceVerifierImpl) object;
         nonceVerifier.setAllowedWindow(config.getNonceAllowedWindow());
      }
      configureObject(config, object);
   }

   private void configure(OAuth2Configuration config, OAuth2RequestFilter filter) {
      filter.setAudienceIsEndpointAddress(config.isAudienceIsEndpointAddress());
      filter.setUseUserSubject(config.isUseUserSubject());
      filter.setCheckFormData(config.isFilterChecksFormDataForToken());
      filter.setRealm(config.getRealm());

      filter.setRedirectURI(config.getLoginRedirectURI());
      filter.setIgnoreBasePath(config.isIgnoreLoginRedirectBasePath());
      filter.setRealm(config.getRealm());
      configureObject(config, filter);
   }

   private void configureObject(OAuth2Configuration config, Object object) {
      if (object instanceof AbstractGrantHandler) {
         AbstractGrantHandler handler = (AbstractGrantHandler) object;
         handler.setCanSupportPublicClients(config.isCanSupportPublicClients());
         handler.setPartialMatchScopeValidation(config.isPartialMatchScopeValidation());
      }

      if (object instanceof RefreshTokenGrantHandler) {
         RefreshTokenGrantHandler handler = (RefreshTokenGrantHandler) object;
         handler.setPartialMatchScopeValidation(config.isPartialMatchScopeValidation());
      }

      if (object instanceof AbstractAccessTokenValidator) {
         AbstractAccessTokenValidator validator = (AbstractAccessTokenValidator) object;
         validator.setRealm(config.getRealm());

         validator.setAudiences(audiences);
         validator.setTokenValidators(tokenValidators);
      }

      if (object instanceof AbstractOAuthService) {
         AbstractOAuthService service = (AbstractOAuthService) object;
         service.setBlockUnsecureRequests(config.isBlockUnsecureRequests());
         service.setWriteOptionalParameters(config.isWriteOptionalParameters());
      }

      if (object instanceof AbstractTokenService) {
         AbstractTokenService tokenService = (AbstractTokenService) object;
         tokenService.setCanSupportPublicClients(config.isCanSupportPublicClients());
         tokenService.setWriteCustomErrors(config.isWriteCustomErrors());
      }

      if (object instanceof RedirectionBasedGrantService) {
         RedirectionBasedGrantService redirectService = (RedirectionBasedGrantService) object;
         redirectService.setPartialMatchScopeValidation(config.isPartialMatchScopeValidation());
         redirectService.setUseRegisteredRedirectUriIfPossible(config.isUseRegisteredRedirectUriIfPossible());
      }

      if (object instanceof AuthorizationCodeGrantService) {
         AuthorizationCodeGrantService codeGrantService = (AuthorizationCodeGrantService) object;
         codeGrantService.setCanSupportPublicClients(config.isCanSupportPublicClients());
      }

      if (object instanceof ImplicitGrantService) {
         ImplicitGrantService implicitGrant = (ImplicitGrantService) object;
         implicitGrant.setReportClientId(config.isReportClientId());
      }

      if (object instanceof AccessTokenService) {
         AccessTokenService accessTokenService = (AccessTokenService) object;
         accessTokenService.setAudiences(audiences);
         accessTokenService.setGrantHandlers(grantHandlers);
      }

      if (object instanceof AbstractClientService) {
         AbstractClientService clientService = (AbstractClientService) object;
         clientService.setBlockUnsecureRequests(config.isBlockUnsecureRequests());
      }
   }

   private static AbstractClientService bind(AbstractClientService object, ClientProvider dataProvider, SubjectProvider subjectProvider) {
      object.setDataProvider(dataProvider);
      object.setResourceOwnerNameProvider(subjectProvider);
      object.setSessionAuthenticityTokenProvider(subjectProvider);
      object.setSubjectCreator(subjectProvider);
      return object;
   }

   private static AbstractAccessTokenValidator bind(AbstractAccessTokenValidator object, OAuthDataProvider dataProvider) {
      object.setDataProvider(dataProvider);
      return object;
   }

   private static AbstractOAuthService bind(RedirectionBasedGrantService object, OAuthDataProvider dataProvider, SubjectProvider subjectProvider) {
      object.setResourceOwnerNameProvider(subjectProvider);
      object.setSessionAuthenticityTokenProvider(subjectProvider);
      object.setSubjectCreator(subjectProvider);
      bind(object, dataProvider);
      return object;
   }

   private static AuthorizationCodeGrantHandler bind(AuthorizationCodeGrantHandler object, OAuthDataProvider dataProvider, CodeVerifierTransformer codeVerifier) {
      object.setCodeVerifierTransformer(codeVerifier);
      bind(object, dataProvider);
      return object;
   }

   private static ResourceOwnerGrantHandler bind(ResourceOwnerGrantHandler handler, OAuthDataProvider dataProvider, ResourceOwnerLoginHandler loginHandler) {
      handler.setLoginHandler(loginHandler);
      bind(handler, dataProvider);
      return handler;
   }

   private static AbstractOAuthService bind(AbstractOAuthService object, OAuthDataProvider dataProvider) {
      object.setDataProvider(dataProvider);
      return object;
   }

   private static RefreshTokenGrantHandler bind(RefreshTokenGrantHandler object, OAuthDataProvider dataProvider) {
      object.setDataProvider(dataProvider);
      return object;
   }

   private static AbstractGrantHandler bind(AbstractGrantHandler object, OAuthDataProvider dataProvider) {
      object.setDataProvider(dataProvider);
      return object;
   }

}

Back to the top