An open API service indexing awesome lists of open source software.

https://github.com/krakphp/hmac

Hmac Library
https://github.com/krakphp/hmac

Last synced: about 1 year ago
JSON representation

Hmac Library

Awesome Lists containing this project

README

          

# Hmac

Hmac is a library for handling hmac authentication. Look at the source code for documentation.

## Usage

```php
'CustomHmac', // authentication scheme. Authorization: :
'hasher' => new Hmac\StdHmacHasher(), // any HmacHasher instance, defaults to Base64HmacHasher(StdHmacHasher)
'hs_gen' => hmac\hmac_hashed_hs_gen(), // any hash string generator function
'time_gen' => hmac\hmac_date_time_gen() // any time_gen function for generating a unit of time
'time_header' => 'Date',
'auth_header' => 'Authorization',
]);

$sign = hmac\hmac_sign_request($config);
$auth = hmac\hmac_auth_request($provider, $config);
```

### Signers

```
hmac_sign_request(HmacConfing = null);
hmac_psr7_sign_request(HmacConfing = null);
hmac_psr7_sign($sign);
```

`hmac_sign_request` is the default signer which creates sign function that accepts an `HmacRequest` and `HmacKeyPair`.

`hmac_psr7_sign_request` is a decorated signer around the hmac_sign_request for accepting and returning Psr Http Requests. It simply just wraps `hmac_sign_request` around the `hmac_psr7_sign` decorator.

`hmac_psr7_sign` is a decorator sign function that accepts a sign function and wraps it and returns a sign function that will accept and return Psr Http Requests.

### Hash String Generator

The hash string generators are functions that take the request and time value and generate the string that will end up being hashed.

provided `hs_gen` funcs:

// returns a hs_gen that will md5 hash the content and join everything by the given separator
hmac_hashed_hs_gen($sep = "\n");

example:

```php
getUri() . $time;
};
}
```

### Time Generator

A time generator is just used to generate a time unit like a timestamp or date stamp.

provided `time_gen` funcs:

// returns the `time` function which creates a unix timestamp
hmac_ts_time_gen();

// returns a time_gen which creates an RFC 2822 formatted date
hmac_date_time_gen();

## Integration

### GuzzleHttp

Simple integration with Guzzle can be done via the `Provider/guzzle.php` functions.

```php
push(guzzle_hmac_middleware(hmac_psr7_sign_request(), $keypair));
$client = new Client(['handler' => $handler]);
```