@@ -551,25 +551,39 @@ class Client
551
551
protected $ host = 'https://demo.docusign.net/restapi ' ;
552
552
553
553
/**
554
- * Docusign Username
554
+ * Docusign Integrator Key
555
555
*
556
556
* @var string|null
557
557
*/
558
- protected $ username ;
558
+ protected $ integrator_key ;
559
559
560
560
/**
561
- * Docusign Password
561
+ * Docusign Auth Server
562
562
*
563
563
* @var string|null
564
564
*/
565
- protected $ password ;
565
+ protected $ auth_server ;
566
566
567
567
/**
568
- * Docusign Integrator Key
568
+ * Docusign RSA Private Key
569
569
*
570
570
* @var string|null
571
571
*/
572
- protected $ integrator_key ;
572
+ protected $ private_key ;
573
+
574
+ /**
575
+ * Docusign JWT expiry in minutes
576
+ *
577
+ * @var int
578
+ */
579
+ protected $ jwt_expiry = 60 ;
580
+
581
+ /**
582
+ * Docusign User ID
583
+ *
584
+ * @var string|null
585
+ */
586
+ protected $ impersonated_user_id ;
573
587
574
588
/**
575
589
* Docusign Account Id
@@ -578,6 +592,13 @@ class Client
578
592
*/
579
593
protected $ account_id ;
580
594
595
+ /**
596
+ * Docusign Jwt Scope
597
+ *
598
+ * @var string|null
599
+ */
600
+ protected $ jwt_scope ;
601
+
581
602
/**
582
603
* Container for all instantiated api objects
583
604
* @var array
@@ -599,35 +620,32 @@ class Client
599
620
public function __construct (array $ params = [])
600
621
{
601
622
$ this ->host = $ params ['host ' ] ?? null ;
602
- $ this ->username = $ params ['username ' ] ?? null ;
603
- $ this ->password = $ params ['password ' ] ?? null ;
623
+ $ this ->auth_server = $ params ['auth_server ' ] ?? ' account-d.docusign.com ' ;
624
+ $ this ->jwt_scope = $ params ['jwt_scope ' ] ?? " signature impersonation " ;
604
625
$ this ->integrator_key = $ params ['integrator_key ' ] ?? null ;
626
+ $ this ->impersonated_user_id = $ params ['impersonated_user_id ' ] ?? null ;
627
+ $ this ->private_key = $ params ['private_key ' ] ?? null ;
605
628
}
606
629
607
630
/**
608
- * REQUIRED $options ['username ' => $username , 'password ' => $password , 'integrator_key' => $key]
631
+ * REQUIRED $options ['impersonated_user_id ' => $impersonated_user_id , 'private_key ' => $private_key , 'integrator_key' => $key]
609
632
*
610
- * OPTIONAL $options ['host' => $host]
633
+ * OPTIONAL $options ['host' => $host, 'auth_server' => $auth_server, 'jwt_scope' => $jwt_scope ]
611
634
*
612
635
* @param array $options
613
636
* @return Configuration
614
637
*/
615
638
public function createConfiguration (array $ options = []): Configuration
616
639
{
617
- foreach (['username ' , 'password ' , 'integrator_key ' ] as $ requiredKey ) {
640
+ foreach (['impersonated_user_id ' , 'integrator_key ' , 'private_key ' ] as $ requiredKey ) {
618
641
if (empty ($ options [$ requiredKey ])) {
619
642
throw new \InvalidArgumentException (
620
643
"Cannot create configuration. [ $ requiredKey => \$value] is missing or empty "
621
644
);
622
645
}
623
646
}
624
647
625
- return (new Configuration )->setHost ($ options ['host ' ] ?? $ this ->host )
626
- ->addDefaultHeader ('X-DocuSign-Authentication ' , \json_encode ([
627
- 'Username ' => $ options ['username ' ],
628
- 'Password ' => $ options ['password ' ],
629
- 'IntegratorKey ' => $ options ['integrator_key ' ]
630
- ]));
648
+ return (new Configuration )->setHost ($ options ['host ' ] ?? $ this ->host );
631
649
}
632
650
633
651
/**
@@ -636,10 +654,12 @@ public function createConfiguration(array $options = []): Configuration
636
654
public function getConfiguration (): Configuration
637
655
{
638
656
return $ this ->createConfiguration ([
639
- 'username ' => $ this ->username ,
640
- 'password ' => $ this ->password ,
641
- 'integrator_key ' => $ this ->integrator_key ,
642
- 'host ' => $ this ->host
657
+ 'impersonated_user_id ' => $ this ->impersonated_user_id ,
658
+ 'private_key ' => $ this ->private_key ,
659
+ 'integrator_key ' => $ this ->integrator_key ,
660
+ 'host ' => $ this ->host ,
661
+ 'auth_server ' => $ this ->auth_server ,
662
+ 'jwt_scope ' => $ this ->jwt_scope ,
643
663
]);
644
664
}
645
665
@@ -699,8 +719,6 @@ public function __call($method, $args)
699
719
}
700
720
701
721
return $ docusignModel ;
702
-
703
-
704
722
}
705
723
706
724
/**
@@ -733,25 +751,40 @@ public function __get($name)
733
751
public function authenticate (): self
734
752
{
735
753
if (!$ this ->authenticated || !isset ($ this ->account_id )) {
736
- $ accounts = $ this ->authentication ->login ();
737
- $ login_accounts = $ accounts ->getLoginAccounts ();
738
- $ account = $ login_accounts [0 ];
754
+ $ accounts = $ this ->login ();
755
+ $ account = $ accounts [0 ];
739
756
$ this ->account_id = $ account ->getAccountId ();
740
- $ base_url = $ account ->getBaseUrl ();
741
- $ base_url = strtolower (substr ($ base_url , 0 , strpos ($ base_url , '/restapi ' ) + 8 ));
742
- // If the host has changed, update host on client config
743
- if ($ this ->host !== $ base_url ) {
744
- $ this ->host = $ base_url ;
745
- // Reset API's
746
- $ this ->_api_container = [];
747
- }
748
757
}
749
758
750
759
$ this ->authenticated = true ;
751
760
752
761
return $ this ;
753
762
}
754
763
764
+ /**
765
+ * Get JWT auth by RSA key
766
+ */
767
+ public function login ()
768
+ {
769
+ $ this ->client ->getOAuth ()->setOAuthBasePath ($ this ->auth_server );
770
+
771
+ try {
772
+ $ response = $ this ->client ->requestJWTUserToken (
773
+ $ this ->integrator_key ,
774
+ $ this ->impersonated_user_id ,
775
+ $ this ->private_key ,
776
+ $ this ->jwt_scope ,
777
+ $ this ->jwt_expiry ,
778
+ );
779
+ } catch (\Throwable $ th ) {
780
+ throw $ th ;
781
+ }
782
+
783
+ $ access_token = $ response [0 ]->getAccessToken ();
784
+
785
+ return $ this ->client ->getUserInfo ($ access_token )[0 ]->getAccounts ();
786
+ }
787
+
755
788
/**
756
789
* @param string $account_id
757
790
* @return $this
@@ -777,7 +810,7 @@ public function isAuthenticated(): bool
777
810
*/
778
811
public function getAccountId (): string
779
812
{
780
- if (null === $ this ->account_id ) {
813
+ if ($ this ->account_id === null ) {
781
814
$ this ->authenticate ();
782
815
}
783
816
0 commit comments