Skip to content

Commit a685e44

Browse files
authored
Merge pull request #3 from docusign/consolidated-launcher
Major refactoring to implement OAuth methods and an entry-point script to use all the examples
2 parents a572e61 + 871b96e commit a685e44

38 files changed

+1605
-501
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
.DS_Store
22
**/.DS_Store
3+
*.pem
4+
ENVELOPE_ID
5+
TEMPLATE_ID
6+
config/
37

8+
debug.log

OAuth/code_grant.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
require 'utils.php';
4+
5+
$authorizationURL = $authorizationEndpoint . 'auth?' . http_build_query([
6+
'redirect_uri' => $redirectURI,
7+
'scope' => 'signature',
8+
'client_id' => $clientID,
9+
'state' => $state,
10+
'response_type' => 'code'
11+
]);
12+
13+
echo "\nOpen the following URL in a browser to continue:\n" . $authorizationURL . "\n";
14+
15+
16+
// on windows I cannot seem to escape the Ampersand so it throws the additional get parametrs as commads and errors the php script
17+
// if(stripos(PHP_OS, 'WIN') === 0){
18+
19+
// shell_exec("start $authorizationURL");
20+
21+
// }
22+
23+
// else {
24+
25+
// shell_exec("xdg-open $authorizationURL");
26+
// }
27+
28+
29+
30+
$auth = startHttpServer($socket);
31+
32+
if ($auth['state'] != $state) {
33+
echo "\nWrong 'state' parameter returned\n";
34+
exit(2);
35+
}
36+
37+
$code = $auth['code'];
38+
echo "\nGetting an access token...\n";
39+
40+
$response = http($authorizationEndpoint . 'token', [
41+
'grant_type' => 'authorization_code',
42+
'redirect_uri' => $redirectURI,
43+
'code' => $code
44+
], [
45+
'Authorization: Basic ' . base64_encode($clientID . ':' .$clientSecret),
46+
], true
47+
);
48+
49+
if (!isset($response->access_token)) {
50+
echo "\nError fetching access token\n";
51+
exit(2);
52+
}
53+
54+
$accessToken = $response->access_token;
55+
file_put_contents($outputFile, $accessToken);
56+
echo "\nAccess token has been written to " . $outputFile . "\n\n";
57+
58+
?>

OAuth/jwt.php

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
require 'utils.php';
4+
5+
$authorizationURL = $authorizationEndpoint . 'auth?' . http_build_query([
6+
'scope' => 'signature impersonation',
7+
'redirect_uri' => $redirectURI,
8+
'client_id' => $clientID,
9+
'state' => $state,
10+
'response_type' => 'code'
11+
]);
12+
13+
echo "\nOpen the following URL in a browser to continue:\n" . $authorizationURL . "\n";
14+
15+
16+
17+
18+
19+
$auth = startHttpServer($socket);
20+
21+
if ($auth['state'] != $state) {
22+
echo "\nWrong 'state' parameter returned\n";
23+
exit(2);
24+
}
25+
26+
$code = $auth['code'];
27+
echo "\nGetting an access token...\n";
28+
29+
$response = http($authorizationEndpoint . 'token', [
30+
'grant_type' => 'authorization_code',
31+
'redirect_uri' => $redirectURI,
32+
'code' => $code
33+
], [
34+
'Authorization: Basic ' . base64_encode($clientID . ':' . $clientSecret),
35+
], true
36+
);
37+
38+
if (!isset($response->access_token)) {
39+
echo "\nError fetching access token\n";
40+
exit(2);
41+
}
42+
43+
$accessToken = $response->access_token;
44+
echo "\nGetting user info...\n";
45+
46+
$userInfo = http($authorizationEndpoint . 'userinfo', false, [
47+
'Authorization: Bearer ' . $accessToken
48+
]);
49+
50+
$timestamp = date_timestamp_get(date_create());
51+
$userID = $userInfo->sub;
52+
$signature = '';
53+
54+
$header = encodeBase64URL(
55+
json_encode([
56+
'typ' => 'JWT',
57+
'alg' => 'RS256'
58+
])
59+
);
60+
61+
$payload = encodeBase64URL(
62+
json_encode([
63+
'sub' => $userID,
64+
'iss' => $clientID,
65+
'iat' => $timestamp,
66+
'exp' => $timestamp + 3000,
67+
'aud' => 'account-d.docusign.com',
68+
'scope' => 'signature impersonation'
69+
])
70+
);
71+
72+
$privateKey = file_get_contents("config/private.key");
73+
openssl_sign($header . '.' . $payload, $signature, $privateKey, 'sha256');
74+
echo "\nGetting a JWT access token...\n";
75+
76+
$response = http($authorizationEndpoint . 'token', [
77+
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
78+
'assertion' => $header . '.' . $payload . '.' . encodeBase64URL($signature)
79+
], false, true);
80+
81+
if(!$response->access_token){
82+
var_dump($response);
83+
}
84+
85+
86+
$accessToken = $response->access_token;
87+
file_put_contents($outputFile, $accessToken);
88+
echo "\nAccess token has been written to " . $outputFile . "\n\n";
89+
90+
?>

