Skip to content

Commit 7889dd4

Browse files
authored
Merge pull request #17 from ChristianKBarnes/master
Allow authentication via OAuth2
2 parents 7ee7044 + 604d19c commit 7889dd4

File tree

2 files changed

+99
-60
lines changed

2 files changed

+99
-60
lines changed

README.md

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ Example:
2929
require 'vendor/autoload.php';
3030

3131
$client = new DocuSign\Rest\Client([
32-
'username' => $username,
33-
'password' => $password,
34-
'integrator_key' => $integrator_key,
35-
'host' => $host
32+
'impersonated_user_id' => $impersonated_user_id,
33+
'integrator_key' => $integrator_key,
34+
'host' => $host,
35+
'private_key' => $private_key,
36+
'auth_server' => $auth_server
3637
]);
3738

3839
$client->accounts // Returns DocuSign\eSign\Api\AccountsApi
@@ -62,10 +63,11 @@ Example:
6263
require 'vendor/autoload.php';
6364

6465
$client = new DocuSign\Rest\Client([
65-
'username' => $username,
66-
'password' => $password,
67-
'integrator_key' => $integrator_key,
68-
'host' => $host
66+
'impersonated_user_id' => $impersonated_user_id,
67+
'private_key' => $private_key,
68+
'integrator_key' => $integrator_key,
69+
'host' => $host,
70+
'auth_server' => $auth_server
6971
]);
7072

7173
$templateRole = $client->templateRole([
@@ -139,19 +141,21 @@ class DocuSignSample
139141
{
140142
public function signatureRequestFromTemplate()
141143
{
142-
$username = "[EMAIL]";
143-
$password = "[PASSWORD]";
144+
145+
$impersonated_user_id = "[IMPERSONATED_USER_ID]";
146+
$private_key = "[PRIVATE_KEY]";
144147
$integrator_key = "[INTEGRATOR_KEY]";
145148

146-
// change to production before going live
149+
// change these to production before going live
147150
$host = "https://demo.docusign.net/restapi";
148-
151+
$auth_server = "account-d.docusign.com";
149152
// Once instantiated, authentication is handled automatically
150153
$client = new DocuSign\Rest\Client([
151-
'username' => $username,
152-
'password' => $password,
153-
'integrator_key' => $integrator_key,
154-
'host' => $host
154+
'impersonated_user_id' => $impersonated_user_id,
155+
'private_key' => $private_key,
156+
'integrator_key' => $integrator_key,
157+
'host' => $host,
158+
'auth_server' => $auth_server
155159
]);
156160

157161
$templateRole = $client->templateRole([
@@ -187,24 +191,26 @@ require 'vendor/autoload.php';
187191

188192
class DocuSignSample
189193
{
190-
191-
protected $username = "[EMAIL]";
192-
protected $password = "[PASSWORD]";
194+
195+
protected $impersonated_user_id = "[IMPERSONATED_USER_ID]";
196+
protected $private_key = "[PRIVATE_KEY]";
193197
protected $integrator_key = "[INTEGRATOR_KEY]";
194198

195-
// change to production before going live
199+
// change these to production before going live
196200
protected $host = "https://demo.docusign.net/restapi";
201+
protected $auth_server = "account-d.docusign.com";
197202

198-
protected $client;
203+
protected $client;
199204

200205
public function __construct()
201206
{
202207
// Once instantiated, authentication is handled automatically
203208
$this->client = new DocuSign\Rest\Client([
204-
'username' => $this->username,
205-
'password' => $this->password,
206-
'integrator_key' => $this->integrator_key,
207-
'host' => $this->host
209+
'impersonated_user_id' => $impersonated_user_id,
210+
'private_key' => $private_key,
211+
'integrator_key' => $integrator_key,
212+
'host' => $host,
213+
'auth_server' => $auth_server
208214
]);
209215
}
210216

src/Client.php

Lines changed: 68 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -551,25 +551,39 @@ class Client
551551
protected $host = 'https://demo.docusign.net/restapi';
552552

553553
/**
554-
* Docusign Username
554+
* Docusign Integrator Key
555555
*
556556
* @var string|null
557557
*/
558-
protected $username;
558+
protected $integrator_key;
559559

560560
/**
561-
* Docusign Password
561+
* Docusign Auth Server
562562
*
563563
* @var string|null
564564
*/
565-
protected $password;
565+
protected $auth_server;
566566

567567
/**
568-
* Docusign Integrator Key
568+
* Docusign RSA Private Key
569569
*
570570
* @var string|null
571571
*/
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;
573587

574588
/**
575589
* Docusign Account Id
@@ -578,6 +592,13 @@ class Client
578592
*/
579593
protected $account_id;
580594

595+
/**
596+
* Docusign Jwt Scope
597+
*
598+
* @var string|null
599+
*/
600+
protected $jwt_scope;
601+
581602
/**
582603
* Container for all instantiated api objects
583604
* @var array
@@ -599,35 +620,32 @@ class Client
599620
public function __construct(array $params = [])
600621
{
601622
$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";
604625
$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;
605628
}
606629

607630
/**
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]
609632
*
610-
* OPTIONAL $options ['host' => $host]
633+
* OPTIONAL $options ['host' => $host, 'auth_server' => $auth_server, 'jwt_scope' => $jwt_scope ]
611634
*
612635
* @param array $options
613636
* @return Configuration
614637
*/
615638
public function createConfiguration(array $options = []): Configuration
616639
{
617-
foreach (['username', 'password', 'integrator_key'] as $requiredKey) {
640+
foreach (['impersonated_user_id', 'integrator_key', 'private_key'] as $requiredKey) {
618641
if (empty($options[$requiredKey])) {
619642
throw new \InvalidArgumentException(
620643
"Cannot create configuration. [$requiredKey => \$value] is missing or empty"
621644
);
622645
}
623646
}
624647

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);
631649
}
632650

633651
/**
@@ -636,10 +654,12 @@ public function createConfiguration(array $options = []): Configuration
636654
public function getConfiguration(): Configuration
637655
{
638656
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,
643663
]);
644664
}
645665

@@ -699,8 +719,6 @@ public function __call($method, $args)
699719
}
700720

701721
return $docusignModel;
702-
703-
704722
}
705723

706724
/**
@@ -733,25 +751,40 @@ public function __get($name)
733751
public function authenticate(): self
734752
{
735753
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];
739756
$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-
}
748757
}
749758

750759
$this->authenticated = true;
751760

752761
return $this;
753762
}
754763

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+
755788
/**
756789
* @param string $account_id
757790
* @return $this
@@ -777,7 +810,7 @@ public function isAuthenticated(): bool
777810
*/
778811
public function getAccountId(): string
779812
{
780-
if (null === $this->account_id) {
813+
if ($this->account_id === null) {
781814
$this->authenticate();
782815
}
783816

0 commit comments

Comments
 (0)