MagratheaApiAuth
extends MagratheaApiControl
in package
Class for handling API Authentication using JWT.
Extends MagratheaApiControl.
Table of Contents
Properties
- $jwtEncodeType : string
- $tokenExpire : string
- $userInfo : object|null
- $model : string|null
- $service : object|null
Methods
- AdminUserLogin() : array<string|int, mixed>
- Logs in an admin user.
- Cache() : mixed
- Caches the current request's response.
- CacheClear() : mixed
- Clears a specific cache entry.
- CacheClearPattern() : mixed
- Clears cache entries matching a pattern.
- CheckExpire() : bool
- check if token is expired
- Create() : object
- Creates a new item.
- Delete() : bool
- Deletes an item by its ID.
- GetAllHeaders() : array<string, string>
- Gets all HTTP headers from the request.
- getAuthorizationHeader() : mixed
- Gets the Authorization header from various server sources.
- GetAuthorizationToken() : string
- Gets the authorization token from the 'Authorization' header.
- GetHeaders() : string|null
- Gets the authorization header.
- GetPhpInput() : mixed
- Reads and parses the raw input stream (php://input).
- GetPost() : array<string|int, mixed>|null
- Gets data from a POST request.
- GetPut() : array<string|int, mixed>|null
- Gets data from a PUT request.
- GetSecret() : string
- Gets the secret key for JWT encoding/decoding from config.
- getTokenByType() : string|null
- get access token from header
- GetTokenInfo() : object|false
- Get token data
- IsLogged() : bool
- check if user is logged with used token
- jwtDecode() : object
- Decodes a JWT token.
- jwtEncode() : string
- Encodes a payload into a JWT token.
- List() : array<string|int, mixed>
- Lists all items using the associated service.
- Raw() : mixed
- Outputs raw text content and terminates the script.
- Read() : object|array<string|int, mixed>
- Reads a single item by its ID, or lists all items if no ID is provided.
- Refresh() : array<string|int, mixed>
- Refreshes a token.
- ResponseLogin() : array<string|int, mixed>
- Creates a login response for an AdminUser.
- ResponsePayload() : array<string|int, mixed>
- Creates a response with a generic payload.
- ResponseUserPayload() : array<string|int, mixed>
- Creates a response payload for a user.
- Update() : object
- Updates an existing item.
Properties
$jwtEncodeType
public
string
$jwtEncodeType
= "HS256"
The encoding algorithm for JWT.
$tokenExpire
public
string
$tokenExpire
= "7 days"
Default token expiration time.
$userInfo
public
object|null
$userInfo
= null
Holds the decoded JWT payload (user information).
$model
protected
string|null
$model
= null
The class name of the model associated with this control.
$service
protected
object|null
$service
= null
The service object for handling business logic.
Methods
AdminUserLogin()
Logs in an admin user.
public
AdminUserLogin(string $email, string $password) : array<string|int, mixed>
Parameters
- $email : string
-
User email.
- $password : string
-
User password.
Tags
Return values
array<string|int, mixed> —Array with token and user data.
Cache()
Caches the current request's response.
public
Cache(string $name[, string|null $data = null ]) : mixed
Parameters
- $name : string
-
Cache key/name.
- $data : string|null = null
-
Specific cache identifier to be appended to the name.
CacheClear()
Clears a specific cache entry.
public
CacheClear(string $name[, string|null $data = null ]) : mixed
Parameters
- $name : string
-
Cache key/name.
- $data : string|null = null
-
Specific cache identifier.
CacheClearPattern()
Clears cache entries matching a pattern.
public
CacheClearPattern(string $pattern) : mixed
Parameters
- $pattern : string
-
The pattern to match against cache keys.
CheckExpire()
check if token is expired
public
CheckExpire() : bool
Tags
Return values
bool —true if not expired
Create()
Creates a new item.
public
Create([array<string|int, mixed>|false $data = false ]) : object
Parameters
- $data : array<string|int, mixed>|false = false
-
Data for the new item. If false, it uses POST data.
Tags
Return values
object —The created model instance.
Delete()
Deletes an item by its ID.
public
Delete([array<string|int, mixed>|false $params = false ]) : bool
Parameters
- $params : array<string|int, mixed>|false = false
-
Parameters from the request, expecting an "id" key.
Tags
Return values
bool —True on successful deletion.
GetAllHeaders()
Gets all HTTP headers from the request.
public
GetAllHeaders() : array<string, string>
Return values
array<string, string> —An associative array of headers.
getAuthorizationHeader()
Gets the Authorization header from various server sources.
public
getAuthorizationHeader() : mixed
GetAuthorizationToken()
Gets the authorization token from the 'Authorization' header.
public
GetAuthorizationToken() : string
It supports 'Basic' and 'Bearer' token types.
Tags
Return values
string —The token string.
GetHeaders()
Gets the authorization header.
public
GetHeaders() : string|null
Return values
string|null —Authorization header content.
GetPhpInput()
Reads and parses the raw input stream (php://input).
public
GetPhpInput() : mixed
Return values
mixed —Parsed data. JSON is decoded into an associative array. Other content types are parsed as a string.
GetPost()
Gets data from a POST request.
public
GetPost() : array<string|int, mixed>|null
Return values
array<string|int, mixed>|null —The POST data as an array, or null if not a POST request.
GetPut()
Gets data from a PUT request.
public
GetPut() : array<string|int, mixed>|null
Return values
array<string|int, mixed>|null —The PUT data as an array, or null if not a PUT request.
GetSecret()
Gets the secret key for JWT encoding/decoding from config.
public
GetSecret() : string
Return values
string —The JWT secret key.
getTokenByType()
get access token from header
public
getTokenByType(string $type) : string|null
Parameters
- $type : string
-
can be 'Bearer' (for Berarer token) or 'Basic' (for Basic token)
Return values
string|null —token
GetTokenInfo()
Get token data
public
GetTokenInfo([string|false $token = false ]) : object|false
Parameters
- $token : string|false = false
-
The JWT token. If false, it tries to get it from headers.
Return values
object|false —Decoded token data as an object, or false if token is not found.
IsLogged()
check if user is logged with used token
public
IsLogged() : bool
Tags
Return values
bool —is logged?
jwtDecode()
Decodes a JWT token.
public
jwtDecode(string $token) : object
Parameters
- $token : string
-
The JWT token to decode.
Return values
object —The decoded payload as an object.
jwtEncode()
Encodes a payload into a JWT token.
public
jwtEncode(mixed $payload) : string
Parameters
- $payload : mixed
-
The payload to encode.
Return values
string —The generated JWT token.
List()
Lists all items using the associated service.
public
List() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —An array of items.
Raw()
Outputs raw text content and terminates the script.
public
Raw(string $content) : mixed
Parameters
- $content : string
-
The content to output.
Read()
Reads a single item by its ID, or lists all items if no ID is provided.
public
Read([array<string|int, mixed>|false $params = false ]) : object|array<string|int, mixed>
Parameters
- $params : array<string|int, mixed>|false = false
-
Parameters from the request, expecting an "id" key.
Tags
Return values
object|array<string|int, mixed> —A model instance if ID is found, or an array of all items.
Refresh()
Refreshes a token.
public
Refresh() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —New token information.
ResponseLogin()
Creates a login response for an AdminUser.
public
ResponseLogin(AdminUser $user) : array<string|int, mixed>
Parameters
- $user : AdminUser
-
The user object.
Return values
array<string|int, mixed> —Array with token and user data.
ResponsePayload()
Creates a response with a generic payload.
public
ResponsePayload(mixed $payload) : array<string|int, mixed>
Parameters
- $payload : mixed
-
The payload to encode.
Return values
array<string|int, mixed> —Array containing refresh token, token, expiration, and data.
ResponseUserPayload()
Creates a response payload for a user.
public
ResponseUserPayload(array<string|int, mixed> $user) : array<string|int, mixed>
Parameters
- $user : array<string|int, mixed>
-
User data to be encoded in the token.
Tags
Return values
array<string|int, mixed> —Array containing refresh token, token, and user data.
Update()
Updates an existing item.
public
Update(array<string|int, mixed> $params) : object
Parameters
- $params : array<string|int, mixed>
-
Parameters from the request, expecting an "id" and/or "data".
Tags
Return values
object —The updated model instance.