Skip to content

Commit d07c0f9

Browse files
authored
docs: Implement guide for custom authenticator integration (#1299)
* docs: add Custom Authenticators section to authenticator list * docs: add practical example for implementing Custom Authenticators * docs: update site menu
1 parent 293665c commit d07c0f9

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Custom Authenticators
2+
3+
CodeIgniter Shield allows you to extend authentication by creating **Custom Authenticators**.
4+
This is done by implementing the `CodeIgniter\Shield\Authentication\AuthenticatorInterface` contract, which ensures full compatibility with Shield’s authentication lifecycle, including `login` and `logout` events.
5+
6+
Custom Authenticators enable project-specific authentication strategies such as:
7+
8+
- External identity providers (OAuth, SAML, OpenID Connect)
9+
- Hardware or device challenges (USB Security Key, FIDO2, TPM, device fingerprinting)
10+
- Hybrid authentication mechanisms
11+
12+
## Why Custom Authenticators
13+
14+
While Shield provides built-in authenticators such as **Session**, **AccessTokens**, **HmacSha256**, and **JWT**, custom authenticators allow you to:
15+
16+
- Enforce project-specific login logic
17+
- Integrate new or external authentication mechanisms
18+
- Keep full compatibility with Shield events and lifecycle
19+
20+
## Implementing a Custom Authenticator
21+
22+
1. Create a PHP class in your `App\Auth\Authentication` namespace.
23+
2. Implement the `CodeIgniter\Shield\Authentication\AuthenticatorInterface`.
24+
3. Implement the required methods:
25+
26+
```php
27+
<?php
28+
29+
declare(strict_types=1);
30+
31+
namespace App\Auth\Authentication;
32+
33+
use CodeIgniter\Shield\Authentication\AuthenticatorInterface;
34+
use CodeIgniter\Shield\Result;
35+
use CodeIgniter\Shield\Entities\User;
36+
37+
class MyCustomAuthenticator implements AuthenticatorInterface
38+
{
39+
public function attempt(array $credentials): Result
40+
{
41+
// Your login logic
42+
}
43+
44+
public function check(array $credentials): Result
45+
{
46+
// Credential verification
47+
}
48+
49+
public function loggedIn(): bool
50+
{
51+
// Return login state
52+
}
53+
54+
public function login(User $user): void
55+
{
56+
// Store user session or token
57+
}
58+
59+
public function loginById($userId): void
60+
{
61+
// Optional: login using user ID
62+
}
63+
64+
public function logout(): void
65+
{
66+
// Remove session or token
67+
}
68+
69+
public function getUser(): ?User
70+
{
71+
// Return the currently logged-in user
72+
}
73+
74+
public function recordActiveDate(): void
75+
{
76+
// Optional: track user activity
77+
}
78+
}
79+
```
80+
81+
## Registering the Authenticator
82+
83+
In CodeIgniter Shield, all authenticators-built-in or custom—are registered through the **app/Config/Auth.php** file. This ensures that Shield recognizes your authenticator and allows you to reference it using its alias in the *auth* helper.
84+
85+
Open **app/Config/Auth.php** and add your custom authenticator to the `$authenticators` array:
86+
87+
```php
88+
public array $authenticators = [
89+
'session' => \CodeIgniter\Shield\Authentication\Session::class,
90+
'tokens' => \CodeIgniter\Shield\Authentication\AccessTokens::class,
91+
'hmac' => \CodeIgniter\Shield\Authentication\HmacSha256::class,
92+
// Register your custom authenticator
93+
'custom' => \App\Auth\Authentication\MyCustomAuthenticator::class,
94+
];
95+
```
96+
The array key `custom` is the alias you will use in the `auth('custom')` helper.
97+
98+
## Using the Authenticator
99+
100+
You can now use your authenticator anywhere in your application via the `auth('custom')` helper:
101+
102+
```php
103+
$credentials = [
104+
'email' => $this->request->getPost('email'),
105+
'password' => $this->request->getPost('password')
106+
];
107+
108+
$result = auth('custom')->attempt($credentials );
109+
110+
if ($result->isOK()) {
111+
$user = $result->extraInfo();
112+
echo "Login successful for: " . $user->email;
113+
} else {
114+
echo "Login failed: " . $result->reason();
115+
}
116+
117+
```
118+
119+
Now all standard authentication methods—such as `attempt()`, `check()`, `loggedIn()`, `login()`, `loginById()`, `logout()`, `getUser()`, and `recordActiveDate()`—are fully available.

docs/getting_started/authenticators.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ Shield provides the following Authenticators:
1717
[HMAC SHA256 Token Authenticator](../references/authentication/hmac.md) for usage.
1818
- **JWT** authenticator provides stateless authentication using JSON Web Token. To use this,
1919
you need additional setup. See [JWT Authentication](../addons/jwt.md).
20+
21+
In addition to the default authenticators listed above, CodeIgniter Shield allows you to build and register Custom Authenticators by implementing the `AuthenticatorInterface`, enabling completely project-specific authentication strategies (external providers, hardware or device challenges, or any other custom logic), while remaining fully compatible with Shield’s authentication lifecycle and its login/logout event system. For full implementation steps, see [Custom Authenticator](../customization/custom_authenticators.md).

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ nav:
115115
- customization/extending_controllers.md
116116
- customization/integrating_custom_view_libs.md
117117
- customization/login_identifier.md
118+
- Custom Authenticators : customization/custom_authenticators.md
118119
- User Management:
119120
- user_management/managing_users.md
120121
- user_management/forcing_password_reset.md

0 commit comments

Comments
 (0)