Skip to content

Commit

Permalink
Comment the signature class, giving methods descriptive phpdocs
Browse files Browse the repository at this point in the history
  • Loading branch information
eddturtle committed Feb 16, 2016
1 parent 842f194 commit 5d8fdb5
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions src/Signature.php
Expand Up @@ -2,6 +2,10 @@

namespace EddTurtle\DirectUpload;

/**
* Class Signature
* @package EddTurtle\DirectUpload
*/
class Signature
{

Expand Down Expand Up @@ -29,6 +33,15 @@ class Signature
private $base64Policy = null;
private $signature = null;

/**
* Signature constructor.
*
* @param string $awsKey
* @param string $awsSecret
* @param string $bucketName
* @param string $regionName
* @param array $options
*/
public function __construct($awsKey, $awsSecret, $bucketName, $regionName, $options = [])
{
$this->key = $awsKey;
Expand All @@ -41,15 +54,19 @@ public function __construct($awsKey, $awsSecret, $bucketName, $regionName, $opti
}

/**
* @return string
* Build the form url for sending files, this will include the bucket and the region name.
*
* @return string the s3 bucket's url.
*/
public function getFormUrl()
{
return "//" . $this->bucket . "." . self::SERVICE . "-" . $this->region . ".amazonaws.com";
}

/**
* @return null
* Get an AWS Signature V4 generated.
*
* @return string the signature.
*/
public function getSignature()
{
Expand All @@ -62,7 +79,9 @@ public function getSignature()
}

/**
* @return array
* Generate the necessary inputs to go within the form.
*
* @return array of the form inputs.
*/
public function getFormInputs()
{
Expand All @@ -83,17 +102,25 @@ public function getFormInputs()
}

/**
* @return string
* Based on getFormInputs(), this will build up the html to go within the form.
*
* @return string html of hidden form inputs.
*/
public function getFormInputsAsHtml()
{
$html = "";
foreach ($this->getFormInputs() as $name => $value) {
$html = '<input type="hidden" name="' . $name . '" value="' . $value . '">';
$html .= '<input type="hidden" name="' . $name . '" value="' . $value . '">';
}
return $html;
}


// Where the magic begins ;)

/**
* Step 1: Generate the Scope
*/
protected function generateScope()
{
$scope = [
Expand All @@ -106,6 +133,9 @@ protected function generateScope()
$this->credentials = implode('/', $scope);
}

/**
* Step 2: Generate a Base64 Policy
*/
protected function generatePolicy()
{
$policy = [
Expand All @@ -125,6 +155,9 @@ protected function generatePolicy()
$this->base64Policy = base64_encode(json_encode($policy));
}

/**
* Step 3: Generate and sign the Signature (v4)
*/
protected function generateSignature()
{
$dateKey = hash_hmac('sha256', $this->getShortDateFormat(), 'AWS4' . $this->secret, true);
Expand All @@ -136,7 +169,7 @@ protected function generateSignature()
}


// Helper funcs.
// Helper functions

private function populateTime()
{
Expand Down

0 comments on commit 5d8fdb5

Please sign in to comment.