Completed
Pull Request — dev (#11)
by
unknown
04:51
created

AbstractAuthenticationPlugin   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 6
c 2
b 1
f 0
lcom 1
cbo 3
dl 0
loc 59
ccs 19
cts 19
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A afterHandlerSelected() 0 20 4
getCredentials() 0 1 ?
1
<?php
2
3
namespace Vectorface\SnappyRouter\Plugin\Authentication;
4
5
use Vectorface\SnappyRouter\Authentication\AuthenticatorInterface;
6
use Vectorface\SnappyRouter\Exception\UnauthorizedException;
7
use Vectorface\SnappyRouter\Exception\InternalErrorException;
8
use Vectorface\SnappyRouter\Handler\AbstractHandler;
9
use Vectorface\SnappyRouter\Plugin\AbstractPlugin;
10
11
/**
12
 * An abstract plugin for extracting authentication information and running an authentication callback.
13
 *
14
 * The extraction of authentication information is left up to the subclass.
15
 *
16
 * @copyright Copyright (c) 2014, VectorFace, Inc.
17
 * @author J. Anderson <[email protected]>
18
 * @author Dan Bruce   <[email protected]>
19
 */
20
abstract class AbstractAuthenticationPlugin extends AbstractPlugin
21
{
22
    /**
23
     * The default dependency injection key for the authentication mechanism.
24
     *
25
     * This also serves as the name of the DI key option, for consistency.
26
     */
27
    const DI_KEY_AUTH = 'AuthMechanism';
28
29
    /** The key for fetching the authentication mechanism from dependency
30
        injection. */
31
    protected $authKey = self::DI_KEY_AUTH;
32
33
    /**
34
     * Constructor for the class.
35
     *
36
     * @param array $options An array of options for the plugin.
37
     */
38 2
    public function __construct($options)
39
    {
40 2
        parent::__construct($options);
41
42 2
        if (isset($options[self::DI_KEY_AUTH])) {
43 1
            $this->authKey = $options[self::DI_KEY_AUTH];
44 1
        }
45 2
    }
46
47
    /**
48
     * Invoked directly after the router decides which handler will be used.
49
     * @param AbstractHandler $handler The handler selected by the router.
50
     */
51 2
    public function afterHandlerSelected(AbstractHandler $handler)
52
    {
53 2
        parent::afterHandlerSelected($handler);
54
55 2
        $auth = $this->get($this->authKey);
56 2
        if (!($auth instanceof AuthenticatorInterface)) {
57 2
            throw new InternalErrorException(sprintf(
58 2
                "Implementation of AuthenticationInterface required. Please check your %s configuration.",
59 2
                $this->authKey
60 2
            ));
61
        }
62
63 2
        if (!($credentials = $this->getCredentials())) {
64 2
            throw new UnauthorizedException("Authentication is required to access this resource.");
65
        }
66
67 2
        if (!$auth->authenticate($credentials)) {
68 2
            throw new UnauthorizedException("Authentication is required to access this resource.");
69
        }
70 2
    }
71
72
    /**
73
     * Extract credentials from the request.
74
     *
75
     * @return mixed An array of credentials; A username and password pair, or false if credentials aren't available
76
     */
77
    abstract public function getCredentials();
78
}
79