ThreemaGateway_Model_Tfa   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 76
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A threemagwSetProviderId() 0 4 1
B getTfaAttemptLimits() 0 31 5
1
<?php
2
/**
3
 * Extends XenForo's Tfa Model.
4
 *
5
 * @package ThreemaGateway
6
 * @author rugk
7
 * @copyright Copyright (c) 2016 rugk
8
 * @license MIT
9
 */
10
11
class ThreemaGateway_Model_Tfa extends XFCP_ThreemaGateway_Model_Tfa
12
{
13
    /**
14
     * Array of attempt limits per provider.
15
     *
16
     * Each entry for a provider has this format: [time, max attempts]
17
     *
18
     * @var array PROVIDER_TFA_LIMITS
19
     */
20
    const PROVIDER_TFA_LIMITS = [
21
        'threemagw_reversed' => [
22
            [60 * 5, 150], // in 5 minutes: 150 tries (every 2 seconds)
23
            [60, 30], // in 1 minutes: 30 tries (every 2 seconds)
24
            [12, 6], // in 12 seconds: 6 requests
25
            [8, 4], // in 8 seconds: 4 requests
26
        ],
27
        'threemagw_fast' => [
28
            [60 * 5, 150], // in 5 minutes: 150 tries (every 2 seconds)
29
            [60, 30], // in 1 minutes: 30 tries (every 2 seconds)
30
            [12, 6], // in 12 seconds: 6 requests
31
            [8, 4], // in 8 seconds: 4 requests
32
        ]
33
    ];
34
35
    /**
36
     * @var string
37
     */
38
    protected $providerId;
39
40
    /**
41
     * Setter for provider ID.
42
     *
43
     * @param string $providerId
44
     */
45
    public function threemagwSetProviderId($providerId)
46
    {
47
        $this->providerId = $providerId;
48
    }
49
50
    /**
51
     * Getter for (custom) attempt limits.
52
     *
53
     * @return array
54
     */
55
    public function getTfaAttemptLimits()
56
    {
57
        /** @var array $defaultLimit the default limit by XenForo's implementation */
58
        $defaultLimit = parent::getTfaAttemptLimits();
59
60
        // check whether provider we handle, has a special limit configuration
61
        if (!array_key_exists($this->providerId, self::PROVIDER_TFA_LIMITS)) {
62
            return $defaultLimit;
63
        }
64
65
        /** @var XenForo_Options $xenOptions */
66
        $xenOptions = XenForo_Application::getOptions();
67
68
        // check whether required options are enabled
69
        /** @var bool $autoTriggerEnabled */
70
        $autoTriggerEnabled = false;
71
        switch ($this->providerId) {
72
            case 'threemagw_reversed':
73
                $autoTriggerEnabled = $xenOptions->threema_gateway_tfa_reversed_auto_trigger;
74
                break;
75
            case 'threemagw_fast':
76
                $autoTriggerEnabled = $xenOptions->threema_gateway_tfa_fast_auto_trigger;
77
                break;
78
        }
79
80
        if (!$autoTriggerEnabled) {
81
            return $defaultLimit;
82
        }
83
84
        return self::PROVIDER_TFA_LIMITS[$this->providerId];
85
    }
86
}
87