11<?php
22namespace Authwave ;
33
4+ use Gt \Http \Uri ;
45use Gt \Session \SessionContainer ;
56
67class Authenticator {
78 const SESSION_KEY = "AUTHWAVE_SESSION " ;
9+ const RESPONSE_QUERY_PARAMETER = "AUTHWAVE_RESPONSE_DATA " ;
810
911 private string $ clientKey ;
10- private string $ clientSecret ;
1112 private string $ currentUriPath ;
1213 private string $ authwaveHost ;
1314 private SessionContainer $ session ;
@@ -16,7 +17,6 @@ class Authenticator {
1617
1718 public function __construct (
1819 string $ clientKey ,
19- string $ clientSecret ,
2020 string $ currentUriPath ,
2121 string $ authwaveHost = "login.authwave.com " ,
2222 SessionContainer $ session = null ,
@@ -27,20 +27,19 @@ public function __construct(
2727 }
2828
2929 if (!$ session ->contains (self ::SESSION_KEY )) {
30+ // TODO: If there is no Token or UserData in the SessionData, do we even
31+ // need to store it to the current session at all?
3032 $ session ->set (self ::SESSION_KEY , new SessionData ());
3133 }
3234
3335 $ this ->clientKey = $ clientKey ;
34- $ this ->clientSecret = $ clientSecret ;
3536 $ this ->currentUriPath = $ currentUriPath ;
3637 $ this ->authwaveHost = $ authwaveHost ;
3738 $ this ->session = $ session ;
3839 $ this ->sessionData = $ session ->get (self ::SESSION_KEY );
3940 $ this ->redirectHandler = $ redirectHandler ?? new RedirectHandler ();
4041
41- if ($ this ->authInProgress ()) {
42- $ this ->completeAuth ();
43- }
42+ $ this ->completeAuth ();
4443 }
4544
4645 public function isLoggedIn ():bool {
@@ -62,9 +61,12 @@ public function login(Token $token = null):void {
6261 }
6362
6463 if (is_null ($ token )) {
65- $ token = new Token ($ this ->clientKey , $ this -> clientSecret );
64+ $ token = new Token ($ this ->clientKey );
6665 }
6766
67+ $ this ->sessionData = new SessionData ($ token );
68+ $ this ->session ->set (self ::SESSION_KEY , $ this ->sessionData );
69+
6870 $ loginUri = new AuthUri (
6971 $ token ,
7072 $ this ->currentUriPath ,
@@ -88,11 +90,41 @@ public function getEmail():string {
8890 return $ userData ->getEmail ();
8991 }
9092
91- private function authInProgress ():bool {
92- return false ;
93+ private function completeAuth ():void {
94+ $ responseCipher = $ this ->getResponseCipher ();
95+
96+ if (!$ responseCipher ) {
97+ return ;
98+ }
99+
100+ $ token = $ this ->sessionData ->getToken ();
101+ $ userData = $ token ->decryptResponseCipher ($ responseCipher );
102+ $ this ->session ->set (
103+ self ::SESSION_KEY ,
104+ new SessionData ($ token , $ userData )
105+ );
106+
107+ $ this ->redirectHandler ->redirect (
108+ (new Uri ($ this ->currentUriPath ))
109+ ->withoutQueryValue (self ::RESPONSE_QUERY_PARAMETER )
110+ );
93111 }
94112
95- private function completeAuth ():void {
113+ private function getResponseCipher ():?string {
114+ $ queryString = parse_url (
115+ $ this ->currentUriPath ,
116+ PHP_URL_QUERY
117+ );
118+ if (!$ queryString ) {
119+ return null ;
120+ }
121+
122+ $ queryParts = [];
123+ parse_str ($ queryString , $ queryParts );
124+ if (empty ($ queryParts [self ::RESPONSE_QUERY_PARAMETER ])) {
125+ return null ;
126+ }
96127
128+ return $ queryParts [self ::RESPONSE_QUERY_PARAMETER ];
97129 }
98130}
0 commit comments