OAuth2
OAuth2
Properties
clientID
protected string $clientID
clientSecret
protected string $clientSecret
grantType
protected string $grantType
httpClient
protected Client $httpClient
metadata
protected array $metadata
scopes
protected array $scopes
public $
Methods
__construct
public __construct({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$clientID | string | |
$clientSecret | string | |
$grantType | string | |
$config | Map |
setAuthURI
Sets the authorization endpoint URI for OAuth2 authentication.
public setAuthURI({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$uri | string | the URI of the authorization endpoint |
setTokenURI
Sets the URI for the token endpoint.
public setTokenURI({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$uri | string | the URI to be set for the token endpoint |
setRegistrationURI
Sets the registration URI for the OAuth2 metadata.
public setRegistrationURI({Array})
This method updates the 'registration_endpoint' key in the metadata array with the provided URI.
Parameters
Parameter | Type | Description |
---|---|---|
$uri | string | the registration URI to be set |
setIntrospectURI
Sets the URI for the introspection endpoint.
public setIntrospectURI({Array})
This method updates the metadata to include the provided URI for the introspection endpoint, which is used to validate access tokens.
Parameters
Parameter | Type | Description |
---|---|---|
$uri | string | the URI of the introspection endpoint |
setRevokeURI
Sets the URI for the revocation endpoint.
public setRevokeURI({Array})
This method allows you to specify the URI that will be used to revoke OAuth2 tokens.
Parameters
Parameter | Type | Description |
---|---|---|
$uri | string | the URI of the revocation endpoint |
setUserinfoURI
Sets the URI for the user info endpoint in the OAuth2 metadata.
public setUserinfoURI({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$uri | string | the URI of the user info endpoint |
setAuthenticateCallback
Sets the callback function to be used for authentication.
public setAuthenticateCallback({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$cb | \Closure | the callback function to be used for authentication |
discover
Discover OAuth2 metadata from a given URI.
public discover({Array})
This method attempts to retrieve and cache OAuth2 metadata from the specified URI. If the metadata is not already cached, it fetches the metadata from the URI and stores it. If the URI cannot be accessed, an exception is thrown.
Parameters
Parameter | Type | Description |
---|---|---|
$uri | string | the URI to discover OAuth2 metadata from |
addScope
Adds one or more scopes to the OAuth2 authorization.
public addScope({Array})
This method accepts a variable number of arguments. Each argument can be a string representing a single scope or an array of scopes. If an array is provided, the method will recursively add each scope in the array.
hasScope
Checks if the authenticated user has a specific scope.
public hasScope({Array})
This method verifies if the user is authenticated and if the specified scope key exists within the user's scopes.
Parameters
Parameter | Type | Description |
---|---|---|
$key | string | the scope key to check |
scopes
Get the current scopes for this OAuth2 adapter.
public scopes({Array})
authenticated
Check if there is a current user authentication in the session namespace.
public authenticated({Array})
Returns true if we have an access token. False otherwise.
authenticate
Authenticate the user credentials using the OAuth2 "password" grant type.
public authenticate({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$identity | string | the user identity (username) |
$credential | string | the user credential (password) |
$autologin | bool | The autologin flag. If checked the session will be remembered and the refresh token used to obtain |
a new access token when it expires. |
| $skip_auth_check
| bool
| |
refresh
Refreshes the OAuth2 token.
public refresh({Array})
This method attempts to refresh the OAuth2 token using the provided refresh token, identity, and credential. If no token is provided, it will attempt to retrieve a refresh token using the getRefreshToken
method. The method sends a POST request to the token endpoint with the necessary parameters to obtain a new access token.
Parameters
Parameter | Type | Description |
---|---|---|
$token | string | the refresh token to use for refreshing the access token |
$identity | string | the identity (username) to use for authentication |
$credential | string | the credential (password) to use for authentication |
getAccessToken
Retrieves the OAuth2 access token.
public getAccessToken({Array})
This method checks if the 'oauth2_data' key exists in the storage. If it exists, it returns the 'access_token' from the 'oauth2_data'. Otherwise, it returns false.
getRefreshToken
Retrieves the refresh token from the OAuth2 data storage.
public getRefreshToken({Array})
This method checks if the 'oauth2_data' key exists in the storage. If it does, it attempts to retrieve the 'refresh_token' from the 'oauth2_data' array. If the 'refresh_token' is not found, it returns false.
queryAuth
public queryAuth({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$identity | string | |
$extra | array | |
$ | bool |
getToken
Retrieves the OAuth2 token from the storage.
public getToken({Array})
This method returns an associative array containing the 'access_token' from the 'oauth2_data' stored in the class. If the 'access_token' is not present, it returns null.
getTokenType
Retrieves the token type from the OAuth2 data storage.
public getTokenType({Array})
This method accesses the 'oauth2_data' array within the storage and returns the value associated with the 'token_type' key. If the 'token_type' key does not exist, it defaults to returning 'Bearer'.
introspect
Introspects the given token using the OAuth2 introspection endpoint.
public introspect({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$token | string | The token to introspect. If null, the access token from storage will be used. |
$token_type | string | the type of the token, default is 'access_token' |
revoke
Revokes the OAuth2 access token.
public revoke({Array})
This method sends a revocation request to the OAuth2 server's revocation endpoint. It constructs a POST request with the client ID and access token, and sends it using the HTTP client. The response from the server is then checked for a result.
userinfo
Retrieves user information from the OAuth2 user info endpoint.
public userinfo({Array})
This method sends a GET request to the user info endpoint specified in the metadata. If the endpoint is not available or the request fails, it returns false. Otherwise, it returns the response body containing the user information.
authorize
Authorizes the OAuth2 data by validating the required properties and storing the data.
private authorize({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$data | \stdClass | the OAuth2 data object containing token information |
authenticateCredentials
Authenticates user credentials against an OAuth2 token endpoint.
private authenticateCredentials({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$identity | string | The user's identity (e.g., username or email). |
$credential | string | The user's credential (e.g., password). |
$grantType | string | The type of grant being requested. Defaults to 'password'. |
$scope | string | optional scope of the access request |
authenticateCode
Authenticates the user using the OAuth2 authorization code flow.
private authenticateCode({Array})
This method handles the OAuth2 authorization code flow by performing the following steps:
- Checks if an authorization code is present in the request.
- Validates the state parameter to prevent CSRF attacks.
- Sends a request to the token endpoint to exchange the authorization code for an access token.
- Handles the implicit grant type by converting the hash response to a query response.
- Redirects the user to the authorization endpoint if no authorization code or access token is present.
getRedirectUri
Generates the redirect URI based on the current request URI.
private getRedirectUri({Array})
Generated by Hazaar API Doc Generator