Skip to content

Commit 81ecf54

Browse files
author
Greg Bowler
committed
Load user's data fields
1 parent a714886 commit 81ecf54

File tree

3 files changed

+53
-14
lines changed

3 files changed

+53
-14
lines changed

src/Authenticator.php

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

4+
use Authwave\ProviderUri\AbstractProviderUri;
45
use Authwave\ProviderUri\AdminUri;
56
use Authwave\ProviderUri\AuthUri;
67
use Authwave\ProviderUri\LogoutUri;
@@ -11,6 +12,8 @@
1112
class Authenticator {
1213
const SESSION_KEY = "AUTHWAVE_SESSION";
1314
const RESPONSE_QUERY_PARAMETER = "AUTHWAVE_RESPONSE_DATA";
15+
const LOGIN_TYPE_DEFAULT = "login-default";
16+
const LOGIN_TYPE_ADMIN = "login-admin";
1417

1518
private string $clientKey;
1619
private string $currentUriPath;
@@ -59,7 +62,10 @@ public function isLoggedIn():bool {
5962
return isset($userData);
6063
}
6164

62-
public function login(Token $token = null):void {
65+
public function login(
66+
Token $token = null,
67+
string $loginType = self::LOGIN_TYPE_DEFAULT
68+
):void {
6369
if($this->isLoggedIn()) {
6470
return;
6571
}
@@ -71,17 +77,18 @@ public function login(Token $token = null):void {
7177
$this->sessionData = new SessionData($token);
7278
$this->session->set(self::SESSION_KEY, $this->sessionData);
7379

74-
$this->redirectHandler->redirect($this->getAuthUri($token));
80+
$this->redirectHandler->redirect(
81+
$this->getAuthUri($token, $loginType)
82+
);
7583
}
7684

7785
public function logout():void {
7886
// TODO: Should the logout redirect the user agent to the redirectPath?
7987
$this->session->remove(self::SESSION_KEY);
80-
$this->redirectHandler->redirect($this->getLogoutUri());
8188
}
8289

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

8794
public function getUuid():string {
@@ -94,7 +101,22 @@ public function getEmail():string {
94101
return $userData->getEmail();
95102
}
96103

97-
public function getAuthUri(Token $token):AuthUri {
104+
public function getField(string $name):?string {
105+
$userData = $this->sessionData->getUserData();
106+
return $userData->getField($name);
107+
}
108+
109+
public function getAuthUri(
110+
Token $token,
111+
string $loginType = self::LOGIN_TYPE_DEFAULT
112+
):AbstractProviderUri {
113+
if($loginType === self::LOGIN_TYPE_ADMIN) {
114+
return new AdminUri(
115+
$this->currentUriPath,
116+
$this->authwaveHost
117+
);
118+
}
119+
98120
return new AuthUri(
99121
$token,
100122
$this->currentUriPath,
@@ -111,12 +133,13 @@ public function getAdminUri(
111133
);
112134
}
113135

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);
136+
public function getProfileUri(
137+
string $path = ProfileUri::PATH_PROFILE
138+
):UriInterface {
139+
return new ProfileUri(
140+
$this->authwaveHost,
141+
$path
142+
);
120143
}
121144

122145
private function completeAuth():void {

src/Token.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Authwave;
33

4+
use stdClass;
5+
46
class Token {
57
const ENCRYPTION_METHOD = "aes128";
68

@@ -62,6 +64,10 @@ public function decryptResponseCipher(string $cipher):UserData {
6264
throw new InvalidUserDataSerializationException();
6365
}
6466

65-
return new UserData($data->{"uuid"}, $data->{"email"});
67+
return new UserData(
68+
$data->{"uuid"},
69+
$data->{"email"},
70+
$data->{"fields"} ?? new StdClass()
71+
);
6672
}
6773
}

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
}

0 commit comments

Comments
 (0)