/vendor/okta/jwt-verifier/src/JwtVerifierBuilder.php

https://github.com/UMMS-Biocore/dolphinnext · PHP · 183 lines · 96 code · 27 blank · 60 comment · 14 complexity · 8790a8a981f43ff01463f83a750ed1aa MD5 · raw file

  1. <?php
  2. /******************************************************************************
  3. * Copyright 2017 Okta, Inc. *
  4. * *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); *
  6. * you may not use this file except in compliance with the License. *
  7. * You may obtain a copy of the License at *
  8. * *
  9. * http://www.apache.org/licenses/LICENSE-2.0 *
  10. * *
  11. * Unless required by applicable law or agreed to in writing, software *
  12. * distributed under the License is distributed on an "AS IS" BASIS, *
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
  14. * See the License for the specific language governing permissions and *
  15. * limitations under the License. *
  16. ******************************************************************************/
  17. namespace Okta\JwtVerifier;
  18. use Okta\JwtVerifier\Discovery\Oauth;
  19. use Okta\JwtVerifier\Adaptors\Adaptor;
  20. use Okta\JwtVerifier\Discovery\DiscoveryMethod;
  21. use Bretterer\IsoDurationConverter\DurationParser;
  22. class JwtVerifierBuilder
  23. {
  24. protected $issuer;
  25. protected $discovery;
  26. protected $request;
  27. protected $adaptor;
  28. protected $audience;
  29. protected $clientId;
  30. protected $nonce;
  31. protected $leeway = 120;
  32. public function __construct(Request $request = null)
  33. {
  34. $this->request = $request;
  35. }
  36. /**
  37. * Sets the issuer URI.
  38. *
  39. * @param string $issuer The issuer URI
  40. * @return JwtVerifierBuilder
  41. */
  42. public function setIssuer(string $issuer): self
  43. {
  44. $this->issuer = rtrim($issuer, "/");
  45. return $this;
  46. }
  47. /**
  48. * Set the Discovery class. This class should be an instance of DiscoveryMethod.
  49. *
  50. * @param DiscoveryMethod $discoveryMethod The DiscoveryMethod instance.
  51. * @return JwtVerifierBuilder
  52. */
  53. public function setDiscovery(DiscoveryMethod $discoveryMethod): self
  54. {
  55. $this->discovery = $discoveryMethod;
  56. return $this;
  57. }
  58. /**
  59. * Set the Adaptor class. This class should be an interface of Adaptor.
  60. *
  61. * @param Adaptor $adaptor The adaptor of the JWT library you are using.
  62. * @return JwtVerifierBuilder
  63. */
  64. public function setAdaptor(Adaptor $adaptor): self
  65. {
  66. $this->adaptor = $adaptor;
  67. return $this;
  68. }
  69. public function setAudience($audience)
  70. {
  71. $this->audience = $audience;
  72. return $this;
  73. }
  74. public function setClientId($clientId)
  75. {
  76. $this->clientId = $clientId;
  77. return $this;
  78. }
  79. public function setNonce($nonce)
  80. {
  81. $this->nonce = $nonce;
  82. return $this;
  83. }
  84. /**
  85. * Set the leeway using ISO_8601 Duration string. ie: PT2M
  86. *
  87. * @param string $leeway ISO_8601 Duration format. Default: PT2M
  88. * @return self
  89. * @throws \InvalidArgumentException
  90. */
  91. public function setLeeway(string $leeway = "PT2M"): self
  92. {
  93. if(strstr($leeway, "P")) {
  94. throw new \InvalidArgumentException("It appears that the leeway provided is not in ISO_8601 Duration Format. Please privide a duration in the format of `PT(n)S`");
  95. }
  96. $leeway = (new DurationParser)->parse($leeway);
  97. $this->leeway = $leeway;
  98. return $this;
  99. }
  100. /**
  101. * Build and return the JwtVerifier.
  102. *
  103. * @throws \InvalidArgumentException
  104. * @return JwtVerifier
  105. */
  106. public function build(): JwtVerifier
  107. {
  108. $this->validateIssuer($this->issuer);
  109. $this->validateClientId($this->clientId);
  110. return new JwtVerifier(
  111. $this->issuer,
  112. $this->discovery,
  113. $this->adaptor,
  114. $this->request,
  115. $this->leeway,
  116. [
  117. 'nonce' => $this->nonce,
  118. 'audience' => $this->audience,
  119. 'clientId' => $this->clientId
  120. ]
  121. );
  122. }
  123. /**
  124. * Validate the issuer
  125. *
  126. * @param string $issuer
  127. * @throws \InvalidArgumentException
  128. * @return void
  129. */
  130. private function validateIssuer($issuer): void {
  131. if (null === $issuer || "" == $issuer) {
  132. throw new \InvalidArgumentException("Your Issuer is missing. You can find your issuer from your authorization server settings in the Okta Developer Console. Find out more information aobut Authorization Servers at https://developer.okta.com/docs/guides/customize-authz-server/overview/");
  133. }
  134. if (strstr($issuer, "https://") == false) {
  135. throw new \InvalidArgumentException("Your Issuer must start with https. Current value: {$issuer}. You can copy your issuer from your authorization server settings in the Okta Developer Console. Find out more information aobut Authorization Servers at https://developer.okta.com/docs/guides/customize-authz-server/overview/");
  136. }
  137. if (strstr($issuer, "{yourOktaDomain}") != false) {
  138. throw new \InvalidArgumentException("Replace {yourOktaDomain} with your Okta domain. You can copy your domain from the Okta Developer Console. Follow these instructions to find it: https://bit.ly/finding-okta-domain");
  139. }
  140. }
  141. /**
  142. * Validate the client id
  143. *
  144. * @param string $cid
  145. * @throws \InvalidArgumentException
  146. * @return void
  147. */
  148. private function validateClientId($cid): void {
  149. if (null === $cid || "" == $cid) {
  150. throw new \InvalidArgumentException("Your client ID is missing. You can copy it from the Okta Developer Console in the details for the Application you created. Follow these instructions to find it: https://bit.ly/finding-okta-app-credentials");
  151. }
  152. if (strstr($cid, "{clientId}") != false) {
  153. throw new \InvalidArgumentException("Replace {clientId} with the client ID of your Application. You can copy it from the Okta Developer Console in the details for the Application you created. Follow these instructions to find it: https://bit.ly/finding-okta-app-credentials");
  154. }
  155. }
  156. }