Skip to content

Commit cd283fe

Browse files
mabarMilan Felix Šulc
authored andcommitted
PHP 7.1, improved BasicAuthenticator (#7)
1 parent 71dc575 commit cd283fe

22 files changed

+275
-272
lines changed

.docs/README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,21 @@ extensions:
6565
auth: Contributte\Http\DI\BasicAuthExtension
6666
```
6767

68-
You have `enable` this extension by yourself. It's disabled by default. Users is array of username => password.
68+
You have `enable` this extension by yourself. It's disabled by default.
6969

7070
```yaml
7171
auth:
7272
enabled: true/false
7373
title: My security zone
7474
users:
75-
username1: password1
76-
username2: password2
77-
username3: password3
75+
username1:
76+
password: password1
77+
unsecured: true
78+
username2:
79+
password: $2y$10$p.U5q.BuQp02srggig.VDOqj5m7pE1rCwKavVQ3S2TrqWlkqu3qlC
80+
unsecured: false # secured by default
81+
username3:
82+
password: $2y$10$bgievYVQMzsRn5Ysup.NKOVUk66aitAniAmts2EJAa91eqkAhohvC
7883
```
7984

8085
## Useful classes

.editorconfig

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
# EditorConfig is awesome: http://EditorConfig.org
22

3-
# Top-most EditorConfig file
43
root = true
54

6-
# Unix-style newlines with a newline ending every file
75
[*]
6+
charset = utf-8
87
end_of_line = lf
98
insert_final_newline = true
10-
11-
# JS / PHP
12-
[*.{js,php,phpt}]
13-
charset = utf-8
9+
trim_trailing_whitespace = true
1410
indent_style = tab
15-
indent_size = 4
11+
indent_size = tab
12+
tab_width = 4
1613

17-
# NEON
18-
[*.neon]
19-
charset = utf-8
20-
indent_style = tab
14+
[*.md]
15+
indent_style = space
2116
indent_size = 4
2217

23-
# Composer, NPM, Travis, BitbucketPipelines
24-
[{composer.json,package.json,.travis.yml,bitbucket-pipelines.yml}]
18+
[{composer.json,package.json,.travis.yml}]
2519
indent_style = space
2620
indent_size = 2

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
/composer.lock
77

88
# Tests
9+
/temp/
910
/tests/*.log
1011
/tests/tmp
1112
/tests/coverage.html
13+
/coverage.xml

.travis.yml

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,58 @@
11
language: php
22

33
php:
4-
- 5.6
5-
- 7.0
64
- 7.1
7-
- hhvm
5+
- 7.2
86

9-
matrix:
10-
fast_finish: true
7+
before_install:
8+
# turn off XDebug
9+
- phpenv config-rm xdebug.ini || return 0
1110

12-
allow_failures:
13-
- php: hhvm
14-
15-
include:
16-
- php: 5.6
17-
env: COMPOSER_FLAG=--prefer-lowest
18-
- php: 5.6
19-
env: COMPOSER_FLAG=--prefer-stable
20-
- php: 7.0
21-
env: COMPOSER_FLAG=--prefer-lowest
22-
- php: 7.0
23-
env: COMPOSER_FLAG=--prefer-stable
24-
- php: 7.1
25-
env: COMPOSER_FLAG=--prefer-lowest
26-
- php: 7.1
27-
env: COMPOSER_FLAG=--prefer-stable
28-
29-
before_script:
11+
install:
3012
# Composer
31-
- travis_retry composer install --no-interaction
32-
# Coverage
33-
- if [[ "$TRAVIS_PHP_VERSION" == "7.1" && "$COMPOSER_FLAG" == "" ]]; then COVERAGE=1; fi
13+
- travis_retry composer install --no-progress --prefer-dist
3414

3515
script:
36-
# Quality Assurance
37-
- travis_retry composer qa
38-
39-
# Nette\Tester
16+
# Nette/Tester
4017
- composer run-script tester
4118

42-
# Nette\Tester + CodeCoverage
43-
- if [ "$COVERAGE" != "" ]; then composer tester-coverage; fi
19+
jobs:
20+
include:
21+
- env: title="Lowest Dependencies 7.1"
22+
php: 7.1
23+
install:
24+
- travis_retry composer update --no-progress --prefer-dist --prefer-lowest
25+
script:
26+
- composer run-script tester
27+
28+
- env: title="Lowest Dependencies 7.2"
29+
php: 7.2
30+
install:
31+
- travis_retry composer update --no-progress --prefer-dist --prefer-lowest
32+
script:
33+
- composer run-script tester
34+
35+
- stage: Quality Assurance
36+
php: 7.2
37+
script:
38+
- composer run-script qa
39+
40+
- stage: Test Coverage
41+
php: 7.2
42+
script:
43+
- composer run-script coverage
44+
after_script:
45+
- wget https://github.com/php-coveralls/php-coveralls/releases/download/v2.0.0/php-coveralls.phar
46+
- php php-coveralls.phar --verbose --config tests/.coveralls.yml
4447

45-
after_script:
46-
# Coverage (Coveralls)
47-
- if [ "$COVERAGE" != "" ]; then wget https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coveralls.phar; fi
48-
- if [ "$COVERAGE" != "" ]; then php coveralls.phar --verbose --config tests/.coveralls.yml; fi
48+
- stage: Phpstan
49+
php: 7.2
50+
script:
51+
- composer run-script phpstan-install
52+
- composer run-script phpstan
53+
54+
allow_failures:
55+
- stage: Test Coverage
4956

5057
after_failure:
5158
# Print *.actual content

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ composer require contributte/http
2626

2727
| State | Version | Branch | PHP |
2828
|-------------|---------|----------|----------|
29+
| dev | `^0.3` | `master` | `>= 7.1` |
30+
| stable | `^0.2` | `master` | `>= 7.1` |
2931
| stable | `^0.1` | `master` | `>= 5.6` |
3032

3133
## Overview
@@ -48,6 +50,13 @@ composer require contributte/http
4850
</br>
4951
<a href="https://github.com/f3l1x">Milan Felix Šulc</a>
5052
</td>
53+
<td align="center">
54+
<a href="https://github.com/mabar">
55+
<img width="150" height="150" src="https://avatars0.githubusercontent.com/u/20974277?s=400&v=4">
56+
</a>
57+
</br>
58+
<a href="https://github.com/mabar">Marek Bartoš</a>
59+
</td>
5160
</tr>
5261
<tbody>
5362
</table>

composer.json

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
{
22
"name": "contributte/http",
33
"description": "Extra contrib to nette/http",
4-
"keywords": ["nette", "http", "request", "response", "curl", "auth"],
4+
"keywords": [
5+
"nette",
6+
"http",
7+
"request",
8+
"response",
9+
"curl",
10+
"auth"
11+
],
512
"type": "library",
6-
"license": "MIT",
13+
"license": [
14+
"MIT"
15+
],
716
"homepage": "https://github.com/contributte/http",
817
"authors": [
918
{
@@ -12,13 +21,18 @@
1221
}
1322
],
1423
"require": {
15-
"php": ">= 5.6",
16-
"nette/http": "^2.4.5"
24+
"php": ">= 7.1",
25+
"nette/http": "~2.4.9"
1726
},
1827
"require-dev": {
19-
"ninjify/qa": "^0.4.0",
20-
"ninjify/nunjuck": "^0.1.4",
21-
"nette/di": "^2.4.8"
28+
"ninjify/qa": "^0.8.0",
29+
"ninjify/nunjuck": "^0.2.0",
30+
"nette/di": "~2.4.13",
31+
"tracy/tracy": "~2.5.1"
32+
},
33+
"conflict": {
34+
"nette/di": "<2.4.13",
35+
"nette/utils": "<2.5.2"
2236
},
2337
"suggest": {
2438
"nette/di": "to use CompilerExtensions"
@@ -28,16 +42,32 @@
2842
"Contributte\\Http\\": "src"
2943
}
3044
},
45+
"minimum-stability": "dev",
46+
"prefer-stable": true,
3147
"scripts": {
3248
"qa": [
3349
"linter src tests",
3450
"codesniffer src tests"
3551
],
3652
"tester": [
37-
"tester -s -p php --colors 1 -c tests/php-unix.ini tests/cases"
53+
"tester -s -p php --colors 1 -C tests/cases"
54+
],
55+
"coverage": [
56+
"tester -s -p php --colors 1 -C -d extension=xdebug.so --coverage ./coverage.xml --coverage-src ./src tests/cases"
3857
],
39-
"tester-coverage": [
40-
"tester -s -p php --colors 1 -c tests/php-unix.ini -d extension=xdebug.so --coverage ./coverage.xml --coverage-src ./src tests/cases"
58+
"phpstan-install": [
59+
"mkdir -p temp/phpstan",
60+
"composer require -d temp/phpstan phpstan/phpstan:0.10.1",
61+
"composer require -d temp/phpstan phpstan/phpstan-nette:0.10",
62+
"composer require -d temp/phpstan phpstan/phpstan-strict-rules:0.10"
63+
],
64+
"phpstan": [
65+
"temp/phpstan/vendor/bin/phpstan analyse -l max -c phpstan.neon src"
4166
]
67+
},
68+
"extra": {
69+
"branch-alias": {
70+
"dev-master": "0.3.x-dev"
71+
}
4272
}
4373
}

phpstan.neon

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
includes:
2+
- temp/phpstan/vendor/phpstan/phpstan-strict-rules/rules.neon
3+
- temp/phpstan/vendor/phpstan/phpstan-nette/extension.neon
4+
- temp/phpstan/vendor/phpstan/phpstan-nette/rules.neon
5+
6+
parameters:
7+
ignoreErrors:
8+
- '#Parameter \#1 \$ch of function curl_setopt_array expects resource, resource\|false given.#'
9+
- '#Parameter \#1 \$ch of function curl_exec expects resource, resource\|false given.#'
10+
- '#Parameter \#1 \$ch of function curl_getinfo expects resource, resource\|false given.#'
11+
- '#Parameter \#1 \$ch of function curl_close expects resource, resource\|false given.#'
12+
- '#Parameter \#1 \$body of method Contributte\\Http\\Curl\\ResponseFactory::setBody\(\) expects string, bool\|string given.#'

ruleset.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="Contributte">
3+
<!-- Contributte Coding Standard -->
4+
<rule ref="./vendor/ninjify/coding-standard/contributte.xml"/>
5+
6+
<!-- Specific rules -->
7+
<rule ref="SlevomatCodingStandard.Files.TypeNameMatchesFileName">
8+
<properties>
9+
<property name="rootNamespaces" type="array" value="
10+
src=>Contributte\Http
11+
"/>
12+
</properties>
13+
</rule>
14+
15+
<!--Exclude folders -->
16+
<exclude-pattern>/tests/tmp</exclude-pattern>
17+
</ruleset>
18+

src/Auth/BasicAuthenticator.php

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,64 @@
1-
<?php
1+
<?php declare(strict_types = 1);
22

33
namespace Contributte\Http\Auth;
44

55
use Nette\Http\IRequest;
66
use Nette\Http\IResponse;
7+
use Tracy\Debugger;
78

89
class BasicAuthenticator
910
{
1011

1112
/** @var string */
1213
private $title;
1314

14-
/** @var array */
15+
/** @var mixed[] */
1516
private $users = [];
1617

17-
/**
18-
* @param string $title
19-
*/
20-
public function __construct($title)
18+
public function __construct(string $title)
2119
{
2220
$this->title = $title;
2321
}
2422

25-
/**
26-
* @param string $user
27-
* @param string $password
28-
* @return static
29-
*/
30-
public function addUser($user, $password)
23+
public function addUser(string $user, string $password, bool $unsecured): self
3124
{
32-
$this->users[$user] = $password;
33-
25+
$this->users[$user] = [
26+
'password' => $password,
27+
'unsecured' => $unsecured,
28+
];
3429
return $this;
3530
}
3631

37-
/**
38-
* @param IRequest $request
39-
* @param IResponse $response
40-
* @return void
41-
*/
42-
public function authenticate(IRequest $request, IResponse $response)
32+
public function authenticate(IRequest $request, IResponse $response): void
4333
{
4434
$user = $request->getUrl()->getUser();
4535
$password = $request->getUrl()->getPassword();
4636

4737
if (!$this->auth($user, $password)) {
38+
if (class_exists(Debugger::class)) {
39+
Debugger::$productionMode = true;
40+
}
41+
4842
$response->setHeader('WWW-Authenticate', sprintf('Basic realm="%s"', $this->title));
4943
$response->setCode(IResponse::S401_UNAUTHORIZED);
44+
5045
echo '<h1>Authentication failed.</h1>';
51-
die();
46+
die;
5247
}
5348
}
5449

55-
/**
56-
* @param string $user
57-
* @param string $password
58-
* @return bool
59-
*/
60-
protected function auth($user, $password)
50+
protected function auth(string $user, string $password): bool
6151
{
62-
if (!isset($this->users[$user])) return FALSE;
52+
if (!isset($this->users[$user])) return false;
53+
54+
if (
55+
($this->users[$user]['unsecured'] === true && !hash_equals($password, $this->users[$user]['password'])) ||
56+
($this->users[$user]['unsecured'] === false && !password_verify($password, $this->users[$user]['password']))
57+
) {
58+
return false;
59+
}
6360

64-
return $this->users[$user] === $password;
61+
return true;
6562
}
6663

6764
}

0 commit comments

Comments
 (0)