U2fPreAuthenticationEvent::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the U2F Security bundle.
5
 *
6
 * (c) Michael Barbey <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Mbarbey\U2fSecurityBundle\Event\Authentication;
13
14
use Symfony\Component\EventDispatcher\Event;
15
use Mbarbey\U2fSecurityBundle\Model\User\U2fUserInterface;
16
17
/**
18
 * Pre U2F authentication event
19
 *
20
 * An event dispatched right before performing an authentication.
21
 *
22
 * This event is the last chance to cancel an authentication before it will be asked to the user.
23
 *
24
 * This event contain the user who will be asked to authenticate and the appId (protocol + domain name).
25
 * You can call the "abort" function if you want to cancel the authentication request.
26
 *
27
 * @author Michael Barbey <[email protected]>
28
 */
29
class U2fPreAuthenticationEvent extends Event
30
{
31
    private $appId;
32
    private $user;
33
    private $reason;
34
    private $abort = false;
35
36
    /**
37
     * Return the name of the event to use when dispatching this event.
38
     *
39
     * @return string
40
     */
41 1
    public static function getName()
42
    {
43 1
        return 'u2f.authentication.before';
44
    }
45
46
    /**
47
     * @param string $appId             The appId for the authentication
48
     * @param U2fUserInterface $user    The user who will be requested to authenticate with a security key
49
     */
50 5
    public function __construct($appId, U2fUserInterface $user)
51
    {
52 5
        $this->appId = $appId;
53 5
        $this->user = $user;
54 5
    }
55
56
    /**
57
     * Return the appId of the authentication.
58
     * The appId *must* be the protocol + your domain name (ex: https://example.com)
59
     *
60
     * @return string
61
     */
62 1
    public function getAppId()
63
    {
64 1
        return $this->appId;
65
    }
66
67
    /**
68
     * Return the user who will be requested to authenticate with it U2F securtity key
69
     *
70
     * @return U2fUserInterface
71
     */
72 1
    public function getUser()
73
    {
74 1
        return $this->user;
75
    }
76
77
    /**
78
     * Cancel the authentication request. You can attach a reason why it was canceled.
79
     *
80
     * Calling this function stop the propagation of the event.
81
     *
82
     * @param string $reason
83
     */
84 5
    public function abort($reason = null)
85
    {
86 5
        $this->reason = $reason;
87 5
        $this->abort = true;
88 5
        $this->stopPropagation();
89 5
    }
90
91
    /**
92
     * Return if the U2F authentication request must be aborted or not.
93
     *
94
     * @return bool
95
     */
96 1
    public function isAborted()
97
    {
98 1
        return $this->abort;
99
    }
100
101
    /**
102
     * Return the reason why the authentication request was aborted.
103
     *
104
     * @return string|NULL
105
     */
106 1
    public function getReason()
107
    {
108 1
        return $this->reason;
109
    }
110
}
111