Coder Social home page Coder Social logo

jessety / simple-hmac-auth-php Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 16 KB

PHP library for interfacing with APIs that implement hmac signatures

License: MIT License

PHP 100.00%
php api-security hmac-authentication request-signing request-signatures simple-hmac-auth

simple-hmac-auth-php's Introduction

simple-hmac-auth-php

PHP library for interfacing with APIs that implement HMAC signatures. Designed for use with a JSON API that implements simple-hmac-auth.

Usage

Instantiate a client object and point it at your service

require_once('SimpleHMACAuthClient.php');

$client = new SimpleHMACAuthClient('API_KEY', 'SECRET');

$client->host = 'localhost';
$client->port = 8000;
$client->ssl = false;

GET request

try {
  
  $items = $client->call('GET', '/items/');

  die('Success: <pre>' . json_encode($items, JSON_PRETTY_PRINT) . '</pre>');

} catch (AuthenticationException $e) {

  die('Failure: <pre>' . json_encode($e, JSON_PRETTY_PRINT) . '</pre>');
}

POST request with body data and query string

try {

  $query = array(
    'debug' => true
  );

  $data = array(
    'test' => true,
    'created' => microtime(true)
  );

  // Create a new item
  $client->call('POST', '/items/', $query, $data);
  
  die('Success!');

} catch (AuthenticationException $e) {

  die('Failure: <pre>' . json_encode($e, JSON_PRETTY_PRINT) . '</pre>');
}

Client Subclass

To write a client for your service, simply extend the class and add functions that match your API routes.

class SampleClient extends SimpleHMACAuthClient {

  public function __construct($apiKey, $secret) {
    parent::__construct($apiKey, $secret);

    // Define the host / port / SSL of your service in the constructor
    $this->host = 'localhost';
    $this->port = 8000;
    $this->ssl = false;
  }

  public function create($data = array()) {

    return $this->call('POST', '/items/', null, $data);
  }

  public function query($parameters = array()) {

    return $this->call('GET', '/items/', $parameters);
  }

  public function detail($id = null) {

    if ($id === null) {
      throw new AuthenticationException('Missing \'id\' parameter');
    }

    return $this->call('GET', '/items/' . rawurlencode($id));
  }

  public function update($id = null, $data = array()) {

    if ($id === null) {
      throw new AuthenticationException('Missing \'id\' parameter');
    }

    return $this->call('POST', '/items/' . rawurlencode($id), null, $data);
  }

  public function delete($id = null) {

    if ($id === null) {
      throw new AuthenticationException('Missing \'id\' parameter');
    }

    return $this->call('DELETE', '/items/' . rawurlencode($id));
  }
}

Because this client's constructor specified the host, port, and SSL status of the service, it can be instantiated without any parameters beyond apiKey and secret.

$client = new SampleClient('API_KEY', 'SECRET'); 

try {

  $parameters = array(
    'debug' => true,
    'limit' => 42
  );

  $items = $client->query($parameters);

  die('Success: <pre>' . json_encode($items, JSON_PRETTY_PRINT) . '</pre>');

} catch (AuthenticationException $e) {

  die('Failure: <pre>' . json_encode($e, JSON_PRETTY_PRINT) . '</pre>');
}

simple-hmac-auth-php's People

Contributors

jessety avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.