Skip to main content
summaryrefslogtreecommitdiffstats
blob: f4754999e80ebe0c6d7eb7a843abf14a782d86da (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
388
389
390
391
392
393
394
395
396
397
398
/*******************************************************************************
 * 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.client.internal.ext;

import static org.eclipse.osee.jaxrs.client.internal.ext.OAuth2Util.newException;
import static org.eclipse.osee.jaxrs.client.internal.ext.OAuth2Util.toException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status.Family;
import javax.ws.rs.core.Response.StatusType;
import javax.ws.rs.core.UriBuilder;
import org.apache.cxf.rs.security.oauth2.client.OAuthClientUtils;
import org.apache.cxf.rs.security.oauth2.client.OAuthClientUtils.Consumer;
import org.apache.cxf.rs.security.oauth2.common.AccessTokenValidation;
import org.apache.cxf.rs.security.oauth2.common.ClientAccessToken;
import org.apache.cxf.rs.security.oauth2.common.OAuthAuthorizationData;
import org.apache.cxf.rs.security.oauth2.common.OOBAuthorizationResponse;
import org.apache.cxf.rs.security.oauth2.utils.OAuthConstants;
import org.apache.cxf.rs.security.oauth2.utils.OAuthUtils;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.jdk.core.util.UrlQuery;
import org.eclipse.osee.jaxrs.client.JaxRsConfirmAccessHandler;
import org.eclipse.osee.jaxrs.client.JaxRsConfirmAccessHandler.ConfirmAccessRequest;
import org.eclipse.osee.jaxrs.client.JaxRsConfirmAccessHandler.ConfirmAccessResponse;
import org.eclipse.osee.jaxrs.client.JaxRsConfirmAccessHandler.Permission;

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

   public static interface OwnerCredentials {

      String getUsername();

      String getPassword();
   }

   private final OAuth2Transport transport;
   private final OwnerCredentials owner;

   private final Consumer client;

   private final String authorizeUri;
   private final String tokenUri;
   private final String tokenValidationUri;

   public OAuth2Flows(OAuth2Transport transport, OwnerCredentials owner, Consumer client, String authorizeUri, String tokenUri, String tokenValidationUri) {
      super();
      this.transport = transport;
      this.owner = owner;
      this.client = client;
      this.authorizeUri = authorizeUri;
      this.tokenUri = tokenUri;
      this.tokenValidationUri = tokenValidationUri;
   }

   public String generateState() {
      return OAuthUtils.generateRandomTokenKey();
   }

   public ClientAccessToken authorizationCodeFlow(JaxRsConfirmAccessHandler handler, String state, String scope, String redirectUri) {
      String sessionCookie = null;
      AuthFlowResponse response = requestAuthorizationGrant(state, scope, redirectUri);
      if (response.isAuthData()) {
         sessionCookie = response.getAuthenticityCookie();
         response = confirmAccess(handler, response.getAuthorizationData(), sessionCookie);
      }

      if (response.isOobAuthorization()) {
         ClientAccessToken token =
            exchangeCodeForToken(sessionCookie, response.getAuthorizationCode(), state, scope, redirectUri);
         if (Strings.isValid(tokenValidationUri)) {
            validateToken(response.getAuthenticityCookie(), token);
         }
         return token;
      } else {
         // could result in token right away if pre-authorized token exists
         throw newException("Auth flow is broken - invalid state");
      }
   }

   public ClientAccessToken refreshFlow(ClientAccessToken data, String state) {
      Map<String, String> extraParams = new HashMap<String, String>();
      if (data.getParameters() != null) {
         extraParams.putAll(data.getParameters());
      }
      extraParams.put(OAuthConstants.STATE, state);
      return transport.sendRefreshToken(owner, client, tokenUri, data.getRefreshToken(), data.getApprovedScope(),
         extraParams);
   }

   protected AuthFlowResponse requestAuthorizationGrant(String state, String scope, String redirectUri) {
      UriBuilder builder = OAuthClientUtils.getAuthorizationURIBuilder(authorizeUri, client.getKey(), scope);
      if (redirectUri != null) {
         builder.queryParam(OAuthConstants.REDIRECT_URI, redirectUri);
      }
      if (state != null) {
         builder.queryParam(OAuthConstants.STATE, state);
      }
      // confidential clients
      builder.queryParam(OAuthConstants.CLIENT_SECRET, client.getSecret());

      URI authorizationURI = builder.build();
      Response response = transport.sendAuthorizationCodeRequest(owner, authorizationURI);
      StatusType statusType = response.getStatusInfo();
      if (Family.SUCCESSFUL != statusType.getFamily()) {
         throw toException(response, "Error requesting authorization code");
      } else {
         OAuthAuthorizationData authData = null;
         try {
            response.bufferEntity();
            authData = response.readEntity(OAuthAuthorizationData.class);
         } catch (Exception ex) {
            try {
               response.bufferEntity();
               OOBAuthorizationResponse oob = response.readEntity(OOBAuthorizationResponse.class);
               return new AuthFlowResponse(response, oob.getAuthorizationCode());
            } catch (Exception ex1) {
               throw newException(ex1, "Unexpected data type error [%s]", response.getEntity());
            }
         }
         checkState(state, authData.getState());
         return new AuthFlowResponse(response, authData);
      }
   }

   protected AuthFlowResponse confirmAccess(JaxRsConfirmAccessHandler handler, OAuthAuthorizationData authData, String sessionCookie) {
      ConfirmAccessRequest request = newAccessConfirmRequest(authData);
      ConfirmAccessResponse ownerResponse = handler.onConfirmAccess(request);
      Form form = newAccessConfirmForm(authData, ownerResponse);
      String replyTo = authData.getReplyTo();

      Response response = transport.sendAccessConfirmation(owner, sessionCookie, replyTo, form);
      StatusType statusType = response.getStatusInfo();
      if (Family.SUCCESSFUL != statusType.getFamily() && Family.REDIRECTION != statusType.getFamily()) {
         throw toException(response, "Error requesting access confirmation");
      } else {
         String code = null;
         URI location = response.getLocation();
         if (location != null) {
            UrlQuery query = new UrlQuery();
            try {
               query.parse(location.getQuery());
               code = query.getParameter(OAuthConstants.AUTHORIZATION_CODE_VALUE);
               checkState(authData.getState(), query.getParameter(OAuthConstants.STATE));
            } catch (UnsupportedEncodingException ex) {
               throw newException(ex, "Exception while parsing auth code URI [%s]", location);
            }
         } else {
            try {
               response.bufferEntity();
               OOBAuthorizationResponse oob = response.readEntity(OOBAuthorizationResponse.class);
               code = oob.getAuthorizationCode();
            } catch (Exception ex) {
               String value = response.readEntity(String.class);
               throw newException(ex, "Unexpected data type error [%s]", value);
            }
         }
         return new AuthFlowResponse(response, code);
      }
   }

   protected ClientAccessToken exchangeCodeForToken(String sessionCookie, String authCode, String state, String scope, String redirectUri) {
      Map<String, String> extraParams = new HashMap<String, String>();
      if (state != null) {
         extraParams.put(OAuthConstants.STATE, state);
      }
      if (scope != null) {
         extraParams.put(OAuthConstants.SCOPE, scope);
      }
      // client must fully authenticate with token end-point
      try {
         return transport.sendAuthorizationCodeGrant(owner, client, sessionCookie, tokenUri, authCode, redirectUri,
            extraParams);
      } catch (Exception ex) {
         throw newException(ex, "Error exchanging authorization grant for access token");
      }
   }

   protected void validateToken(String sessionCookie, ClientAccessToken token) {
      Form form = new Form();
      form.param(OAuthConstants.AUTHORIZATION_SCHEME_TYPE, token.getTokenType());
      form.param(OAuthConstants.AUTHORIZATION_SCHEME_DATA, token.getTokenKey());
      try {
         AccessTokenValidation validation =
            transport.sendTokenValidationRequest(owner, client, sessionCookie, tokenValidationUri, form);
         assertTokenEquals(token, validation);
      } catch (Exception ex) {
         throw newException(ex, "Token validation failed");
      }
   }

   private static void assertTokenEquals(ClientAccessToken expected, AccessTokenValidation actual) {
      if (!(//
      equals(expected.getTokenKey(), actual.getTokenKey()) // 
         && equals(expected.getTokenType(), actual.getTokenType()) //
         && equals(expected.getExpiresIn(), actual.getTokenLifetime()) //
      && equals(expected.getIssuedAt(), actual.getTokenIssuedAt()) //
      )) {
         throw newException("Token validation failed");
      }
   }

   private static void checkState(String expected, String actual) {
      if (!equals(expected, actual)) {
         throw newException("OAuth Authorization Flow - Expected state [%s] did not match response state [%s]",
            expected, actual);
      }
   }

   private static boolean equals(Object a, Object b) {
      if (a == null && b == null) {
         return true;
      } else if (a != null && b != null) {
         return a.equals(b);
      }
      return false;
   }

   protected static Form newAccessConfirmForm(OAuthAuthorizationData data, ConfirmAccessResponse ownerResponse) {
      Form form = new Form();
      form.param(OAuthConstants.CLIENT_ID, data.getClientId());
      form.param(OAuthConstants.CLIENT_AUDIENCE, data.getAudience());
      form.param(OAuthConstants.SESSION_AUTHENTICITY_TOKEN, data.getAuthenticityToken());
      form.param(OAuthConstants.REDIRECT_URI, data.getRedirectUri());
      form.param(OAuthConstants.STATE, data.getState());
      form.param(OAuthConstants.SCOPE, data.getProposedScope());

      List<? extends Permission> permissions = ownerResponse.getPermissionsGranted();
      if (permissions != null) {
         for (Permission permission : permissions) {
            String name = String.format("%s_status", permission.getName());
            form.param(name, OAuthConstants.AUTHORIZATION_DECISION_ALLOW);
         }
      }
      form.param(
         OAuthConstants.AUTHORIZATION_DECISION_KEY,
         ownerResponse.isGranted() ? OAuthConstants.AUTHORIZATION_DECISION_ALLOW : OAuthConstants.AUTHORIZATION_DECISION_DENY);
      return form;
   }

   protected static ConfirmAccessRequest newAccessConfirmRequest(OAuthAuthorizationData data) {
      return new ConfirmAccessRequestWrapper(data);
   }

   private static final class ConfirmAccessRequestWrapper implements ConfirmAccessRequest {
      private final OAuthAuthorizationData data;
      private List<Permission> permissions;

      public ConfirmAccessRequestWrapper(OAuthAuthorizationData data) {
         super();
         this.data = data;
      }

      @Override
      public List<? extends Permission> getPermissionsRequested() {
         if (permissions == null) {
            List<Permission> permissions = new ArrayList<Permission>();
            for (org.apache.cxf.rs.security.oauth2.common.Permission perm : data.getPermissions()) {
               permissions.add(asPermission(perm));
            }
            this.permissions = Collections.unmodifiableList(permissions);
         }
         return permissions;
      }

      @Override
      public String getEndUserName() {
         return data.getEndUserName();
      }

      @Override
      public String getApplicationWebUri() {
         return data.getApplicationWebUri();
      }

      @Override
      public String getApplicationName() {
         return data.getApplicationName();
      }

      @Override
      public String getApplicationLogoUri() {
         return data.getApplicationLogoUri();
      }

      @Override
      public String getApplicationDescription() {
         return data.getApplicationDescription();
      }

      @Override
      public String toString() {
         return "ConfirmAccessRequest [data=" + data + "]";
      }
   };

   protected static Permission asPermission(final org.apache.cxf.rs.security.oauth2.common.Permission perm) {
      return new PermissionWrapper(perm);
   }

   private static final class PermissionWrapper implements Permission {
      private final org.apache.cxf.rs.security.oauth2.common.Permission perm;

      public PermissionWrapper(org.apache.cxf.rs.security.oauth2.common.Permission perm) {
         super();
         this.perm = perm;
      }

      @Override
      public String getName() {
         return perm.getPermission();
      }

      @Override
      public String getDescription() {
         return perm.getDescription();
      }

      @Override
      public boolean isDefault() {
         return perm.isDefault();
      }

      @Override
      public String toString() {
         return "Permission [name=" + getName() + ", description=" + getDescription() + ", isDefault=" + isDefault() + "]";
      }
   }

   protected static final class AuthFlowResponse {
      private final OAuthAuthorizationData authData;
      private final String authCode;
      private final Response response;
      private final boolean isAuthData;
      private final boolean isOobAuthorization;

      public AuthFlowResponse(Response response, OAuthAuthorizationData authData) {
         super();
         this.response = response;
         this.isAuthData = true;
         this.isOobAuthorization = false;
         this.authData = authData;
         this.authCode = null;
      }

      public AuthFlowResponse(Response response, String authCode) {
         super();
         this.response = response;
         this.authCode = authCode;
         this.isAuthData = false;
         this.isOobAuthorization = true;
         this.authData = null;
      }

      public String getAuthorizationCode() {
         return authCode;
      }

      public String getAuthenticityCookie() {
         return (String) response.getMetadata().getFirst("Set-Cookie");
      }

      public boolean isAuthData() {
         return isAuthData;
      }

      public boolean isOobAuthorization() {
         return isOobAuthorization;
      }

      public OAuthAuthorizationData getAuthorizationData() {
         return authData;
      }

      @SuppressWarnings({"unchecked"})
      public <T> T getData(Class<T> clazz) {
         return (T) response.getEntity();
      }

   }
}

Back to the top