Skip to content

Commit

Permalink
Begin migration to version 2, which only supports PHP 7
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-scott committed Apr 23, 2016
1 parent 6720f2a commit 6884f12
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ language: php

php:

- 5.6

- 7.0

install:

Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@
}
},
"require": {
"php": "^5.6|^7.0",
"paragonie/constant_time_encoding": "^1|^2",
"paragonie/random_compat": "^1|^2"
"php": "^7.0",
"paragonie/constant_time_encoding": "^2"
},
"require-dev": {
"phpunit/phpunit": "^4|^5"
Expand Down
36 changes: 20 additions & 16 deletions src/AntiCSRF.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php
declare(strict_types=1);
namespace ParagonIE\AntiCSRF;

use \ParagonIE\ConstantTime\Base64;
use \ParagonIE\ConstantTime\Binary;
use \ParagonIE\ConstantTime\{
Base64,
Binary
};

/**
* Copyright (c) 2015 - 2016 Paragon Initiative Enterprises <https://paragonie.com>
Expand Down Expand Up @@ -98,10 +101,10 @@ public function __construct(
* Insert a CSRF token to a form
*
* @param string $lockTo This CSRF token is only valid for this HTTP request endpoint
* @param boolean $echo if true, echo instead of returning
* @param bool $echo if true, echo instead of returning
* @return string
*/
public function insertToken($lockTo = null, $echo = true)
public function insertToken(string $lockTo = '', bool $echo = true): string
{
$token_array = $this->getTokenArray($lockTo);
$ret = \implode(
Expand All @@ -127,33 +130,34 @@ function($key, $value) {
/**
* @return string
*/
public function getSessionIndex()
public function getSessionIndex(): string
{
return $this->sessionIndex;
}

/**
* @return string
*/
public function getFormIndex()
public function getFormIndex(): string
{
return $this->formIndex;
}

/**
* @return string
*/
public function getFormToken()
public function getFormToken(): string
{
return $this->formToken;
}

/**
* Retrieve a token array for unit testing endpoints
*
* @param string $lockTo
* @return array
*/
public function getTokenArray($lockTo = null)
public function getTokenArray(string $lockTo = ''): array
{
if (!isset($this->session[$this->sessionIndex])) {
$this->session[$this->sessionIndex] = [];
Expand Down Expand Up @@ -194,9 +198,9 @@ public function getTokenArray($lockTo = null)
/**
* Validate a request based on $this->session and $this->post data
*
* @return boolean
* @return bool
*/
public function validateRequest()
public function validateRequest(): bool
{
if (!isset($this->session[$this->sessionIndex])) {
// We don't even have a session array initialized
Expand Down Expand Up @@ -259,7 +263,7 @@ public function validateRequest()
isset($this->server['REMOTE_ADDR'])
? $this->server['REMOTE_ADDR']
: '127.0.0.1',
\base64_decode($stored['token']),
Bass64::decode($stored['token']),
true
)
);
Expand All @@ -274,7 +278,7 @@ public function validateRequest()
* @param array $options
* @return self
*/
public function reconfigure(array $options = [])
public function reconfigure(array $options = []): self
{
foreach ($options as $opt => $val) {
switch ($opt) {
Expand Down Expand Up @@ -302,7 +306,7 @@ public function reconfigure(array $options = [])
* @param string $lockTo What URI endpoint this is valid for
* @return string[]
*/
protected function generateToken($lockTo)
protected function generateToken(string $lockTo): array
{
$index = Base64::encode(\random_bytes(18));
$token = Base64::encode(\random_bytes(33));
Expand Down Expand Up @@ -344,8 +348,8 @@ protected function recycleTokens()
// Sort by creation time
\uasort(
$this->session[$this->sessionIndex],
function($a, $b) {
return $a['created'] - $b['created'];
function ($a, $b) {
return $a['created'] <=> $b['created'];
}
);

Expand All @@ -362,7 +366,7 @@ function($a, $b) {
* @param string $untrusted
* @return string
*/
protected static function noHTML($untrusted)
protected static function noHTML(string $untrusted): string
{
return \htmlentities($untrusted, ENT_QUOTES, 'UTF-8');
}
Expand Down
3 changes: 1 addition & 2 deletions tests/AntiCSRFTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

use \ParagonIE\AntiCSRF\AntiCSRF;

class AntiCSRFTest extends PHPUnit_Framework_TestCase
Expand All @@ -14,7 +13,7 @@ public function testInsertToken()
$server = $_SERVER;

$csrft = new AntiCSRF($post, $session, $server);
$token_html = $csrft->insertToken(null, false);
$token_html = $csrft->insertToken('', false);

$idx = $csrft->getSessionIndex();
$this->assertFalse(
Expand Down

0 comments on commit 6884f12

Please sign in to comment.