OAuth/jwt2.php

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
$PORT = '8080';
4+
$IP = 'localhost';
5+
6+
$outputFile = 'ds_access_token.txt';
7+
$state = bin2hex(random_bytes(5));
8+
9+
$clientID = getenv("CLIENT_ID");
10+
$clientSecret = getenv("CLIENT_SECRET");
11+
$authorizationEndpoint = 'https://account-d.docusign.com/oauth/';
12+
// $userID = '2448759b-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
13+
// echo getenv("USERID");
14+
$userID = getenv('USERID');
15+
16+
$socket = 'tcp://' . $IP . ':' . $PORT;
17+
$redirectURI = 'http://' . $IP . ':' . $PORT . '/authorization-code/callback';
18+
19+
function startHttpServer ($socket) {
20+
$responseOk = "HTTP/1.0 200 OK\r\n"
21+
. "Content-Type: text/plain\r\n\r\n"
22+
. "Ok. You may close this tab and return to the shell.\r\n";
23+
24+
$responseErr = "HTTP/1.0 400 Bad Request\r\n"
25+
. "Content-Type: text/plain\r\n\r\n"
26+
. "Bad Request\r\n";
27+
28+
ini_set('default_socket_timeout', 60 * 5);
29+
$server = stream_socket_server($socket, $errno, $errstr);
30+
31+
if (!$server) {
32+
Log::err('Error starting HTTP server');
33+
return false;
34+
}
35+
36+
do {
37+
$sock = stream_socket_accept($server);
38+
39+
if (!$sock) {
40+
Log::err('Error accepting socket connection');
41+
exit(1);
42+
}
43+
44+
$contentLength = 0;
45+
$headers = [];
46+
$body = null;
47+
48+
while (false !== ($line = trim(fgets($sock)))) {
49+
if ($line === '') break;
50+
$regex = '#^Content-Length:\s*([[:digit:]]+)\s*$#i';
51+
52+
if (preg_match($regex, $line, $matches)) {
53+
$contentLength = (int)$matches[1];
54+
}
55+
56+
$headers[] = $line;
57+
}
58+
59+
if ($contentLength > 0) {
60+
$body = fread($sock, $contentLength);
61+
}
62+
63+
list($method, $url, $httpver) = explode(' ', $headers[0]);
64+
65+
if ($method == 'GET') {
66+
$parts = parse_url($url);
67+
68+
if (isset($parts['path']) && $parts['path'] == '/authorization-code/callback' && isset($parts['query'])) {
69+
parse_str($parts['query'], $query);
70+
71+
if (isset($query['code']) && isset($query['state'])) {
72+
fwrite($sock, $responseOk);
73+
fclose($sock);
74+
return $query;
75+
}
76+
}
77+
}
78+
79+
fwrite($sock, $responseErr);
80+
fclose($sock);
81+
} while (true);
82+
}
83+
84+
function http ($url, $params = false, $headers = false, $post = false) {
85+
$ch = curl_init($url);
86+
87+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
88+
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
89+
curl_setopt($ch, CURLOPT_VERBOSE, 1);
90+
91+
if ($post) curl_setopt($ch, CURLOPT_POST, 1);
92+
93+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
94+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
95+
96+
if ($params) {
97+
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
98+
}
99+
100+
if ($headers) {
101+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
102+
}
103+
104+
$resp = curl_exec($ch);
105+
return json_decode($resp);
106+
}
107+
108+
function encodeBase64URL ($data) {
109+
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
110+
}
111+
112+
$timestamp = date_timestamp_get(date_create());
113+
$signature = '';
114+
115+
$header = encodeBase64URL(
116+
json_encode([
117+
'typ' => 'JWT',
118+
'alg' => 'RS256'
119+
])
120+
);
121+
122+
$payload = encodeBase64URL(
123+
json_encode([
124+
'sub' => $userID,
125+
'iss' => $clientID,
126+
'iat' => $timestamp,
127+
'exp' => $timestamp + 3000,
128+
'aud' => 'account-d.docusign.com',
129+
'scope' => 'signature impersonation'
130+
])
131+
);
132+
133+
$privateKey = file_get_contents("../private.key");
134+
$signature = openssl_sign($header . '.' . $payload, $signature, $privateKey, 'sha256');
135+
echo "\nGetting a JWT access token...\n";
136+
137+
$response = http($authorizationEndpoint . 'token', [
138+
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
139+
'assertion' => $header . '.' . $payload . '.' . encodeBase64URL($signature)
140+
], false, true);
141+
142+
143+
if(!$response->access_token){
144+
var_dump($response);
145+
}
146+
147+
148+
$accessToken = $response->access_token;
149+
file_put_contents($outputFile, $accessToken);
150+
echo "\nAccess token has been written to " . $outputFile . "\n\n";
151+
152+
?>

0 commit comments

Comments
 (0)