Passed
Push — master ( b01c2b...66819a )
by Raza
11:47
created

Authy::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Srmklive\Authy\Services;
4
5
use Exception;
6
use GuzzleHttp\Client as HttpClient;
7
use Srmklive\Authy\Contracts\Auth\TwoFactor\Authenticatable as TwoFactorAuthenticatable;
8
use Srmklive\Authy\Contracts\Auth\TwoFactor\PhoneToken as SendPhoneTokenContract;
9
use Srmklive\Authy\Contracts\Auth\TwoFactor\Provider as BaseProvider;
10
use Srmklive\Authy\Contracts\Auth\TwoFactor\SMSToken as SendSMSTokenContract;
11
12
class Authy implements BaseProvider, SendSMSTokenContract, SendPhoneTokenContract
13
{
14
    /**
15
     * Array containing configuration data.
16
     *
17
     * @var array
18
     */
19
    private $config;
20
21
    /**
22
     * Authy constructor.
23
     */
24
    public function __construct()
25
    {
26
        if (!empty(config('authy.mode')) && (config('authy.mode') == 'sandbox')) {
27
            $this->config['api_key'] = config('authy.sandbox.key');
28
            $this->config['api_url'] = 'http://sandbox-api.authy.com';
29
        } else {
30
            $this->config['api_key'] = config('authy.live.key');
31
            $this->config['api_url'] = 'https://api.authy.com';
32
        }
33
    }
34
35
    /**
36
     * Determine if the given user has two-factor authentication enabled.
37
     *
38
     * @param \Srmklive\Authy\Contracts\Auth\TwoFactor\Authenticatable $user
39
     *
40
     * @return bool
41
     */
42
    public function isEnabled(TwoFactorAuthenticatable $user)
43
    {
44
        return isset($user->getTwoFactorAuthProviderOptions()['id']);
45
    }
46
47
    /**
48
     * Register the given user with the provider.
49
     *
50
     * @param \Srmklive\Authy\Contracts\Auth\TwoFactor\Authenticatable $user
51
     * @param bool                                                     $sms
52
     *
53
     * @return void
54
     */
55
    public function register(TwoFactorAuthenticatable $user, $sms = false)
56
    {
57
        $response = json_decode((new HttpClient())->post($this->config['api_url'].'/protected/json/users/new?api_key='.$this->config['api_key'], [
58
            'form_params' => [
59
                'user' => [
60
                    'email'        => $user->getEmailForTwoFactorAuth(),
61
                    'cellphone'    => preg_replace('/[^0-9]/', '', $user->getAuthPhoneNumber()),
62
                    'country_code' => $user->getAuthCountryCode(),
63
                ],
64
            ],
65
        ])->getBody(), true);
66
67
        $user->setTwoFactorAuthProviderOptions([
68
            'id'  => $response['user']['id'],
69
            'sms' => $sms,
70
        ]);
71
    }
72
73
    /**
74
     * Send the user two-factor authentication token via SMS.
75
     *
76
     * @param \Srmklive\Authy\Contracts\Auth\TwoFactor\Authenticatable $user
77
     *
78
     * @return void
79
     */
80 View Code Duplication
    public function sendSmsToken(TwoFactorAuthenticatable $user)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82
        try {
83
            $options = $user->getTwoFactorAuthProviderOptions();
84
85
            $response = json_decode((new HttpClient())->get(
86
                $this->config['api_url'].'/protected/json/sms/'.$options['id'].
87
                '?force=true&api_key='.$this->config['api_key']
88
            )->getBody(), true);
89
90
            return $response['success'] === true;
91
        } catch (Exception $e) {
92
            return false;
93
        }
94
    }
95
96
    /**
97
     * Start the user two-factor authentication via phone call.
98
     *
99
     * @param \Srmklive\Authy\Contracts\Auth\TwoFactor\Authenticatable $user
100
     *
101
     * @return void
102
     */
103 View Code Duplication
    public function sendPhoneCallToken(TwoFactorAuthenticatable $user)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
    {
105
        try {
106
            $options = $user->getTwoFactorAuthProviderOptions();
107
108
            $response = json_decode((new HttpClient())->get(
109
                $this->config['api_url'].'/protected/json/call/'.$options['id'].
110
                '?force=true&api_key='.$this->config['api_key']
111
            )->getBody(), true);
112
113
            return $response['success'] === true;
114
        } catch (Exception $e) {
115
            return false;
116
        }
117
    }
118
119
    /**
120
     * Determine if the given token is valid for the given user.
121
     *
122
     * @param \Srmklive\Authy\Contracts\Auth\TwoFactor\Authenticatable $user
123
     * @param string                                                   $token
124
     *
125
     * @return bool
126
     */
127
    public function tokenIsValid(TwoFactorAuthenticatable $user, $token)
128
    {
129
        try {
130
            $options = $user->getTwoFactorAuthProviderOptions();
131
132
            $response = json_decode((new HttpClient())->get(
133
                $this->config['api_url'].'/protected/json/verify/'.
134
                $token.'/'.$options['id'].'?force=true&api_key='.
135
                $this->config['api_key']
136
            )->getBody(), true);
137
138
            return $response['token'] === 'is valid';
139
        } catch (Exception $e) {
140
            return false;
141
        }
142
    }
143
144
    /**
145
     * Delete the given user from the provider.
146
     *
147
     * @param \Srmklive\Authy\Contracts\Auth\TwoFactor\Authenticatable $user
148
     *
149
     * @return bool
150
     */
151
    public function delete(TwoFactorAuthenticatable $user)
152
    {
153
        $options = $user->getTwoFactorAuthProviderOptions();
154
155
        (new HttpClient())->post(
156
            $this->config['api_url'].'/protected/json/users/delete/'.
157
            $options['id'].'?api_key='.$this->config['api_key']
158
        );
159
160
        $user->setTwoFactorAuthProviderOptions([]);
161
    }
162
163
    /**
164
     * Determine if the given user should be sent two-factor authentication token via SMS/phone call.
165
     *
166
     * @param \Srmklive\Authy\Contracts\Auth\TwoFactor\Authenticatable $user
167
     *
168
     * @return bool
169
     */
170
    public function canSendToken(TwoFactorAuthenticatable $user)
171
    {
172
        $sendToken = collect(
173
            $user->getTwoFactorAuthProviderOptions()
174
        )->pluck(['sms', 'phone', 'email'])->filter(function ($value) {
175
            return !empty($value) ? $value : null;
176
        })->isEmpty();
177
178
        return ($this->isEnabled($user) && !$sendToken) ? true : false;
179
    }
180
}
181