• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

daycry / auth / 22540946991

01 Mar 2026 09:55AM UTC coverage: 63.267%. Remained the same
22540946991

push

github

web-flow
Merge pull request #37 from daycry/development

Refactor Auth configuration and update CHANGELOG for v4.0.0

32 of 42 new or added lines in 18 files covered. (76.19%)

1 existing line in 1 file now uncovered.

3064 of 4843 relevant lines covered (63.27%)

41.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.97
/src/Filters/AuthFilter.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of Daycry Auth.
7
 *
8
 * (c) Daycry <daycry9@proton.me>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace Daycry\Auth\Filters;
15

16
use CodeIgniter\Filters\FilterInterface;
17
use CodeIgniter\HTTP\IncomingRequest;
18
use CodeIgniter\HTTP\RequestInterface;
19
use CodeIgniter\HTTP\ResponseInterface;
20
use Config\Services;
21
use Daycry\Auth\Authentication\Authenticators\Session;
22
use Daycry\Auth\Config\Auth;
23
use Daycry\Auth\Interfaces\AuthenticatorInterface;
24
use Daycry\Auth\Result;
25

26
/**
27
 * Authentication Filter.
28
 *
29
 * JSON Web Token authentication for web applications
30
 * Access Token authentication for web applications
31
 */
32
class AuthFilter implements FilterInterface
33
{
34
    public function before(RequestInterface $request, $arguments = null)
9✔
35
    {
36
        helper('checkEndpoint');
9✔
37

38
        if (! $request instanceof IncomingRequest) {
9✔
39
            return;
×
40
        }
41

42
        $endpoint = checkEndpoint();
9✔
43

44
        $alias = $this->determineAuthenticator($arguments, $endpoint);
9✔
45

46
        /** @var AuthenticatorInterface $authenticator */
47
        $authenticator = auth($alias)->getAuthenticator();
9✔
48

49
        /** @var Auth $config */
50
        $config = config(Auth::class);
9✔
51

52
        if ($authenticator instanceof Session) {
9✔
53
            return $this->handleSessionAuthentication($authenticator, $config);
3✔
54
        }
55

56
        return $this->handleTokenAuthentication($authenticator);
6✔
57
    }
58

59
    /**
60
     * Determine which authenticator to use
61
     *
62
     * @param mixed $endpoint
63
     */
64
    private function determineAuthenticator(?array $arguments, $endpoint): string
9✔
65
    {
66
        $alias = $arguments ? $arguments[0] : service('settings')->get('Auth.defaultAuthenticator');
9✔
67

68
        return ($endpoint && $endpoint->auth) ? $endpoint->auth : $alias;
9✔
69
    }
70

71
    /**
72
     * Handle session-based authentication
73
     */
74
    private function handleSessionAuthentication(Session $authenticator, Auth $config)
3✔
75
    {
76
        if (auth()->loggedIn()) {
3✔
77
            if (setting('AuthSecurity.recordActiveDate')) {
2✔
78
                $authenticator->recordActiveDate();
2✔
79
            }
80

81
            $user = $authenticator->getUser();
2✔
82

83
            // Check if user is banned
84
            if ($user->isBanned()) {
2✔
85
                $error = $user->getBanMessage() ?? lang('Auth.logOutBannedUser');
1✔
86
                $authenticator->logout();
1✔
87

88
                return redirect()->to($config->logoutRedirect())
1✔
89
                    ->with('error', $error);
1✔
90
            }
91

92
            // Check if user needs activation
93
            if ($user !== null && ! $user->isActivated()) {
1✔
94
                $hasAction = $authenticator->startUpAction('register', $user);
×
95
                if ($hasAction) {
×
96
                    return redirect()->route('auth-action-show')
×
97
                        ->with('error', lang('Auth.activationBlocked'));
×
98
                }
99
            }
100

101
            return;
1✔
102
        }
103

104
        // Handle pending actions
105
        if ($authenticator->isPending()) {
1✔
106
            return redirect()->route('auth-action-show')
×
107
                ->with('error', $authenticator->getPendingMessage());
×
108
        }
109

110
        // Save current URL for redirect after login
111
        if (uri_string() !== route_to('login')) {
1✔
112
            session()->setTempdata('beforeLoginUrl', current_url(), 300);
1✔
113
        }
114

115
        return redirect()->route('login');
1✔
116
    }
117

118
    /**
119
     * Handle token-based authentication
120
     */
121
    private function handleTokenAuthentication(AuthenticatorInterface $authenticator)
6✔
122
    {
123
        $result = $authenticator->attempt();
6✔
124

125
        if (! $result->isOK()) {
6✔
126
            return service('response')
4✔
127
                ->setStatusCode(ResponseInterface::HTTP_UNAUTHORIZED)
4✔
128
                ->setJson(['message' => $result->reason()]);
4✔
129
        }
130

131
        if (setting('AuthSecurity.recordActiveDate')) {
2✔
132
            $authenticator->recordActiveDate();
2✔
133
        }
134

135
        // Handle additional access token validation if enabled
136
        if (service('settings')->get('AuthSecurity.accessTokenEnabled')) {
2✔
137
            return $this->validateAccessToken();
×
138
        }
139
    }
140

141
    /**
142
     * Validate access token when enabled
143
     */
144
    private function validateAccessToken()
×
145
    {
146
        $accessToken = (Services::auth(false))->setAuthenticator('access_token')->attempt();
×
147

NEW
148
        if (! $accessToken->isOK() && service('settings')->get('AuthSecurity.strictApiAndAuth')) {
×
149
            return service('response')
×
150
                ->setStatusCode(ResponseInterface::HTTP_UNAUTHORIZED)
×
151
                ->setJson([
×
152
                    'message' => ($accessToken instanceof Result)
×
153
                        ? $accessToken->reason()
×
154
                        : lang('Auth.badToken'),
×
155
                ]);
×
156
        }
157
    }
158

159
    /**
160
     * We don't have anything to do here.
161
     *
162
     * @param array|null $arguments
163
     */
164
    public function after(RequestInterface $request, ResponseInterface $response, $arguments = null): void
3✔
165
    {
166
        // Nothing required
167
    }
3✔
168
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc