Skip to content

Commit

Permalink
Merge ca1d0c3 into ba3b78b
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentchalamon committed Dec 20, 2019
2 parents ba3b78b + ca1d0c3 commit bb483cf
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/configure-docusign.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ docusign:
private_key: "Path/To/Your/PrivateKey.pem"
integration_key: "YourIntegrationKey"
user_guid: "YourUserId"
grant_type: authorization_code # Grant type (authorization_code or implicit)
```

Next: [Configure the bundle](configure-the-bundle.md)
Expand Down
1 change: 1 addition & 0 deletions doc/configure-the-bundle.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ docusign:
private_key: "YourPrivateKey"
integration_key: "YourIntegrationKey"
user_guid: "YourUserId"
grant_type: authorization_code # Grant type (authorization_code or implicit)

# Your DocuSign account id
account_id: "YourAccountId"
Expand Down
1 change: 1 addition & 0 deletions features/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
'private_key' => '%kernel.project_dir%/var/jwt/docusign.pem',
'integration_key' => $_SERVER['DOCUSIGN_INTEGRATION_KEY'],
'user_guid' => $_SERVER['DOCUSIGN_USER_GUID'],
'grant_type' => 'authorization_code',
],
'sign_path' => '/docusign/sign',
'callback' => 'embedded_callback',
Expand Down
11 changes: 9 additions & 2 deletions src/Controller/Consent.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,32 @@ class Consent
{
public const DEMO_CONSENT_URI = 'https://account-d.docusign.com/oauth/auth';
public const CONSENT_URI = 'https://account.docusign.com/oauth/auth';
public const RESPONSE_TYPE = [
'authorization_code' => 'code',
'implicit' => 'token',
];

private $envelopeBuilder;
private $router;
private $consentUri;
private $integrationKey;
private $responseType;

public function __construct(EnvelopeBuilderInterface $envelopeBuilder, RouterInterface $router, string $consentUri, string $integrationKey)
public function __construct(EnvelopeBuilderInterface $envelopeBuilder, RouterInterface $router, string $consentUri, string $integrationKey, string $responseType)
{
$this->envelopeBuilder = $envelopeBuilder;
$this->router = $router;
$this->consentUri = $consentUri;
$this->integrationKey = $integrationKey;
$this->responseType = $responseType;
}

public function __invoke(): RedirectResponse
{
return new RedirectResponse(sprintf(
'%s?response_type=token&scope=signature%%20impersonation&client_id=%s&redirect_uri=%s',
'%s?response_type=%s&scope=signature%%20impersonation&client_id=%s&redirect_uri=%s',
$this->consentUri,
$this->responseType,
$this->integrationKey,
CallbackRouteGenerator::getCallbackRoute($this->router, $this->envelopeBuilder)
), 301);
Expand Down
6 changes: 6 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ public function getConfigTreeBuilder(): TreeBuilder
->info('Obtain your user UID (also called API username) from DocuSign Admin > Users > User > Actions > Edit')
->end()
->integerNode('ttl')->defaultValue(3600)->info('Token TTL in seconds (default: 3600)')->end()
->enumNode('grant_type')
->values(['authorization_code', 'implicit'])
->info('Grant type to use: authorization_code or implicit.')
->cannotBeEmpty()
->defaultValue('authorization_code')
->end()
->end()
->end()
->arrayNode('storage')
Expand Down
6 changes: 6 additions & 0 deletions src/DependencyInjection/DocusignExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use DocusignBundle\EnvelopeCreator\EnvelopeCreator;
use DocusignBundle\EnvelopeCreator\SendEnvelope;
use DocusignBundle\EnvelopeCreator\TraceableEnvelopeBuilderCallable;
use DocusignBundle\Exception\InvalidGrantTypeException;
use DocusignBundle\Grant\GrantInterface;
use DocusignBundle\Grant\JwtGrant;
use DocusignBundle\Routing\DocusignLoader;
Expand Down Expand Up @@ -127,10 +128,15 @@ public function load(array $configs, ContainerBuilder $container): void
]);

if (!empty($value['auth_jwt'])) {
if (!isset(Consent::RESPONSE_TYPE[$value['auth_jwt']['grant_type']])) {
throw new InvalidGrantTypeException('Grant type '.$value['auth_jwt']['grant_type'].' is not valid. '.'Please select one of the followings: authorization_code, implicit.');
}

$container->register("docusign.consent.$name", Consent::class)
->setAutowired(true)
->setPublic(true)
->setArguments([
'$responseType' => Consent::RESPONSE_TYPE[$value['auth_jwt']['grant_type']],
'$envelopeBuilder' => new Reference("docusign.envelope_builder.$name"),
'$consentUri' => $value['demo'] ? Consent::DEMO_CONSENT_URI : Consent::CONSENT_URI,
'$integrationKey' => $value['auth_jwt']['integration_key'],
Expand Down
18 changes: 18 additions & 0 deletions src/Exception/InvalidGrantTypeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the DocusignBundle.
*
* (c) Grégoire Hébert <gregoire@les-tilleuls.coop>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace DocusignBundle\Exception;

final class InvalidGrantTypeException extends \RuntimeException
{
}
66 changes: 66 additions & 0 deletions tests/Controller/ConsentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* This file is part of the DocusignBundle.
*
* (c) Grégoire Hébert <gregoire@les-tilleuls.coop>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace DocusignBundle\Tests\Controller;

use DocusignBundle\Controller\Consent;
use DocusignBundle\EnvelopeBuilderInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Router;
use Symfony\Component\Routing\RouterInterface;

/**
* @author Vincent Chalamon <vincent@les-tilleuls.coop>
*/
final class ConsentTest extends TestCase
{
/**
* @dataProvider getData
*/
public function testItRedirectsToValidUri(string $responseType, string $expected): void
{
/** @var EnvelopeBuilderInterface|ObjectProphecy $envelopeBuilderMock */
$envelopeBuilderMock = $this->prophesize(EnvelopeBuilderInterface::class);
/** @var RouterInterface|ObjectProphecy $routerMock */
$routerMock = $this->prophesize(RouterInterface::class);

$envelopeBuilderMock->getEnvelopeId()->willReturn('12345')->shouldBeCalledTimes(1);
$envelopeBuilderMock->getCallbackParameters()->willReturn(['foo' => 'bar'])->shouldBeCalledTimes(1);
$envelopeBuilderMock->getCallback()->willReturn('docusign_callback')->shouldBeCalledTimes(1);
$routerMock
->generate('docusign_callback', ['envelopeId' => '12345', 'foo' => 'bar'], Router::ABSOLUTE_URL)
->willReturn('https://www.example.com/docusign/callback')
->shouldBeCalledTimes(1);

$consent = new Consent(
$envelopeBuilderMock->reveal(),
$routerMock->reveal(),
Consent::DEMO_CONSENT_URI,
'c3b2d475-2cbd-47f5-a903-9b3aa0fefe5b',
$responseType
);
$response = $consent();
$this->assertInstanceOf(RedirectResponse::class, $response);
$this->assertEquals($expected, $response->getTargetUrl());
}

public function getData(): array
{
return [
['code', Consent::DEMO_CONSENT_URI.'?response_type=code&scope=signature%20impersonation&client_id=c3b2d475-2cbd-47f5-a903-9b3aa0fefe5b&redirect_uri=https://www.example.com/docusign/callback'],
['token', Consent::DEMO_CONSENT_URI.'?response_type=token&scope=signature%20impersonation&client_id=c3b2d475-2cbd-47f5-a903-9b3aa0fefe5b&redirect_uri=https://www.example.com/docusign/callback'],
];
}
}
4 changes: 4 additions & 0 deletions tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function testItLoadsDefaultConfig(): void
'integration_key' => 'e6a5e84b-8f67-4f18-ad35-2cce6a5814c6',
'user_guid' => 'be385011-09a7-4bbf-bfb4-9f9f06f9c8d9',
'ttl' => 1600,
'grant_type' => 'authorization_code',
],
'account_id' => 1234567,
'default_signer_name' => 'Grégoire Hébert',
Expand All @@ -71,6 +72,7 @@ public function testItLoadsDefaultConfig(): void
'integration_key' => 'e6a5e84b-8f67-4f18-ad35-2cce6a5814c6',
'user_guid' => 'be385011-09a7-4bbf-bfb4-9f9f06f9c8d9',
'ttl' => 1600,
'grant_type' => 'authorization_code',
],
'account_id' => 1234567,
'default_signer_name' => 'Grégoire Hébert',
Expand Down Expand Up @@ -125,6 +127,7 @@ public function testItNormalizesConfig(): void
'integration_key' => 'e6a5e84b-8f67-4f18-ad35-2cce6a5814c6',
'user_guid' => 'be385011-09a7-4bbf-bfb4-9f9f06f9c8d9',
'ttl' => 2400,
'grant_type' => 'authorization_code',
],
'account_id' => 1234567,
'default_signer_name' => 'Grégoire Hébert',
Expand Down Expand Up @@ -161,6 +164,7 @@ public function testItNormalizesConfig(): void
'integration_key' => 'e6a5e84b-8f67-4f18-ad35-2cce6a5814c6',
'user_guid' => 'be385011-09a7-4bbf-bfb4-9f9f06f9c8d9',
'ttl' => 2400,
'grant_type' => 'authorization_code',
],
'account_id' => 1234567,
'default_signer_name' => 'Grégoire Hébert',
Expand Down
2 changes: 2 additions & 0 deletions tests/DependencyInjection/DocusignExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class DocusignExtensionTest extends TestCase
'private_key' => '%kernel.project_dir%/var/jwt/docusign.pem',
'integration_key' => 'f19cf430-4a00-491a-9dfe-af1a24323513',
'user_guid' => 'c4ba97f8-a3a9-4c1e-b4a8-ee529763bada',
'grant_type' => 'authorization_code',
],
'account_id' => 5625922,
'default_signer_name' => 'Grégoire Hébert',
Expand All @@ -64,6 +65,7 @@ class DocusignExtensionTest extends TestCase
'private_key' => '%kernel.project_dir%/var/jwt/docusign.pem',
'integration_key' => '2b616b94-f56a-4221-95cb-a915fe427e5d',
'user_guid' => '5ea2b503-51af-4059-8b16-21efc6a0577b',
'grant_type' => 'authorization_code',
],
'account_id' => 5625922,
'default_signer_name' => 'Grégoire Hébert',
Expand Down
1 change: 1 addition & 0 deletions tests/config/docusign.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ docusign:
private_key: '%kernel.project_dir%/var/jwt/docusign.pem'
integration_key: 'ee54b24f-eabf-420d-b628-51edd78669dd'
user_guid: '8fa60e09-0cb8-40ca-af5d-3e25f62e262b'
grant_type: authorization_code
account_id: 1234567
default_signer_name: 'Grégoire Hébert'
default_signer_email: 'gregoire@les-tilleuls.coop'
Expand Down

0 comments on commit bb483cf

Please sign in to comment.