Skip to content

Commit

Permalink
adjustments to auth manager
Browse files Browse the repository at this point in the history
  • Loading branch information
donwilson committed Jan 10, 2024
1 parent d4b5b1c commit 66b5961
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 39 deletions.
103 changes: 65 additions & 38 deletions src/Magnetar/Auth/AuthManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ class AuthManager {
*/
protected ?string $user_model=null;

/**
* Status of whether the request has been checked for authentication
* @var bool
*/
protected bool $requestChecked = false;

/**
* Constructor
*/
Expand All @@ -47,27 +53,14 @@ protected function setDefaultUserModel(): void {
$this->user_model = $this->app['config']['auth.model.class'] ?? null;
}

/**
* Get the user model
* @return \Magnetar\Model\Model
*
* @throws \Magnetar\Auth\Exceptions\AuthorizationException
*/
protected function newUserModel(): Model {
if(null === $this->user_model) {
throw new AuthorizationException('Model class for authentication is not specified');
}

return new $this->user_model;
}

/**
* Attempt to authenticate a user. The $credentials array should specify the columns to validate against and their values
* @param array|null $credentials The object to authenticate with. Can be a Request object or an assoc array
* @param bool $remember Whether to remember the user. If true, a cookie will be set
* @param Request|array|null $credentials The object to authenticate with. Can be a Request object or an assoc array
* @return bool
*/
public function attempt(array|null $credentials=null, bool $remember=false): bool {
public function attempt(
Request|array|null $credentials=null
): bool {
if(null === $credentials) {
$credentials = $this->app->request();
}
Expand All @@ -77,59 +70,69 @@ public function attempt(array|null $credentials=null, bool $remember=false): boo
// use cookie to remember user
$cookies = $credentials->cookies();

die(var_dump($cookies));
if(isset($cookies[ $this->rememberCookieName() ])) {

}
} else if(is_array($credentials)) {

}

return false;
}

/**
* Act as a specific user
* @param Model $user The user to act as
* @return void
*
* @throws \Magnetar\Auth\Exceptions\AuthorizationException
*/
public function actAs(Model $user): void {
if(!($user instanceof $this->user_model)) {
throw new AuthorizationException('Invalid user model');
}

$this->user = $user;
}

/**
* Check if a user is authenticated
* @return bool
*/
public function check(): bool {
if(null !== $this->user) {
return true;
}



return false;
return (null !== $this->user);
}

/**
* Get the currently authenticated user
* @return User
* @return User|null
*/
public function user(): User {
// @TODO

return new User();
public function user(): User|null {
return $this->user;
}

/**
* Get the ID of the currently authenticated user. Returns 0 if no user is authenticated
* @return int
* Get the ID (key) of the currently authenticated user. Returns 0 if no user is authenticated
* @return int|string
*/
public function id(): int {
// @TODO

return 0;
public function id(): int|string {
return $this->user?->getKey() ?? 0;
}

/**
* Log the user out
* @return void
*/
public function logout(): void {
// @TODO
$this->invalidateSession();
$this->invalidateRememberCookie();
}

/**
* Remember the user by looking up the 'remember me' cookie
* @return bool
*
* @throws \Magnetar\Auth\Exceptions\AuthorizationException
*/
public function remember(): bool {
if(null !== $this->user) {
Expand All @@ -146,7 +149,7 @@ public function remember(): bool {
$this->app['config']['app.key'],
null,//$this->app['config']['app.digest'],
$this->app['config']['app.cipher']
))::decrypt($raw_cookie);
))->decrypt($raw_cookie);

// validate cookie
if(!isset($cookie['id']) || !isset($cookie['token'])) {
Expand All @@ -170,6 +173,16 @@ public function remember(): bool {
return (null !== $this->user);
}

/**
* Invalidate the current session
* @return void
*/
protected function invalidateSession(): void {
if(null !== $this->user) {
$this->user = null;
}
}

/**
* Invalidate the existing 'remember me' cookie
* @return void
Expand Down Expand Up @@ -197,4 +210,18 @@ protected function getRememberCookie(): array|null {
protected function rememberCookieName(): string {
return Str::snake_case($this->app['config']['app.name'] ?? 'magnetar') .'_auth'. ((null !== $this->user_model)?'_'. substr(md5($this->user_model), 0, 10):'');
}

/**
* Get the user model
* @return \Magnetar\Model\Model
*
* @throws \Magnetar\Auth\Exceptions\AuthorizationException
*/
protected function newUserModel(): Model {
if(null === $this->user_model) {
throw new AuthorizationException('Model class for authentication is not specified');
}

return new $this->user_model;
}
}
2 changes: 1 addition & 1 deletion src/Magnetar/Auth/Middleware/AuthenticateMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function handle(Request $request, Closure $next): Response {
* @throws \Magnetar\Auth\Exceptions\AuthorizationException
*/
protected function authenticate(Request $request): void {
if(app('auth')->remember()) {
if(app('auth')->attempt($request)) {
return;
}

Expand Down

0 comments on commit 66b5961

Please sign in to comment.