Skip to content

Commit 4c32d77

Browse files
author
Greg Bowler
committed
Begin OpenSSL development
1 parent fbb5f59 commit 4c32d77

File tree

7 files changed

+83
-32
lines changed

7 files changed

+83
-32
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
"require": {
66
"php": ">=7.4",
7+
"ext-openssl": "*",
78
"phpgt/http": "1.*"
89
},
910
"require-dev": {

src/Authenticator.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
use Psr\Http\Message\UriInterface;
66

77
class Authenticator {
8-
private Cipher $cipher;
8+
private Token $token;
99
private UriInterface $baseUri;
10+
private string $returnPath;
1011

1112
/**
1213
* @param Token $token This must be the same instance of the Token when
@@ -15,17 +16,26 @@ class Authenticator {
1516
* @param string $baseUri The base URI of the application. This is the
1617
* URI authority with optional scheme, as localhost allows http://
1718
*/
18-
public function __construct(Token $token, string $baseUri) {
19-
$this->cipher = $token->generateCipher();
19+
public function __construct(
20+
Token $token,
21+
string $baseUri,
22+
string $returnPath = "/"
23+
) {
24+
$this->token = $token;
2025
$this->baseUri = $this->normaliseBaseUri($baseUri);
26+
$this->returnPath = $returnPath;
2127
}
2228

2329
/**
2430
* The AuthUri is where to redirect the user agent to for authentication
2531
* on the remote Authwave provider.
2632
*/
2733
public function getAuthUri():UriInterface {
28-
return $this->baseUri;
34+
return new AuthUri(
35+
$this->baseUri,
36+
$this->token,
37+
$this->returnPath
38+
);
2939
}
3040

3141
private function normaliseBaseUri(string $baseUri):Uri {

src/Cipher.php

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/InitVector.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
namespace Authwave;
3+
4+
class InitVector {
5+
private string $bytes;
6+
7+
public function __construct(int $length = 8) {
8+
$this->bytes = random_bytes($length);
9+
}
10+
11+
public function __toString():string {
12+
return bin2hex($this->bytes);
13+
}
14+
}

src/Token.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,35 @@
22
namespace Authwave;
33

44
class Token {
5+
const ENCRYPTION_METHOD = "aes128";
6+
57
private string $key;
6-
private string $tokenValue;
8+
private string $secret;
9+
private InitVector $iv;
710

8-
public function __construct(string $key) {
11+
public function __construct(
12+
string $key,
13+
string $secret,
14+
InitVector $iv = null
15+
) {
916
$this->key = $key;
10-
$this->tokenValue = random_bytes(16);
17+
$this->secret = $secret;
18+
$this->iv = $iv ?? new InitVector();
1119
}
1220

13-
public function generateCipher():Cipher {
21+
public function generateCipher():string {
22+
$rawCipher = openssl_encrypt(
23+
$this->secret,
24+
self::ENCRYPTION_METHOD,
25+
$this->key,
26+
0,
27+
$this->iv
28+
);
29+
30+
return base64_encode($rawCipher);
31+
}
1432

33+
public function getIv():InitVector {
34+
return $this->iv;
1535
}
1636
}

test/phpunit/AuthenticatorTest.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,13 @@
22
namespace Authwave\Test;
33

44
use Authwave\Authenticator;
5-
use Authwave\Cipher;
65
use Authwave\InsecureProtocolException;
76
use Authwave\Token;
8-
use PHPUnit\Framework\MockObject\MockObject;
97
use PHPUnit\Framework\TestCase;
108

119
class AuthenticatorTest extends TestCase {
1210
public function testGetAuthUriHostname() {
13-
$cipher = self::createMock(Cipher::class);
1411
$token = self::createMock(Token::class);
15-
$token->method("generateCipher")
16-
->willReturn($cipher);
17-
1812
$sut = new Authenticator($token, "example.com");
1913
$authUri = $sut->getAuthUri();
2014
self::assertStringStartsWith(
@@ -26,11 +20,7 @@ public function testGetAuthUriHostname() {
2620
// All AuthUris MUST be served over HTTPS, with the one exception of localhost.
2721
// But it should still default to HTTPS on localhost.
2822
public function testGetAuthUriHostnameLocalhostHttpsByDefault() {
29-
$cipher = self::createMock(Cipher::class);
3023
$token = self::createMock(Token::class);
31-
$token->method("generateCipher")
32-
->willReturn($cipher);
33-
3424
$sut = new Authenticator($token, "localhost");
3525
$authUri = $sut->getAuthUri();
3626
self::assertStringStartsWith(
@@ -41,11 +31,7 @@ public function testGetAuthUriHostnameLocalhostHttpsByDefault() {
4131

4232
// We should be able to set the scheme to HTTP for localhost hostname only.
4333
public function testGetAuthUriHostnameLocalhostHttpAllowed() {
44-
$cipher = self::createMock(Cipher::class);
4534
$token = self::createMock(Token::class);
46-
$token->method("generateCipher")
47-
->willReturn($cipher);
48-
4935
$sut = new Authenticator($token, "http://localhost");
5036
$authUri = $sut->getAuthUri();
5137
self::assertStringStartsWith(
@@ -56,11 +42,7 @@ public function testGetAuthUriHostnameLocalhostHttpAllowed() {
5642

5743
// We should NOT be able to set the scheme to HTTP for other hostnames.
5844
public function testGetAuthUriHostnameNotLocalhostHttpNotAllowed() {
59-
$cipher = self::createMock(Cipher::class);
6045
$token = self::createMock(Token::class);
61-
$token->method("generateCipher")
62-
->willReturn($cipher);
63-
6446
self::expectException(InsecureProtocolException::class);
6547
new Authenticator($token, "http://localhost.com");
6648
}

test/phpunit/TokenTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
namespace Authwave\Test;
3+
4+
use Authwave\Token;
5+
use PHPUnit\Framework\TestCase;
6+
7+
class TokenTest extends TestCase {
8+
public function testGenerateCipherSameForSameToken() {
9+
$token = new Token(
10+
"test-key",
11+
"test-secret"
12+
);
13+
14+
$cipher1 = $token->generateCipher();
15+
$cipher2 = $token->generateCipher();
16+
17+
self::assertSame($cipher1, $cipher2);
18+
}
19+
20+
public function testGenerateCipherDifferentForDifferentTokenSameDetails() {
21+
$key = "test-key";
22+
$secret = "test-secret";
23+
$token1 = new Token($key, $secret);
24+
$token2 = new Token($key, $secret);
25+
$cipher1 = $token1->generateCipher();
26+
$cipher2 = $token2->generateCipher();
27+
28+
self::assertNotSame($cipher1, $cipher2);
29+
}
30+
}

0 commit comments

Comments
 (0)