Examples
<?php $privateKey = "1fa80a88af0273294bcc3ec7dbe1e255f9c7cb92de234b92c1ffe540afb487a1059d3d6b068a781c381b10fbc138bf512df9d401ffe7097dd8a0b8101a4de758"; $publicKey = "47b9d96c506329efcad87ca24ddeab4be67b56fd872bb63226763f7d4e704dc1fc0bda49b19d1fc69c8d3e8ab20873fadb8c686d416b478482ec6d8e6c9f76fa"; $uri = 'http://path_to_service/'; // Example: 'http://stage.api.adorbit.com/' $mediaType = 'application/vnd.adorbit+json;version=1.0'; // Construct the message using a combination of HTTP method and URI separated by a new line. Proper case is very important! $message = "GET\n{$uri}"; // Create the authentication signature. $signature = base64_encode(hash_hmac('sha512', $message, $privateKey)); $curl = curl_init(); // Set options prior to making the request (i.e. what media type we accept). curl_setopt_array($curl, array( CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => $uri, CURLOPT_HTTPHEADER => array("Accept: {$mediaType}", "Authorization: ADORBIT {$publicKey}:{$signature}", ), ) ); // Get the body of the response. $content = curl_exec($curl); // Get meta data from the response (i.e. response code, content type, etc.). $info = curl_getinfo($curl); curl_close($curl); // Confirm the response is okay, matches the expected content type, and can be decoded into JSON. if ($info['http_code'] === 200 && $info['content_type'] === $mediaType && $data = json_decode($content)) { // Do something with $data. }
<?php $privateKey = "1fa80a88af0273294bcc3ec7dbe1e255f9c7cb92de234b92c1ffe540afb487a1059d3d6b068a781c381b10fbc138bf512df9d401ffe7097dd8a0b8101a4de758"; $publicKey = "47b9d96c506329efcad87ca24ddeab4be67b56fd872bb63226763f7d4e704dc1fc0bda49b19d1fc69c8d3e8ab20873fadb8c686d416b478482ec6d8e6c9f76fa"; $uri = 'http://stage.api.adorbit.com/'; // Path to service $mediaType = 'application/vnd.adorbit+json;version=1.0'; // Construct the message using a combination of HTTP method and URI separated by a new line. Proper case is very important! $message = "GET\n{$uri}"; // Create the authentication signature. $signature = base64_encode(hash_hmac('sha512', $message, $privateKey)); $curl = curl_init(); // Set options prior to making the request (i.e. what media type we accept). curl_setopt_array($curl, array( CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => $uri, CURLOPT_HTTPHEADER => array("Accept: {$mediaType}", "Authorization: ADORBIT {$publicKey}:{$signature}", ), ) ); // Get the body of the response. $content = curl_exec($curl); // Get meta data from the response (i.e. response code, content type, etc.). $info = curl_getinfo($curl); curl_close($curl); // Confirm the response is okay, matches the expected content type, and can be decoded into JSON. if ($info['http_code'] === 200 && $info['content_type'] === $mediaType && $data = json_decode($content)) { $curl = curl_init(); // Set the new URI to retrieve companies. $uri = $data->companies; // Set the new media type. $mediaType = 'application/vnd.adorbit.companies+json;version=1.0'; $message = "GET\n{$uri}"; $signature = base64_encode(hash_hmac('sha512', $message, $privateKey)); curl_setopt_array($curl, array( CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => $uri, CURLOPT_HTTPHEADER => array("Accept: {$mediaType}", "Authorization: ADORBIT {$publicKey}:{$signature}", ), ) ); $content = curl_exec($curl); $info = curl_getinfo($curl); // Confirm the response is okay... if ($info['http_code'] === 200 && $info['content_type'] === $mediaType && $data = json_decode($content)) { // Do something with $data. } curl_close($curl); }
<?php /** * Generate and execute a request to retrieve data from the service. * * @param $uri Absolute path of URI * @param $mediaType What will be placed in the Accept header and confirmed in the returned response. * @return JSON-decoded object based on response. */ function makeRequest($uri, $mediaType) { $privateKey = "1fa80a88af0273294bcc3ec7dbe1e255f9c7cb92de234b92c1ffe540afb487a1059d3d6b068a781c381b10fbc138bf512df9d401ffe7097dd8a0b8101a4de758"; $publicKey = "47b9d96c506329efcad87ca24ddeab4be67b56fd872bb63226763f7d4e704dc1fc0bda49b19d1fc69c8d3e8ab20873fadb8c686d416b478482ec6d8e6c9f76fa"; $message = "GET\n{$uri}"; $signature = base64_encode(hash_hmac('sha512', $message, $privateKey)); $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => $uri, CURLOPT_HTTPHEADER => array("Accept: {$mediaType}", "Authorization: ADORBIT {$publicKey}:{$signature}", ), ) ); $content = curl_exec($curl); $info = curl_getinfo($curl); curl_close($curl); if ($info['http_code'] === 200 && $info['content_type'] === $mediaType && $data = json_decode($content)) { return $data; } else { throw new Exception('The response did not meet the expected criteria.'); } } // Get the list of available end-points. $rootData = makeRequest('http://stage.api.adorbit.com/', 'application/vnd.adorbit+json;version=1.0'); // Retrieve a list of companies. $companies = makeRequest($rootData->contacts, 'application/vnd.adorbit.contacts+json;version=1.0');
class AdOrbitAuthenticationToken { private readonly string _privateKey; public AdOrbitAuthenticationToken(string privateKey) { _privateKey = privateKey; } public string UsingMessage(string message) { var encoder = new UTF8Encoding(); byte[] keyBytes = encoder.GetBytes(_privateKey); byte[] messageBytes = encoder.GetBytes(message); byte[] hashBytes = new HMACSHA512(keyBytes).ComputeHash(messageBytes); var hexString = ToHexString(hashBytes); return Convert.ToBase64String(encoder.GetBytes(hexString)); } /// <summary> /// Convert a byte array to a 2-character wide hex string /// </summary> /// <param name="bytes"></param> /// <returns></returns> private static string ToHexString(byte[] bytes) { StringBuilder sb = new StringBuilder(bytes.Length * 2); foreach (byte b in bytes) { sb.AppendFormat("{0:x2}", b); } return sb.ToString(); } } Usage: string publicKey = "<publicKey>"; string privateKey = "<privateKey>"; var message = "GET\nhttp://gbm.api.adorbit.com/"; var token = new AdOrbitAuthenticationToken(privateKey).UsingMessage(message); var authentication = $"ADORBIT {publicKey}:{token}";
In this section: