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

daycry / auth / 12237641651

09 Dec 2024 02:24PM UTC coverage: 59.304% (-0.04%) from 59.342%
12237641651

push

github

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

Fix: Chain filter

3 of 5 new or added lines in 1 file covered. (60.0%)

1858 of 3133 relevant lines covered (59.3%)

22.2 hits per line

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

61.11
/src/Filters/ChainAuthFilter.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\RedirectResponse;
19
use CodeIgniter\HTTP\RequestInterface;
20
use CodeIgniter\HTTP\Response;
21
use CodeIgniter\HTTP\ResponseInterface;
22
use Daycry\Auth\Config\Auth;
23
use Exception;
24

25
/**
26
 * Chain Authentication Filter.
27
 *
28
 * Checks all authentication systems specified within
29
 * `Config\Auth->authenticationChain`
30
 */
31
class ChainAuthFilter implements FilterInterface
32
{
33
    /**
34
     * Do whatever processing this filter needs to do.
35
     * By default it should not return anything during
36
     * normal execution. However, when an abnormal state
37
     * is found, it should return an instance of
38
     * CodeIgniter\HTTP\Response. If it does, script
39
     * execution will end and that Response will be
40
     * sent back to the client, allowing for error pages,
41
     * redirects, etc.
42
     *
43
     * @param array|null $arguments
44
     *
45
     * @return RedirectResponse|void
46
     */
47
    public function before(RequestInterface $request, $arguments = null)
48
    {
49
        if (! $request instanceof IncomingRequest) {
3✔
50
            return;
×
51
        }
52

53
        helper('settings');
3✔
54

55
        /** @var Auth $config */
56
        $config = config(Auth::class);
3✔
57
        $chain  = $config->authenticationChain;
3✔
58

59
        foreach ($chain as $alias) {
3✔
60
            try {
61
                if (auth($alias)->loggedIn()) {
3✔
62
                    // Make sure Auth uses this Authenticator
63
                    auth()->setAuthenticator($alias);
2✔
64

65
                    return;
3✔
66
                }
NEW
67
            } catch (Exception $e) {
×
NEW
68
                continue;
×
69
            }
70
        }
71

72
        if ($request->getHeaderLine('Accept') === 'application/json' || $request->getHeaderLine('Accept') === 'application/xml') {
1✔
73
            return service('response')->setStatusCode(
×
74
                401,
×
75
                lang('Auth.invalidUser')
×
76
            );
×
77
        }
78

79
        return redirect()->route('login');
1✔
80
    }
81

82
    /**
83
     * We don't have anything to do here.
84
     *
85
     * @param array|null $arguments
86
     */
87
    public function after(RequestInterface $request, ResponseInterface $response, $arguments = null): void
88
    {
89
        // Nothing required
90
    }
2✔
91
}
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