|
| 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