docusign / docusign-esign-php-client

The Official Docusign PHP Client Library used to interact with the eSign REST API. Send, sign, and approve documents using this client.
https://www.docusign.com/devcenter
MIT License
196 stars 123 forks source link

Setting up fast and simple authentication #188

Closed bastien70 closed 1 year ago

bastien70 commented 1 year ago

Hello, I need to integrate DocuSign into my Symfony 6 project.

In the future, I will have to ask clients to sign documents.

I am at the authentication stage.

i've :

        $integrator_key = 'xxxx'
        $userId = 'xxxx'
        $host = 'https://demo.docusign.net';

        $config = new Configuration();
        $config->setHost($host);

        $apiClient = new ApiClient($config);

        $userToken = $apiClient->requestJWTUserToken($integrator_key, $userId, 'rsa_private_key ????');

But I don't know what si the rsa_private_key, how to find this parameter in my DocuSign Developer account please ?

By the way, in case I just want to send documents and several people have to sign it, and nothing more, what authentication method do you recommend?

I tried to look what was in the generated project (from https://developers.docusign.com/docs/esign-rest-api/quickstart/?utm_medium=social&utm_source=youtube)

But I find that there are a lot of things, and I don't know if I really need all that. My goal would only be to authenticate myself only for a short time, the time to send the signature request, or to check the status of a document, so I think of reconnecting each time.

Is there a solution to do this simply?

EDIT:

Is something like this should be okay to authentication and send document ?

public function send()
    {
        $username = "VOTRE_NOM_D'UTILISATEUR_DOCUSIGN";
        $password = "VOTRE_MOT_DE_PASSE_DOCUSIGN";
        $integratorKey = "VOTRE_CLE_D'INTEGRATEUR_DOCUSIGN";

        // Authenticate
        $config = new \DocuSign\eSign\Configuration();
        $config->setHost("https://demo.docusign.net/restapi");
        $config->addDefaultHeader("X-DocuSign-Authentication", "{\"Username\":\"" . $username . "\",\"Password\":\"" . $password . "\",\"IntegratorKey\":\"" . $integratorKey . "\"}");
        $apiClient = new \DocuSign\eSign\Client\ApiClient($config);
        $authenticationApi = new AuthenticationApi($apiClient);
        $loginInfo = $authenticationApi->login();

        // Get accountId from loginInfo
        $accountId = $loginInfo->getLoginAccounts()[0]->getAccountId();

        // Create envelope definition
        $document = new \DocuSign\eSign\Model\Document();
        $document->setDocumentBase64(base64_encode(file_get_contents('path/to/pdf.pdf')));
        $document->setName('pdf.pdf');
        $document->setDocumentId('1');

        $signer = new \DocuSign\eSign\Model\Signer();
        $signer->setEmail('signer1@example.com');
        $signer->setName('Signer 1');
        $signer->setRecipientId('1');
        $signer->setRoutingOrder('1');

        $signer2 = new \DocuSign\eSign\Model\Signer();
        $signer2->setEmail('signer2@example.com');
        $signer2->setName('Signer 2');
        $signer2->setRecipientId('2');
        $signer2->setRoutingOrder('2');

        $recipients = new \DocuSign\eSign\Model\Recipients();
        $recipients->setSigners(array($signer, $signer2));

        $envelopeDefinition = new \DocuSign\eSign\Model\EnvelopeDefinition();
        $envelopeDefinition->setEmailSubject('Please sign this document');
        $envelopeDefinition->setDocuments(array($document));
        $envelopeDefinition->setRecipients($recipients);
        $envelopeDefinition->setStatus('sent');

        // Create and send envelope
        $envelopesApi = new EnvelopesApi($apiClient);
        try {
                        $envelopeSummary = $envelopesApi->createEnvelope($accountId, $envelopeDefinition);
            return new Response('Envelope sent. Envelope ID: ' . $envelopeSummary->getEnvelopeId());
        } catch (ApiException $e) {
            return new Response('Error while sending envelope: ' . $e->getMessage(), $e->getCode());
        }
    }

It's especially the authentication part that bothers me. I'm looking for something quick and easy.

smd9788 commented 1 year ago

Hello @bastien70 , you will need to generate a new RSA Keypair on your integration key in DocuSign. The keypair will consist of a public and private key and you will use the private key in your application config as the _rsa_privatekey property. This link has instructions to create an RSA keypair:

https://developers.docusign.com/platform/configure-app/#add-the-rsa-key-pair

In terms of which OAuth method is best for your use case, Authorization Code Grant (ACG) is often considered to be easier and faster to implement, but between ACG and JWT Grant, each have their own pros and cons. Please take a look at this page for the pros, cons, and differences between the two:

https://developers.docusign.com/platform/auth/choose/

Please keep in mind, that ACG does not use an RSA keypair like JWT does, but instead uses a secret key.

I have a couple comments on the code you provided here. The host URL you should be authenticating against is "https://account-d.docusign.com", which is the DocuSign account server. When you create envelopes or make other calls, you will use the "https://demo.docusign.net". My other comment is, it looks like you are using "Legacy (Basic) Authentication", by using the "X-DocuSign-Authentication" header and username/password/integrationkey combination. This is no longer supported on new integrations and you must use OAuth 2.0 like ACG or JWT instead.