Skip to content

Commit d3bea61

Browse files
author
Greg Bowler
committed
Handle user data fields
1 parent eb3745c commit d3bea61

File tree

7 files changed

+60
-56
lines changed

7 files changed

+60
-56
lines changed

src/Authenticator.php

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
<?php
22
namespace Authwave;
33

4+
use Authwave\ProviderUri\AbstractProviderUri;
45
use Authwave\ProviderUri\AdminUri;
56
use Authwave\ProviderUri\AuthUri;
6-
use Authwave\ProviderUri\LogoutUri;
77
use Gt\Http\Uri;
88
use Gt\Session\SessionContainer;
99
use Psr\Http\Message\UriInterface;
1010

1111
class Authenticator {
1212
const SESSION_KEY = "AUTHWAVE_SESSION";
1313
const RESPONSE_QUERY_PARAMETER = "AUTHWAVE_RESPONSE_DATA";
14+
const LOGIN_TYPE_DEFAULT = "login-default";
15+
const LOGIN_TYPE_ADMIN = "login-admin";
1416

1517
private string $clientKey;
1618
private string $currentUriPath;
@@ -59,7 +61,10 @@ public function isLoggedIn():bool {
5961
return isset($userData);
6062
}
6163

62-
public function login(Token $token = null):void {
64+
public function login(
65+
Token $token = null,
66+
string $loginType = self::LOGIN_TYPE_DEFAULT
67+
):void {
6368
if($this->isLoggedIn()) {
6469
return;
6570
}
@@ -71,17 +76,18 @@ public function login(Token $token = null):void {
7176
$this->sessionData = new SessionData($token);
7277
$this->session->set(self::SESSION_KEY, $this->sessionData);
7378

74-
$this->redirectHandler->redirect($this->getAuthUri($token));
79+
$this->redirectHandler->redirect(
80+
$this->getAuthUri($token, $loginType)
81+
);
7582
}
7683

7784
public function logout():void {
7885
// TODO: Should the logout redirect the user agent to the redirectPath?
7986
$this->session->remove(self::SESSION_KEY);
80-
$this->redirectHandler->redirect($this->getLogoutUri());
8187
}
8288

83-
public function adminLogin():void {
84-
// TODO: Implement!
89+
public function adminLogin(Token $token = null):void {
90+
$this->login($token, self::LOGIN_TYPE_ADMIN);
8591
}
8692

8793
public function getUuid():string {
@@ -94,7 +100,22 @@ public function getEmail():string {
94100
return $userData->getEmail();
95101
}
96102

97-
public function getAuthUri(Token $token):AuthUri {
103+
public function getField(string $name):?string {
104+
$userData = $this->sessionData->getUserData();
105+
return $userData->getField($name);
106+
}
107+
108+
public function getAuthUri(
109+
Token $token,
110+
string $loginType = self::LOGIN_TYPE_DEFAULT
111+
):AbstractProviderUri {
112+
if($loginType === self::LOGIN_TYPE_ADMIN) {
113+
return new AdminUri(
114+
$this->currentUriPath,
115+
$this->authwaveHost
116+
);
117+
}
118+
98119
return new AuthUri(
99120
$token,
100121
$this->currentUriPath,
@@ -111,12 +132,13 @@ public function getAdminUri(
111132
);
112133
}
113134

114-
public function getLogoutUri(string $returnToPath = null):UriInterface {
115-
if(is_null($returnToPath)) {
116-
$returnToPath = (new Uri($this->currentUriPath))->getPath();
117-
}
118-
119-
return new LogoutUri($this->authwaveHost, $returnToPath);
135+
public function getProfileUri(
136+
string $path = ProfileUri::PATH_PROFILE
137+
):UriInterface {
138+
return new ProfileUri(
139+
$this->authwaveHost,
140+
$path
141+
);
120142
}
121143

122144
private function completeAuth():void {

src/ProviderUri/LogoutUri.php

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

src/ProviderUri/ProfileUri.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
namespace Authwave\ProviderUri;
3+
4+
class ProfileUri extends AbstractProviderUri {
5+
6+
}

src/Token.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public function decryptResponseCipher(string $cipher):UserData {
6262
throw new InvalidUserDataSerializationException();
6363
}
6464

65-
return new UserData($data->{"uuid"}, $data->{"email"});
65+
return new UserData(
66+
$data->{"uuid"},
67+
$data->{"email"},
68+
$data->{"fields"}
69+
);
6670
}
6771
}

src/UserData.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@
44
class UserData {
55
private string $uuid;
66
private string $email;
7+
private object $fields;
78

8-
public function __construct(string $uuid, string $email) {
9+
public function __construct(
10+
string $uuid,
11+
string $email,
12+
object $fields
13+
) {
914
$this->uuid = $uuid;
1015
$this->email = $email;
16+
$this->fields = $fields;
1117
}
1218

1319
public function getUuid():string {
@@ -17,4 +23,8 @@ public function getUuid():string {
1723
public function getEmail():string {
1824
return $this->email;
1925
}
26+
27+
public function getField(string $name):?string {
28+
return $this->fields->{$name} ?? null;
29+
}
2030
}

test/phpunit/AuthenticatorTest.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -82,29 +82,6 @@ public function testLogoutClearsSession() {
8282
self::assertEmpty($_SESSION);
8383
}
8484

85-
public function testLogoutRedirectsToCurrentPath() {
86-
$_SESSION = [];
87-
$currentPath = "/current/example/path";
88-
89-
$redirectHandler = self::createMock(RedirectHandler::class);
90-
$redirectHandler->expects(self::once())
91-
->method("redirect")
92-
->with(self::callback(fn(UriInterface $uri) =>
93-
$uri->getHost() === AuthUri::DEFAULT_BASE_REMOTE_URI
94-
&& $uri->getPath() === LogoutUri::PATH_LOGOUT
95-
&& $uri->getQuery() === "returnTo=" . urlencode($currentPath)
96-
));
97-
98-
$sut = new Authenticator(
99-
"test-key",
100-
$currentPath,
101-
AuthUri::DEFAULT_BASE_REMOTE_URI,
102-
null,
103-
$redirectHandler
104-
);
105-
$sut->logout();
106-
}
107-
10885
public function testLoginRedirects() {
10986
$_SESSION = [];
11087

test/phpunit/TokenTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ public function testDecryptResponseCipher() {
7979
$serialized = serialize((object)[
8080
"uuid" => $uuid,
8181
"email" => $email,
82+
"fields" => (object)[
83+
"example1" => "value1",
84+
]
8285
]);
8386

8487
$cipher = openssl_encrypt(

0 commit comments

Comments
 (0)