001/* 002 * oauth2-oidc-sdk 003 * 004 * Copyright 2012-2016, Connect2id Ltd and contributors. 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use 007 * this file except in compliance with the License. You may obtain a copy of the 008 * License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software distributed 013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 015 * specific language governing permissions and limitations under the License. 016 */ 017 018package com.nimbusds.oauth2.sdk; 019 020 021import java.net.URI; 022import java.net.URISyntaxException; 023import java.util.*; 024 025import net.jcip.annotations.Immutable; 026 027import com.nimbusds.jwt.JWT; 028import com.nimbusds.jwt.JWTClaimsSet; 029import com.nimbusds.jwt.JWTParser; 030import com.nimbusds.jwt.SignedJWT; 031import com.nimbusds.oauth2.sdk.http.HTTPRequest; 032import com.nimbusds.oauth2.sdk.id.ClientID; 033import com.nimbusds.oauth2.sdk.id.State; 034import com.nimbusds.oauth2.sdk.pkce.CodeChallenge; 035import com.nimbusds.oauth2.sdk.pkce.CodeChallengeMethod; 036import com.nimbusds.oauth2.sdk.pkce.CodeVerifier; 037import com.nimbusds.oauth2.sdk.util.*; 038import com.nimbusds.openid.connect.sdk.Prompt; 039 040 041/** 042 * Authorisation request. Used to authenticate an end-user and request the 043 * end-user's consent to grant the client access to a protected resource. 044 * Supports custom request parameters. 045 * 046 * <p>Extending classes may define additional request parameters as well as 047 * enforce tighter requirements on the base parameters. 048 * 049 * <p>Example HTTP request: 050 * 051 * <pre> 052 * https://server.example.com/authorize? 053 * response_type=code 054 * &client_id=s6BhdRkqt3 055 * &state=xyz 056 * &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb 057 * </pre> 058 * 059 * <p>Related specifications: 060 * 061 * <ul> 062 * <li>OAuth 2.0 (RFC 6749), sections 4.1.1 and 4.2.1. 063 * <li>OAuth 2.0 Multiple Response Type Encoding Practices 1.0. 064 * <li>OAuth 2.0 Form Post Response Mode 1.0. 065 * <li>Proof Key for Code Exchange by OAuth Public Clients (RFC 7636). 066 * <li>Resource Indicators for OAuth 2.0 (RFC 8707) 067 * <li>OAuth 2.0 Incremental Authorization 068 * (draft-ietf-oauth-incremental-authz-04) 069 * <li>The OAuth 2.0 Authorization Framework: JWT Secured Authorization 070 * Request (JAR) draft-ietf-oauth-jwsreq-29 071 * <li>Financial-grade API: JWT Secured Authorization Response Mode for 072 * OAuth 2.0 (JARM) 073 * </ul> 074 */ 075@Immutable 076public class AuthorizationRequest extends AbstractRequest { 077 078 079 /** 080 * The registered parameter names. 081 */ 082 private static final Set<String> REGISTERED_PARAMETER_NAMES; 083 084 085 static { 086 Set<String> p = new HashSet<>(); 087 088 p.add("response_type"); 089 p.add("client_id"); 090 p.add("redirect_uri"); 091 p.add("scope"); 092 p.add("state"); 093 p.add("response_mode"); 094 p.add("code_challenge"); 095 p.add("code_challenge_method"); 096 p.add("resource"); 097 p.add("include_granted_scopes"); 098 p.add("request_uri"); 099 p.add("request"); 100 p.add("prompt"); 101 102 REGISTERED_PARAMETER_NAMES = Collections.unmodifiableSet(p); 103 } 104 105 106 /** 107 * The response type (required unless in JAR). 108 */ 109 private final ResponseType rt; 110 111 112 /** 113 * The client identifier (required). 114 */ 115 private final ClientID clientID; 116 117 118 /** 119 * The redirection URI where the response will be sent (optional). 120 */ 121 private final URI redirectURI; 122 123 124 /** 125 * The scope (optional). 126 */ 127 private final Scope scope; 128 129 130 /** 131 * The opaque value to maintain state between the request and the 132 * callback (recommended). 133 */ 134 private final State state; 135 136 137 /** 138 * The response mode (optional). 139 */ 140 private final ResponseMode rm; 141 142 143 /** 144 * The authorisation code challenge for PKCE (optional). 145 */ 146 private final CodeChallenge codeChallenge; 147 148 149 /** 150 * The authorisation code challenge method for PKCE (optional). 151 */ 152 private final CodeChallengeMethod codeChallengeMethod; 153 154 155 /** 156 * The resource URI(s) (optional). 157 */ 158 private final List<URI> resources; 159 160 161 /** 162 * Indicates incremental authorisation (optional). 163 */ 164 private final boolean includeGrantedScopes; 165 166 167 /** 168 * Request object (optional). 169 */ 170 private final JWT requestObject; 171 172 173 /** 174 * Request object URI (optional). 175 */ 176 private final URI requestURI; 177 178 179 /** 180 * The requested prompt (optional). 181 */ 182 protected final Prompt prompt; 183 184 185 /** 186 * Custom parameters. 187 */ 188 private final Map<String,List<String>> customParams; 189 190 191 /** 192 * Builder for constructing authorisation requests. 193 */ 194 public static class Builder { 195 196 197 /** 198 * The endpoint URI (optional). 199 */ 200 private URI uri; 201 202 203 /** 204 * The response type (required unless in JAR). 205 */ 206 private ResponseType rt; 207 208 209 /** 210 * The client identifier (required). 211 */ 212 private final ClientID clientID; 213 214 215 /** 216 * The redirection URI where the response will be sent 217 * (optional). 218 */ 219 private URI redirectURI; 220 221 222 /** 223 * The scope (optional). 224 */ 225 private Scope scope; 226 227 228 /** 229 * The opaque value to maintain state between the request and 230 * the callback (recommended). 231 */ 232 private State state; 233 234 235 /** 236 * The response mode (optional). 237 */ 238 private ResponseMode rm; 239 240 241 /** 242 * The authorisation code challenge for PKCE (optional). 243 */ 244 private CodeChallenge codeChallenge; 245 246 247 /** 248 * The authorisation code challenge method for PKCE (optional). 249 */ 250 private CodeChallengeMethod codeChallengeMethod; 251 252 253 /** 254 * Indicates incremental authorisation (optional). 255 */ 256 private boolean includeGrantedScopes; 257 258 259 /** 260 * The resource URI(s) (optional). 261 */ 262 private List<URI> resources; 263 264 265 /** 266 * Request object (optional). 267 */ 268 private JWT requestObject; 269 270 271 /** 272 * Request object URI (optional). 273 */ 274 private URI requestURI; 275 276 277 /** 278 * The requested prompt (optional). 279 */ 280 private Prompt prompt; 281 282 283 /** 284 * Custom parameters. 285 */ 286 private final Map<String,List<String>> customParams = new HashMap<>(); 287 288 289 /** 290 * Creates a new authorisation request builder. 291 * 292 * @param rt The response type. Corresponds to the 293 * {@code response_type} parameter. Must not be 294 * {@code null}. 295 * @param clientID The client identifier. Corresponds to the 296 * {@code client_id} parameter. Must not be 297 * {@code null}. 298 */ 299 public Builder(final ResponseType rt, final ClientID clientID) { 300 301 if (rt == null) 302 throw new IllegalArgumentException("The response type must not be null"); 303 304 this.rt = rt; 305 306 307 if (clientID == null) 308 throw new IllegalArgumentException("The client ID must not be null"); 309 310 this.clientID = clientID; 311 } 312 313 314 /** 315 * Creates a new JWT secured authorisation request (JAR) 316 * builder. 317 * 318 * @param requestObject The request object. Must not be 319 * {@code null}.' 320 * @param clientID The client ID. Must not be 321 * {@code null}. 322 */ 323 public Builder(final JWT requestObject, final ClientID clientID) { 324 325 if (requestObject == null) 326 throw new IllegalArgumentException("The request object must not be null"); 327 328 this.requestObject = requestObject; 329 330 if (clientID == null) 331 throw new IllegalArgumentException("The client ID must not be null"); 332 333 this.clientID = clientID; 334 } 335 336 337 /** 338 * Creates a new JWT secured authorisation request (JAR) 339 * builder. 340 * 341 * @param requestURI The request object URI. Must not be 342 * {@code null}. 343 * @param clientID The client ID. Must not be {@code null}. 344 */ 345 public Builder(final URI requestURI, final ClientID clientID) { 346 347 if (requestURI == null) 348 throw new IllegalArgumentException("The request URI must not be null"); 349 350 this.requestURI = requestURI; 351 352 if (clientID == null) 353 throw new IllegalArgumentException("The client ID must not be null"); 354 355 this.clientID = clientID; 356 } 357 358 359 /** 360 * Creates a new authorisation request builder from the 361 * specified request. 362 * 363 * @param request The authorisation request. Must not be 364 * {@code null}. 365 */ 366 public Builder(final AuthorizationRequest request) { 367 368 uri = request.getEndpointURI(); 369 scope = request.scope; 370 rt = request.getResponseType(); 371 clientID = request.getClientID(); 372 redirectURI = request.getRedirectionURI(); 373 state = request.getState(); 374 rm = request.getResponseMode(); 375 codeChallenge = request.getCodeChallenge(); 376 codeChallengeMethod = request.getCodeChallengeMethod(); 377 resources = request.getResources(); 378 includeGrantedScopes = request.includeGrantedScopes(); 379 requestObject = request.requestObject; 380 requestURI = request.requestURI; 381 prompt = request.prompt; 382 customParams.putAll(request.getCustomParameters()); 383 } 384 385 386 /** 387 * Sets the response type. Corresponds to the 388 * {@code response_type} parameter. 389 * 390 * @param rt The response type. Must not be {@code null}. 391 * 392 * @return This builder. 393 */ 394 public Builder responseType(final ResponseType rt) { 395 396 if (rt == null) 397 throw new IllegalArgumentException("The response type must not be null"); 398 399 this.rt = rt; 400 return this; 401 } 402 403 404 /** 405 * Sets the redirection URI. Corresponds to the optional 406 * {@code redirection_uri} parameter. 407 * 408 * @param redirectURI The redirection URI, {@code null} if not 409 * specified. 410 * 411 * @return This builder. 412 */ 413 public Builder redirectionURI(final URI redirectURI) { 414 415 this.redirectURI = redirectURI; 416 return this; 417 } 418 419 420 /** 421 * Sets the scope. Corresponds to the optional {@code scope} 422 * parameter. 423 * 424 * @param scope The scope, {@code null} if not specified. 425 * 426 * @return This builder. 427 */ 428 public Builder scope(final Scope scope) { 429 430 this.scope = scope; 431 return this; 432 } 433 434 435 /** 436 * Sets the state. Corresponds to the recommended {@code state} 437 * parameter. 438 * 439 * @param state The state, {@code null} if not specified. 440 * 441 * @return This builder. 442 */ 443 public Builder state(final State state) { 444 445 this.state = state; 446 return this; 447 } 448 449 450 /** 451 * Sets the response mode. Corresponds to the optional 452 * {@code response_mode} parameter. Use of this parameter is 453 * not recommended unless a non-default response mode is 454 * requested (e.g. form_post). 455 * 456 * @param rm The response mode, {@code null} if not specified. 457 * 458 * @return This builder. 459 */ 460 public Builder responseMode(final ResponseMode rm) { 461 462 this.rm = rm; 463 return this; 464 } 465 466 467 /** 468 * Sets the code challenge for Proof Key for Code Exchange 469 * (PKCE) by public OAuth clients. 470 * 471 * @param codeChallenge The code challenge, {@code null} 472 * if not specified. 473 * @param codeChallengeMethod The code challenge method, 474 * {@code null} if not specified. 475 * 476 * @return This builder. 477 */ 478 @Deprecated 479 public Builder codeChallenge(final CodeChallenge codeChallenge, final CodeChallengeMethod codeChallengeMethod) { 480 481 this.codeChallenge = codeChallenge; 482 this.codeChallengeMethod = codeChallengeMethod; 483 return this; 484 } 485 486 487 /** 488 * Sets the code challenge for Proof Key for Code Exchange 489 * (PKCE) by public OAuth clients. 490 * 491 * @param codeVerifier The code verifier to use to 492 * compute the code challenge, 493 * {@code null} if PKCE is not 494 * specified. 495 * @param codeChallengeMethod The code challenge method, 496 * {@code null} if not specified. 497 * Defaults to 498 * {@link CodeChallengeMethod#PLAIN} 499 * if a code verifier is specified. 500 * 501 * @return This builder. 502 */ 503 public Builder codeChallenge(final CodeVerifier codeVerifier, final CodeChallengeMethod codeChallengeMethod) { 504 505 if (codeVerifier != null) { 506 CodeChallengeMethod method = codeChallengeMethod != null ? codeChallengeMethod : CodeChallengeMethod.getDefault(); 507 this.codeChallenge = CodeChallenge.compute(method, codeVerifier); 508 this.codeChallengeMethod = method; 509 } else { 510 this.codeChallenge = null; 511 this.codeChallengeMethod = null; 512 } 513 return this; 514 } 515 516 517 /** 518 * Sets the resource server URI(s). 519 * 520 * @param resources The resource URI(s), {@code null} if not 521 * specified. 522 * 523 * @return This builder. 524 */ 525 public Builder resources(final URI ... resources) { 526 if (resources != null) { 527 this.resources = Arrays.asList(resources); 528 } else { 529 this.resources = null; 530 } 531 return this; 532 } 533 534 535 /** 536 * Requests incremental authorisation. 537 * 538 * @param includeGrantedScopes {@code true} to request 539 * incremental authorisation. 540 * 541 * @return This builder. 542 */ 543 public Builder includeGrantedScopes(final boolean includeGrantedScopes) { 544 545 this.includeGrantedScopes = includeGrantedScopes; 546 return this; 547 } 548 549 550 /** 551 * Sets the request object. Corresponds to the optional 552 * {@code request} parameter. Must not be specified together 553 * with a request object URI. 554 * 555 * @param requestObject The request object, {@code null} if not 556 * specified. 557 * 558 * @return This builder. 559 */ 560 public Builder requestObject(final JWT requestObject) { 561 562 this.requestObject = requestObject; 563 return this; 564 } 565 566 567 /** 568 * Sets the request object URI. Corresponds to the optional 569 * {@code request_uri} parameter. Must not be specified 570 * together with a request object. 571 * 572 * @param requestURI The request object URI, {@code null} if 573 * not specified. 574 * 575 * @return This builder. 576 */ 577 public Builder requestURI(final URI requestURI) { 578 579 this.requestURI = requestURI; 580 return this; 581 } 582 583 584 /** 585 * Sets the requested prompt. Corresponds to the optional 586 * {@code prompt} parameter. 587 * 588 * @param prompt The requested prompt, {@code null} if not 589 * specified. 590 * 591 * @return This builder. 592 */ 593 public Builder prompt(final Prompt prompt) { 594 595 this.prompt = prompt; 596 return this; 597 } 598 599 600 /** 601 * Sets a custom parameter. 602 * 603 * @param name The parameter name. Must not be {@code null}. 604 * @param values The parameter values, {@code null} if not 605 * specified. 606 * 607 * @return This builder. 608 */ 609 public Builder customParameter(final String name, final String ... values) { 610 611 if (values == null || values.length == 0) { 612 customParams.remove(name); 613 } else { 614 customParams.put(name, Arrays.asList(values)); 615 } 616 617 return this; 618 } 619 620 621 /** 622 * Sets the URI of the endpoint (HTTP or HTTPS) for which the 623 * request is intended. 624 * 625 * @param uri The endpoint URI, {@code null} if not specified. 626 * 627 * @return This builder. 628 */ 629 public Builder endpointURI(final URI uri) { 630 631 this.uri = uri; 632 return this; 633 } 634 635 636 /** 637 * Builds a new authorisation request. 638 * 639 * @return The authorisation request. 640 */ 641 public AuthorizationRequest build() { 642 643 try { 644 return new AuthorizationRequest(uri, rt, rm, clientID, redirectURI, scope, state, 645 codeChallenge, codeChallengeMethod, resources, includeGrantedScopes, 646 requestObject, requestURI, 647 prompt, 648 customParams); 649 } catch (IllegalArgumentException e) { 650 throw new IllegalStateException(e.getMessage(), e); 651 } 652 } 653 } 654 655 656 /** 657 * Creates a new minimal authorisation request. 658 * 659 * @param uri The URI of the authorisation endpoint. May be 660 * {@code null} if the {@link #toHTTPRequest} method 661 * will not be used. 662 * @param rt The response type. Corresponds to the 663 * {@code response_type} parameter. Must not be 664 * {@code null}. 665 * @param clientID The client identifier. Corresponds to the 666 * {@code client_id} parameter. Must not be 667 * {@code null}. 668 */ 669 public AuthorizationRequest(final URI uri, 670 final ResponseType rt, 671 final ClientID clientID) { 672 673 this(uri, rt, null, clientID, null, null, null, null, null, null, false, null, null, null, null); 674 } 675 676 677 /** 678 * Creates a new authorisation request. 679 * 680 * @param uri The URI of the authorisation endpoint. 681 * May be {@code null} if the 682 * {@link #toHTTPRequest} method will not be 683 * used. 684 * @param rt The response type. Corresponds to the 685 * {@code response_type} parameter. Must not 686 * be {@code null}. 687 * @param rm The response mode. Corresponds to the 688 * optional {@code response_mode} parameter. 689 * Use of this parameter is not recommended 690 * unless a non-default response mode is 691 * requested (e.g. form_post). 692 * @param clientID The client identifier. Corresponds to the 693 * {@code client_id} parameter. Must not be 694 * {@code null}. 695 * @param redirectURI The redirection URI. Corresponds to the 696 * optional {@code redirect_uri} parameter. 697 * {@code null} if not specified. 698 * @param scope The request scope. Corresponds to the 699 * optional {@code scope} parameter. 700 * {@code null} if not specified. 701 * @param state The state. Corresponds to the recommended 702 * {@code state} parameter. {@code null} if 703 * not specified. 704 */ 705 public AuthorizationRequest(final URI uri, 706 final ResponseType rt, 707 final ResponseMode rm, 708 final ClientID clientID, 709 final URI redirectURI, 710 final Scope scope, 711 final State state) { 712 713 this(uri, rt, rm, clientID, redirectURI, scope, state, null, null, null, false, null, null, null, null); 714 } 715 716 717 /** 718 * Creates a new authorisation request with extension and custom 719 * parameters. 720 * 721 * @param uri The URI of the authorisation endpoint. 722 * May be {@code null} if the 723 * {@link #toHTTPRequest} method will not 724 * be used. 725 * @param rt The response type. Corresponds to the 726 * {@code response_type} parameter. Must 727 * not be {@code null}, unless a request a 728 * request object or URI is specified. 729 * @param rm The response mode. Corresponds to the 730 * optional {@code response_mode} 731 * parameter. Use of this parameter is not 732 * recommended unless a non-default 733 * response mode is requested (e.g. 734 * form_post). 735 * @param clientID The client identifier. Corresponds to 736 * the {@code client_id} parameter. Must 737 * not be {@code null}, unless a request 738 * object or URI is specified. 739 * @param redirectURI The redirection URI. Corresponds to the 740 * optional {@code redirect_uri} parameter. 741 * {@code null} if not specified. 742 * @param scope The request scope. Corresponds to the 743 * optional {@code scope} parameter. 744 * {@code null} if not specified. 745 * @param state The state. Corresponds to the 746 * recommended {@code state} parameter. 747 * {@code null} if not specified. 748 * @param codeChallenge The code challenge for PKCE, 749 * {@code null} if not specified. 750 * @param codeChallengeMethod The code challenge method for PKCE, 751 * {@code null} if not specified. 752 * @param resources The resource URI(s), {@code null} if not 753 * specified. 754 * @param includeGrantedScopes {@code true} to request incremental 755 * authorisation. 756 * @param requestObject The request object. Corresponds to the 757 * optional {@code request} parameter. Must 758 * not be specified together with a request 759 * object URI. {@code null} if not 760 * specified. 761 * @param requestURI The request object URI. Corresponds to 762 * the optional {@code request_uri} 763 * parameter. Must not be specified 764 * together with a request object. 765 * {@code null} if not specified. 766 * @param prompt The requested prompt. Corresponds to the 767 * optional {@code prompt} parameter. 768 * @param customParams Custom parameters, empty map or 769 * {@code null} if none. 770 */ 771 public AuthorizationRequest(final URI uri, 772 final ResponseType rt, 773 final ResponseMode rm, 774 final ClientID clientID, 775 final URI redirectURI, 776 final Scope scope, 777 final State state, 778 final CodeChallenge codeChallenge, 779 final CodeChallengeMethod codeChallengeMethod, 780 final List<URI> resources, 781 final boolean includeGrantedScopes, 782 final JWT requestObject, 783 final URI requestURI, 784 final Prompt prompt, 785 final Map<String, List<String>> customParams) { 786 787 super(uri); 788 789 if (rt == null && requestObject == null && requestURI == null) 790 throw new IllegalArgumentException("The response type must not be null"); 791 792 this.rt = rt; 793 794 this.rm = rm; 795 796 797 if (clientID == null) 798 throw new IllegalArgumentException("The client ID must not be null"); 799 800 this.clientID = clientID; 801 802 803 this.redirectURI = redirectURI; 804 this.scope = scope; 805 this.state = state; 806 807 this.codeChallenge = codeChallenge; 808 this.codeChallengeMethod = codeChallengeMethod; 809 810 if (resources != null) { 811 for (URI resourceURI: resources) { 812 if (! ResourceUtils.isValidResourceURI(resourceURI)) 813 throw new IllegalArgumentException("Resource URI must be absolute and with no query or fragment: " + resourceURI); 814 } 815 } 816 817 this.resources = resources; 818 819 this.includeGrantedScopes = includeGrantedScopes; 820 821 if (requestObject != null && requestURI != null) 822 throw new IllegalArgumentException("Either a request object or a request URI must be specified, but not both"); 823 824 this.requestObject = requestObject; 825 this.requestURI = requestURI; 826 827 if (requestObject instanceof SignedJWT) { 828 // Make sure the "sub" claim is not set to the client_id value 829 // https://tools.ietf.org/html/draft-ietf-oauth-jwsreq-29#section-10.8 830 JWTClaimsSet requestObjectClaims; 831 try { 832 requestObjectClaims = requestObject.getJWTClaimsSet(); 833 } catch (java.text.ParseException e) { 834 // Should never happen 835 throw new IllegalArgumentException("Illegal request parameter: " + e.getMessage(), e); 836 } 837 if (clientID.getValue().equals(requestObjectClaims.getSubject())) { 838 throw new IllegalArgumentException("Illegal request parameter: The JWT sub (subject) claim must not equal the client_id"); 839 } 840 } 841 842 this.prompt = prompt; // technically OpenID 843 844 if (MapUtils.isNotEmpty(customParams)) { 845 this.customParams = Collections.unmodifiableMap(customParams); 846 } else { 847 this.customParams = Collections.emptyMap(); 848 } 849 } 850 851 852 /** 853 * Returns the registered (standard) OAuth 2.0 authorisation request 854 * parameter names. 855 * 856 * @return The registered OAuth 2.0 authorisation request parameter 857 * names, as a unmodifiable set. 858 */ 859 public static Set<String> getRegisteredParameterNames() { 860 861 return REGISTERED_PARAMETER_NAMES; 862 } 863 864 865 /** 866 * Gets the response type. Corresponds to the {@code response_type} 867 * parameter. 868 * 869 * @return The response type, may be {@code null} for a 870 * {@link #specifiesRequestObject() JWT secured authorisation 871 * request} with a {@link #getRequestObject() request} or 872 * {@link #getRequestURI() request_uri} parameter. 873 */ 874 public ResponseType getResponseType() { 875 876 return rt; 877 } 878 879 880 /** 881 * Gets the optional response mode. Corresponds to the optional 882 * {@code response_mode} parameter. 883 * 884 * @return The response mode, {@code null} if not specified. 885 */ 886 public ResponseMode getResponseMode() { 887 888 return rm; 889 } 890 891 892 /** 893 * Returns the implied response mode, determined by the optional 894 * {@code response_mode} parameter, and if that isn't specified, by 895 * the {@code response_type}. 896 * 897 * @return The implied response mode. 898 */ 899 public ResponseMode impliedResponseMode() { 900 901 return ResponseMode.resolve(rm, rt); 902 } 903 904 905 /** 906 * Gets the client identifier. Corresponds to the {@code client_id} 907 * parameter. 908 * 909 * @return The client identifier. 910 */ 911 public ClientID getClientID() { 912 913 return clientID; 914 } 915 916 917 /** 918 * Gets the redirection URI. Corresponds to the optional 919 * {@code redirection_uri} parameter. 920 * 921 * @return The redirection URI, {@code null} if not specified. 922 */ 923 public URI getRedirectionURI() { 924 925 return redirectURI; 926 } 927 928 929 /** 930 * Gets the scope. Corresponds to the optional {@code scope} parameter. 931 * 932 * @return The scope, {@code null} if not specified. 933 */ 934 public Scope getScope() { 935 936 return scope; 937 } 938 939 940 /** 941 * Gets the state. Corresponds to the recommended {@code state} 942 * parameter. 943 * 944 * @return The state, {@code null} if not specified. 945 */ 946 public State getState() { 947 948 return state; 949 } 950 951 952 /** 953 * Returns the code challenge for PKCE. 954 * 955 * @return The code challenge, {@code null} if not specified. 956 */ 957 public CodeChallenge getCodeChallenge() { 958 959 return codeChallenge; 960 } 961 962 963 /** 964 * Returns the code challenge method for PKCE. 965 * 966 * @return The code challenge method, {@code null} if not specified. 967 */ 968 public CodeChallengeMethod getCodeChallengeMethod() { 969 970 return codeChallengeMethod; 971 } 972 973 974 /** 975 * Returns the resource server URI. 976 * 977 * @return The resource URI(s), {@code null} if not specified. 978 */ 979 public List<URI> getResources() { 980 981 return resources; 982 } 983 984 985 /** 986 * Returns {@code true} if incremental authorisation is requested. 987 * 988 * @return {@code true} if incremental authorisation is requested, 989 * else {@code false}. 990 */ 991 public boolean includeGrantedScopes() { 992 993 return includeGrantedScopes; 994 } 995 996 997 /** 998 * Gets the request object. Corresponds to the optional {@code request} 999 * parameter. 1000 * 1001 * @return The request object, {@code null} if not specified. 1002 */ 1003 public JWT getRequestObject() { 1004 1005 return requestObject; 1006 } 1007 1008 1009 /** 1010 * Gets the request object URI. Corresponds to the optional 1011 * {@code request_uri} parameter. 1012 * 1013 * @return The request object URI, {@code null} if not specified. 1014 */ 1015 public URI getRequestURI() { 1016 1017 return requestURI; 1018 } 1019 1020 1021 /** 1022 * Returns {@code true} if this is a JWT secured authentication 1023 * request. 1024 * 1025 * @return {@code true} if a request object via a {@code request} or 1026 * {@code request_uri} parameter is specified, else 1027 * {@code false}. 1028 */ 1029 public boolean specifiesRequestObject() { 1030 1031 return requestObject != null || requestURI != null; 1032 } 1033 1034 1035 /** 1036 * Gets the requested prompt. Corresponds to the optional 1037 * {@code prompt} parameter. 1038 * 1039 * @return The requested prompt, {@code null} if not specified. 1040 */ 1041 public Prompt getPrompt() { 1042 1043 return prompt; 1044 } 1045 1046 1047 /** 1048 * Returns the additional custom parameters. 1049 * 1050 * @return The additional custom parameters as a unmodifiable map, 1051 * empty map if none. 1052 */ 1053 public Map<String,List<String>> getCustomParameters () { 1054 1055 return customParams; 1056 } 1057 1058 1059 /** 1060 * Returns the specified custom parameter. 1061 * 1062 * @param name The parameter name. Must not be {@code null}. 1063 * 1064 * @return The parameter value(s), {@code null} if not specified. 1065 */ 1066 public List<String> getCustomParameter(final String name) { 1067 1068 return customParams.get(name); 1069 } 1070 1071 1072 /** 1073 * Returns the URI query parameters for this authorisation request. 1074 * Query parameters which are part of the authorisation endpoint are 1075 * not included. 1076 * 1077 * <p>Example parameters: 1078 * 1079 * <pre> 1080 * response_type = code 1081 * client_id = s6BhdRkqt3 1082 * state = xyz 1083 * redirect_uri = https://client.example.com/cb 1084 * </pre> 1085 * 1086 * @return The parameters. 1087 */ 1088 public Map<String,List<String>> toParameters() { 1089 1090 // Put custom params first, so they may be overwritten by std params 1091 Map<String, List<String>> params = new LinkedHashMap<>(customParams); 1092 1093 params.put("client_id", Collections.singletonList(clientID.getValue())); 1094 1095 if (rt != null) 1096 params.put("response_type", Collections.singletonList(rt.toString())); 1097 1098 if (rm != null) 1099 params.put("response_mode", Collections.singletonList(rm.getValue())); 1100 1101 if (redirectURI != null) 1102 params.put("redirect_uri", Collections.singletonList(redirectURI.toString())); 1103 1104 if (scope != null) 1105 params.put("scope", Collections.singletonList(scope.toString())); 1106 1107 if (state != null) 1108 params.put("state", Collections.singletonList(state.getValue())); 1109 1110 if (codeChallenge != null) { 1111 params.put("code_challenge", Collections.singletonList(codeChallenge.getValue())); 1112 1113 if (codeChallengeMethod != null) { 1114 params.put("code_challenge_method", Collections.singletonList(codeChallengeMethod.getValue())); 1115 } 1116 } 1117 1118 if (includeGrantedScopes) 1119 params.put("include_granted_scopes", Collections.singletonList("true")); 1120 1121 if (resources != null) { 1122 List<String> resourceValues = new LinkedList<>(); 1123 for (URI resourceURI: resources) { 1124 if (resourceURI != null) { 1125 resourceValues.add(resourceURI.toString()); 1126 } 1127 } 1128 params.put("resource", resourceValues); 1129 } 1130 1131 if (requestObject != null) { 1132 try { 1133 params.put("request", Collections.singletonList(requestObject.serialize())); 1134 1135 } catch (IllegalStateException e) { 1136 throw new SerializeException("Couldn't serialize request object to JWT: " + e.getMessage(), e); 1137 } 1138 } 1139 1140 if (requestURI != null) 1141 params.put("request_uri", Collections.singletonList(requestURI.toString())); 1142 1143 if (prompt != null) 1144 params.put("prompt", Collections.singletonList(prompt.toString())); 1145 1146 return params; 1147 } 1148 1149 1150 /** 1151 * Returns the parameters for this authorisation request as a JSON Web 1152 * Token (JWT) claims set. Intended for creating a request object. 1153 * 1154 * @return The parameters as JWT claim set. 1155 */ 1156 public JWTClaimsSet toJWTClaimsSet() { 1157 1158 if (specifiesRequestObject()) { 1159 throw new IllegalStateException("Cannot create nested JWT secured authorization request"); 1160 } 1161 1162 return JWTClaimsSetUtils.toJWTClaimsSet(toParameters()); 1163 } 1164 1165 1166 /** 1167 * Returns the URI query string for this authorisation request. 1168 * 1169 * <p>Note that the '?' character preceding the query string in an URI 1170 * is not included in the returned string. 1171 * 1172 * <p>Example URI query string: 1173 * 1174 * <pre> 1175 * response_type=code 1176 * &client_id=s6BhdRkqt3 1177 * &state=xyz 1178 * &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb 1179 * </pre> 1180 * 1181 * @return The URI query string. 1182 */ 1183 public String toQueryString() { 1184 1185 Map<String, List<String>> params = new HashMap<>(); 1186 if (getEndpointURI() != null) { 1187 params.putAll(URLUtils.parseParameters(getEndpointURI().getQuery())); 1188 } 1189 params.putAll(toParameters()); 1190 1191 return URLUtils.serializeParameters(params); 1192 } 1193 1194 1195 /** 1196 * Returns the complete URI representation for this authorisation 1197 * request, consisting of the {@link #getEndpointURI authorization 1198 * endpoint URI} with the {@link #toQueryString query string} appended. 1199 * 1200 * <p>Example URI: 1201 * 1202 * <pre> 1203 * https://server.example.com/authorize? 1204 * response_type=code 1205 * &client_id=s6BhdRkqt3 1206 * &state=xyz 1207 * &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb 1208 * </pre> 1209 * 1210 * @return The URI representation. 1211 */ 1212 public URI toURI() { 1213 1214 if (getEndpointURI() == null) 1215 throw new SerializeException("The authorization endpoint URI is not specified"); 1216 1217 StringBuilder sb = new StringBuilder(URIUtils.stripQueryString(getEndpointURI()).toString()); 1218 sb.append('?'); 1219 sb.append(toQueryString()); 1220 try { 1221 return new URI(sb.toString()); 1222 } catch (URISyntaxException e) { 1223 throw new SerializeException("Couldn't append query string: " + e.getMessage(), e); 1224 } 1225 } 1226 1227 1228 /** 1229 * Returns the matching HTTP request. 1230 * 1231 * @param method The HTTP request method which can be GET or POST. Must 1232 * not be {@code null}. 1233 * 1234 * @return The HTTP request. 1235 */ 1236 public HTTPRequest toHTTPRequest(final HTTPRequest.Method method) { 1237 1238 if (getEndpointURI() == null) 1239 throw new SerializeException("The endpoint URI is not specified"); 1240 1241 HTTPRequest httpRequest; 1242 if (method.equals(HTTPRequest.Method.GET)) { 1243 httpRequest = new HTTPRequest(HTTPRequest.Method.GET, getEndpointURI()); 1244 } else if (method.equals(HTTPRequest.Method.POST)) { 1245 httpRequest = new HTTPRequest(HTTPRequest.Method.POST, getEndpointURI()); 1246 } else { 1247 throw new IllegalArgumentException("The HTTP request method must be GET or POST"); 1248 } 1249 1250 httpRequest.setQuery(toQueryString()); 1251 1252 return httpRequest; 1253 } 1254 1255 1256 @Override 1257 public HTTPRequest toHTTPRequest() { 1258 1259 return toHTTPRequest(HTTPRequest.Method.GET); 1260 } 1261 1262 1263 /** 1264 * Parses an authorisation request from the specified URI query 1265 * parameters. 1266 * 1267 * <p>Example parameters: 1268 * 1269 * <pre> 1270 * response_type = code 1271 * client_id = s6BhdRkqt3 1272 * state = xyz 1273 * redirect_uri = https://client.example.com/cb 1274 * </pre> 1275 * 1276 * @param params The parameters. Must not be {@code null}. 1277 * 1278 * @return The authorisation request. 1279 * 1280 * @throws ParseException If the parameters couldn't be parsed to an 1281 * authorisation request. 1282 */ 1283 public static AuthorizationRequest parse(final Map<String,List<String>> params) 1284 throws ParseException { 1285 1286 return parse(null, params); 1287 } 1288 1289 1290 /** 1291 * Parses an authorisation request from the specified URI and query 1292 * parameters. 1293 * 1294 * <p>Example parameters: 1295 * 1296 * <pre> 1297 * response_type = code 1298 * client_id = s6BhdRkqt3 1299 * state = xyz 1300 * redirect_uri = https://client.example.com/cb 1301 * </pre> 1302 * 1303 * @param uri The URI of the authorisation endpoint. May be 1304 * {@code null} if the {@link #toHTTPRequest()} method 1305 * will not be used. 1306 * @param params The parameters. Must not be {@code null}. 1307 * 1308 * @return The authorisation request. 1309 * 1310 * @throws ParseException If the parameters couldn't be parsed to an 1311 * authorisation request. 1312 */ 1313 public static AuthorizationRequest parse(final URI uri, final Map<String,List<String>> params) 1314 throws ParseException { 1315 1316 // Parse response_mode, response_type, client_id, redirect_uri and state first, 1317 // needed if parsing results in a error response 1318 final ClientID clientID; 1319 URI redirectURI = null; 1320 State state = State.parse(MultivaluedMapUtils.getFirstValue(params, "state")); 1321 ResponseMode rm = null; 1322 ResponseType rt = null; 1323 1324 // Optional response_mode 1325 String v = MultivaluedMapUtils.getFirstValue(params, "response_mode"); 1326 if (StringUtils.isNotBlank(v)) { 1327 rm = new ResponseMode(v); 1328 } 1329 1330 // Mandatory client_id 1331 v = MultivaluedMapUtils.getFirstValue(params, "client_id"); 1332 if (StringUtils.isBlank(v)) { 1333 // No automatic redirection https://tools.ietf.org/html/rfc6749#section-4.1.2.1 1334 String msg = "Missing client_id parameter"; 1335 throw new ParseException(msg, OAuth2Error.INVALID_REQUEST.appendDescription(": " + msg)); 1336 } 1337 clientID = new ClientID(v); 1338 1339 // Optional redirect_uri 1340 v = MultivaluedMapUtils.getFirstValue(params, "redirect_uri"); 1341 if (StringUtils.isNotBlank(v)) { 1342 try { 1343 redirectURI = new URI(v); 1344 } catch (URISyntaxException e) { 1345 // No automatic redirection https://tools.ietf.org/html/rfc6749#section-4.1.2.1 1346 String msg = "Invalid redirect_uri parameter: " + e.getMessage(); 1347 throw new ParseException(msg, OAuth2Error.INVALID_REQUEST.appendDescription(": " + msg)); 1348 } 1349 } 1350 1351 // Mandatory response_type, unless in JAR 1352 v = MultivaluedMapUtils.getFirstValue(params, "response_type"); 1353 if (StringUtils.isNotBlank(v)) { 1354 try { 1355 rt = ResponseType.parse(v); 1356 } catch (ParseException e) { 1357 // Only cause 1358 String msg = "Invalid response_type parameter"; 1359 throw new ParseException(msg, OAuth2Error.INVALID_REQUEST.appendDescription(": " + msg), 1360 clientID, redirectURI, rm, state, e); 1361 } 1362 } 1363 1364 // Check for a JAR in request or request_uri parameters 1365 v = MultivaluedMapUtils.getFirstValue(params, "request_uri"); 1366 URI requestURI = null; 1367 if (StringUtils.isNotBlank(v)) { 1368 try { 1369 requestURI = new URI(v); 1370 } catch (URISyntaxException e) { 1371 String msg = "Invalid request_uri parameter: " + e.getMessage(); 1372 throw new ParseException(msg, OAuth2Error.INVALID_REQUEST.appendDescription(": " + msg), 1373 clientID, redirectURI, ResponseMode.resolve(rm, rt), state, e); 1374 } 1375 } 1376 1377 v = MultivaluedMapUtils.getFirstValue(params, "request"); 1378 1379 JWT requestObject = null; 1380 1381 if (StringUtils.isNotBlank(v)) { 1382 1383 // request_object and request_uri must not be present at the same time 1384 if (requestURI != null) { 1385 String msg = "Invalid request: Found mutually exclusive request and request_uri parameters"; 1386 throw new ParseException(msg, OAuth2Error.INVALID_REQUEST.appendDescription(": " + msg), 1387 clientID, redirectURI, ResponseMode.resolve(rm, rt), state, null); 1388 } 1389 1390 try { 1391 requestObject = JWTParser.parse(v); 1392 1393 if (requestObject instanceof SignedJWT) { 1394 // Make sure the "sub" claim is not set to the client_id value 1395 // https://tools.ietf.org/html/draft-ietf-oauth-jwsreq-29#section-10.8 1396 JWTClaimsSet requestObjectClaims = requestObject.getJWTClaimsSet(); 1397 if (clientID.getValue().equals(requestObjectClaims.getSubject())) { 1398 throw new java.text.ParseException("The JWT sub (subject) claim must not equal the client_id", 0); 1399 } 1400 } 1401 1402 } catch (java.text.ParseException e) { 1403 String msg = "Invalid request parameter: " + e.getMessage(); 1404 throw new ParseException(msg, OAuth2Error.INVALID_REQUEST.appendDescription(": " + msg), 1405 clientID, redirectURI, ResponseMode.resolve(rm, rt), state, e); 1406 } 1407 } 1408 1409 // Response type mandatory, unless in JAR 1410 if (rt == null && requestObject == null && requestURI == null) { 1411 String msg = "Missing response_type parameter"; 1412 throw new ParseException(msg, OAuth2Error.INVALID_REQUEST.appendDescription(": " + msg), 1413 clientID, redirectURI, ResponseMode.resolve(rm, null), state, null); 1414 } 1415 1416 1417 // Parse optional scope 1418 v = MultivaluedMapUtils.getFirstValue(params, "scope"); 1419 1420 Scope scope = null; 1421 1422 if (StringUtils.isNotBlank(v)) 1423 scope = Scope.parse(v); 1424 1425 1426 // Parse optional code challenge and method for PKCE 1427 CodeChallenge codeChallenge = null; 1428 CodeChallengeMethod codeChallengeMethod = null; 1429 1430 v = MultivaluedMapUtils.getFirstValue(params, "code_challenge"); 1431 1432 if (StringUtils.isNotBlank(v)) 1433 codeChallenge = CodeChallenge.parse(v); 1434 1435 if (codeChallenge != null) { 1436 1437 v = MultivaluedMapUtils.getFirstValue(params, "code_challenge_method"); 1438 1439 if (StringUtils.isNotBlank(v)) 1440 codeChallengeMethod = CodeChallengeMethod.parse(v); 1441 } 1442 1443 List<URI> resources = null; 1444 1445 List<String> vList = params.get("resource"); 1446 1447 if (vList != null) { 1448 1449 resources = new LinkedList<>(); 1450 1451 for (String uriValue: vList) { 1452 1453 if (uriValue == null) 1454 continue; 1455 1456 String errMsg = "Illegal resource parameter: Must be an absolute URI and with no query or fragment: " + uriValue; 1457 1458 URI resourceURI; 1459 try { 1460 resourceURI = new URI(uriValue); 1461 } catch (URISyntaxException e) { 1462 throw new ParseException(errMsg, OAuth2Error.INVALID_RESOURCE.setDescription(errMsg), 1463 clientID, redirectURI, ResponseMode.resolve(rm, rt), state, e); 1464 } 1465 1466 if (! ResourceUtils.isValidResourceURI(resourceURI)) { 1467 throw new ParseException(errMsg, OAuth2Error.INVALID_RESOURCE.setDescription(errMsg), 1468 clientID, redirectURI, ResponseMode.resolve(rm, rt), state, null); 1469 } 1470 1471 resources.add(resourceURI); 1472 } 1473 } 1474 1475 boolean includeGrantedScopes = false; 1476 v = MultivaluedMapUtils.getFirstValue(params, "include_granted_scopes"); 1477 if ("true".equals(v)) { 1478 includeGrantedScopes = true; 1479 } 1480 1481 Prompt prompt; 1482 try { 1483 prompt = Prompt.parse(MultivaluedMapUtils.getFirstValue(params, "prompt")); 1484 1485 } catch (ParseException e) { 1486 String msg = "Invalid prompt parameter: " + e.getMessage(); 1487 throw new ParseException(msg, OAuth2Error.INVALID_REQUEST.appendDescription(": " + msg), 1488 clientID, redirectURI, ResponseMode.resolve(rm, rt), state, e); 1489 } 1490 1491 // Parse custom parameters 1492 Map<String,List<String>> customParams = null; 1493 1494 for (Map.Entry<String,List<String>> p: params.entrySet()) { 1495 1496 if (! REGISTERED_PARAMETER_NAMES.contains(p.getKey())) { 1497 // We have a custom parameter 1498 if (customParams == null) { 1499 customParams = new HashMap<>(); 1500 } 1501 customParams.put(p.getKey(), p.getValue()); 1502 } 1503 } 1504 1505 1506 return new AuthorizationRequest(uri, rt, rm, clientID, redirectURI, scope, state, 1507 codeChallenge, codeChallengeMethod, resources, includeGrantedScopes, 1508 requestObject, requestURI, 1509 prompt, 1510 customParams); 1511 } 1512 1513 1514 /** 1515 * Parses an authorisation request from the specified URI query string. 1516 * 1517 * <p>Example URI query string: 1518 * 1519 * <pre> 1520 * response_type=code 1521 * &client_id=s6BhdRkqt3 1522 * &state=xyz 1523 * &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb 1524 * </pre> 1525 * 1526 * @param query The URI query string. Must not be {@code null}. 1527 * 1528 * @return The authorisation request. 1529 * 1530 * @throws ParseException If the query string couldn't be parsed to an 1531 * authorisation request. 1532 */ 1533 public static AuthorizationRequest parse(final String query) 1534 throws ParseException { 1535 1536 return parse(null, URLUtils.parseParameters(query)); 1537 } 1538 1539 1540 /** 1541 * Parses an authorisation request from the specified URI and query 1542 * string. 1543 * 1544 * <p>Example URI query string: 1545 * 1546 * <pre> 1547 * response_type=code 1548 * &client_id=s6BhdRkqt3 1549 * &state=xyz 1550 * &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb 1551 * </pre> 1552 * 1553 * @param uri The URI of the authorisation endpoint. May be 1554 * {@code null} if the {@link #toHTTPRequest()} method 1555 * will not be used. 1556 * @param query The URI query string. Must not be {@code null}. 1557 * 1558 * @return The authorisation request. 1559 * 1560 * @throws ParseException If the query string couldn't be parsed to an 1561 * authorisation request. 1562 */ 1563 public static AuthorizationRequest parse(final URI uri, final String query) 1564 throws ParseException { 1565 1566 return parse(uri, URLUtils.parseParameters(query)); 1567 } 1568 1569 1570 /** 1571 * Parses an authorisation request from the specified URI. 1572 * 1573 * <p>Example URI: 1574 * 1575 * <pre> 1576 * https://server.example.com/authorize? 1577 * response_type=code 1578 * &client_id=s6BhdRkqt3 1579 * &state=xyz 1580 * &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb 1581 * </pre> 1582 * 1583 * @param uri The URI. Must not be {@code null}. 1584 * 1585 * @return The authorisation request. 1586 * 1587 * @throws ParseException If the URI couldn't be parsed to an 1588 * authorisation request. 1589 */ 1590 public static AuthorizationRequest parse(final URI uri) 1591 throws ParseException { 1592 1593 return parse(URIUtils.getBaseURI(uri), URLUtils.parseParameters(uri.getRawQuery())); 1594 } 1595 1596 1597 /** 1598 * Parses an authorisation request from the specified HTTP request. 1599 * 1600 * <p>Example HTTP request (GET): 1601 * 1602 * <pre> 1603 * https://server.example.com/authorize? 1604 * response_type=code 1605 * &client_id=s6BhdRkqt3 1606 * &state=xyz 1607 * &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb 1608 * </pre> 1609 * 1610 * @param httpRequest The HTTP request. Must not be {@code null}. 1611 * 1612 * @return The authorisation request. 1613 * 1614 * @throws ParseException If the HTTP request couldn't be parsed to an 1615 * authorisation request. 1616 */ 1617 public static AuthorizationRequest parse(final HTTPRequest httpRequest) 1618 throws ParseException { 1619 1620 String query = httpRequest.getQuery(); 1621 1622 if (query == null) 1623 throw new ParseException("Missing URI query string"); 1624 1625 try { 1626 return parse(URIUtils.getBaseURI(httpRequest.getURL().toURI()), query); 1627 1628 } catch (URISyntaxException e) { 1629 1630 throw new ParseException(e.getMessage(), e); 1631 } 1632 } 1633}