Issues (6)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Api.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Djunehor\Eyowo;
4
5
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Exception\ClientException;
8
use Psr\Http\Message\MessageInterface;
9
10
class Api
11
{
12
13
    private $baseUrl = "https://api.console.eyowo.com/";
14
    private $appKey;
15
    private $appSecret;
16
    public $client;
17
    protected $output;
18
    protected $factors = ['sms'];
19
    protected $providers = ['etisalat', 'glo', 'airtel', 'mtn'];
20
    protected $errorCodes = [
21
        400 => 'Bad Request -- Your request is invalid.',
22
        401 => 'Unauthorized -- Your API key is wrong.',
23
        403 => 'Forbidden -- The kitten requested is hidden for administrators only.',
24
        404 => 'Not Found -- The specified kitten could not be found.',
25
        405 => 'Method Not Allowed -- You tried to access a kitten with an invalid method.',
26
        406 => "Not Acceptable -- You requested a format that isn't json.",
27
        410 => 'Gone -- The kitten requested has been removed from our servers.',
28
        418 => "I'm a teapot.",
29
        429 => "Too Many Requests -- You're requesting too many kittens! Slow down!",
30
        500 => "Internal Server Error -- We had a problem with our server. Try again later.",
31
        503 => "Service Unavailable -- We're temporarily offline for maintenance. Please try again later."
32
    ];
33
34
    protected $errorMessage;
35
    protected $statusCode;
36
37 14
    public function __construct($appKey = null)
38
    {
39 14
        $this->appKey = isset($appKey) ? $appKey : getenv('EYOWO_APP_KEY');
40
41 14
        $this->client = new Client([
42 14
            'base_uri' => $this->baseUrl,
43
            'headers' => [
44 14
                'X-App-Key' => $this->appKey,
45 14
                'Content-Type' => 'application/json',
46
            ]
47
        ]);;
48 14
    }
49
50 2
    public function resolveResponse(MessageInterface $response)
51
    {
52 2
        $this->statusCode = $response->getStatusCode();
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Psr\Http\Message\MessageInterface as the method getStatusCode() does only exist in the following implementations of said interface: GuzzleHttp\Psr7\Response.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
53 2
        $this->errorMessage = array_key_exists($this->statusCode, $this->errorCodes) ? $this->errorCodes[$this->statusCode] : '';
54
55 2
        return $this->output = json_decode($response->getBody()->getContents(), true);
56
    }
57
58 9
    public function handleException(ClientException $exception)
59
    {
60 9
        $this->statusCode = $exception->getCode();
61 9
        $this->errorMessage = $exception->getMessage();
62
63 9
        $output = json_decode($exception->getResponse()->getBody(), true);
64 9
        $this->output['success'] = false;
65 9
        $this->output['message'] = array_key_exists('message', $output) ? $output['message'] : $this->errorMessage;
66
67 9
        return $this->output;
68
69
    }
70
71
    public function getStatusCode()
72
    {
73
        return $this->statusCode;
74
    }
75
76 1
    public function getError()
77
    {
78 1
        return $this->errorMessage;
79
    }
80
81 1 View Code Duplication
    public function validate($phone)
0 ignored issues
show
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...
82
    {
83
        try {
84 1
            $response = $this->client->post('/v1/users/auth/validate', [
85
                'form_params' => [
86 1
                    'mobile' => $phone
87
                ]
88
            ]);
89 1
        } catch (ClientException $e) {
90 1
            return $this->handleException($e);
91
        }
92
93
        return $this->resolveResponse($response);
94
95
    }
96
97 2
    public function initiateAuthorization($phone, $factor = 'sms')
98
    {
99 2
        if (!in_array($factor, $this->factors)) {
100 1
            throw new \Exception("Factor [$factor] not available. Available factors are: " . join(", ", $this->factors));
101
        }
102
103
        try {
104 1
            $response = $this->client->post('/v1/users/auth', [
105
                'form_params' => [
106 1
                    'mobile' => $phone,
107 1
                    'factor' => $factor
108
                ]
109
            ]);
110 1
        } catch (ClientException $e) {
111 1
            return $this->handleException($e);
112
        }
113
114
        return $this->resolveResponse($response);
115
    }
116
117 1 View Code Duplication
    public function generateToken($phone, $passcode, $factor = 'sms')
0 ignored issues
show
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...
118
    {
119 1
        if (!in_array($factor, $this->factors)) {
120
            throw new \Exception("Factor [$factor] not available. Available factors are: " . join(", ", $this->factors));
121
        }
122
123
        try {
124 1
            $response = $this->client->post('/v1/users/auth', [
125
                'form_params' => [
126 1
                    'mobile' => $phone,
127 1
                    'factor' => $factor,
128 1
                    'passcode' => $passcode
129
                ]
130
            ]);
131 1
        } catch (ClientException $e) {
132 1
            return $this->handleException($e);
133
        }
134
135
        return $this->resolveResponse($response);
136
    }
137
138 1 View Code Duplication
    public function refreshToken($refreshToken)
0 ignored issues
show
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...
139
    {
140
141
        try {
142 1
            $response = $this->client->post('/v1//users/accessToken', [
143
                'form_params' => [
144 1
                    'refreshToken' => $refreshToken,
145
                ]
146
            ]);
147 1
        } catch (ClientException $e) {
148 1
            return $this->handleException($e);
149
        }
150
151
        return $this->resolveResponse($response);
152
    }
153
154 1
    public function transferToPhone($walletToken, $amount, $mobile)
155
    {
156
157
        try {
158 1
            $response = $this->client->post('/v1/transfers/phone', [
159 1
                'headers' => ['X-App-Wallet-Access-Token' => $walletToken],
160
                'form_params' => [
161 1
                    'amount' => $amount,
162 1
                    'mobile' => $mobile
163
                ]
164
            ]);
165 1
        } catch (ClientException $e) {
166 1
            return $this->handleException($e);
167
        }
168
169
        return $this->resolveResponse($response);
170
    }
171
172 1
    public function transferToBank($walletToken, $amount, $accountName, $accountNumber, $bankCode)
173
    {
174
175
        try {
176 1
            $response = $this->client->post('/v1/transfers/bank', [
177 1
                'headers' => ['X-App-Wallet-Access-Token' => $walletToken],
178
                'form_params' => [
179 1
                    'amount' => $amount,
180 1
                    'accountName' => $accountName,
181 1
                    'accountNumber' => $accountNumber,
182 1
                    'bankCode' => $bankCode
183
                ]
184
            ]);
185 1
        } catch (ClientException $e) {
186 1
            return $this->handleException($e);
187
        }
188
189
        return $this->resolveResponse($response);
190
    }
191
192 2 View Code Duplication
    public function vtu($walletToken, $mobile, $amount, $provider)
0 ignored issues
show
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...
193
    {
194
195 2
        if (!in_array(strtolower($provider), $this->providers)) {
196 1
            throw new \Exception("Provider [$provider] not supported. Supported providers are: " . join(",", $this->providers));
197
        }
198
199
        try {
200 1
            $response = $this->client->post('/v1/users/payments/bills/vtu', [
201 1
                'headers' => ['X-App-Wallet-Access-Token' => $walletToken],
202
                'form_params' => [
203 1
                    'amount' => $amount,
204 1
                    'mobile' => $mobile,
205 1
                    'provider' => $provider
206
                ]
207
            ]);
208 1
        } catch (ClientException $e) {
209 1
            return $this->handleException($e);
210
        }
211
212
        return $this->resolveResponse($response);
213
    }
214
215 2 View Code Duplication
    public function balance($walletToken)
0 ignored issues
show
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...
216
    {
217
218
        try {
219 2
            $response = $this->client->get('/v1/users/accessToken', [
220 2
                'headers' => ['X-App-Wallet-Access-Token' => $walletToken]
221
            ]);
222
223
            return $this->resolveResponse($response);
224 2
        } catch (ClientException $e) {
225 2
            return $this->handleException($e);
226
        }
227
    }
228
229 2
    public function banks()
230
    {
231
        try {
232 2
            $response = $this->client->get('/v1/queries/banks');
233
        } catch (ClientException $e) {
234
            return $this->handleException($e);
235
        }
236
237 2
        return $this->resolveResponse($response);
238
    }
239
240 1
    public function getBanks()
241
    {
242 1
        return isset($this->output['data']['banks']) ? $this->output['data']['banks'] : [];
243
    }
244
245 1
    public function getWalletBalance()
246
    {
247 1
        return isset($this->output['balance']) ? $this->output['balance'] : 0;
248
    }
249
250 1
    public function getTokenData()
251
    {
252 1
        return isset($this->output['data']) ? $this->output['data'] : [];
253
    }
254
255 1
    public function getAccessToken()
256
    {
257 1
        $tokenData = $this->getTokenData();
258 1
        return isset($tokenData['accessToken']) ? $tokenData['accessToken'] : null;
259
    }
260
261 1
    public function getRefreshToken()
262
    {
263 1
        $tokenData = $this->getTokenData();
264 1
        return isset($tokenData['refreshToken']) ? $tokenData['refreshToken'] : null;
265
    }
266
267
    public function getUser()
268
    {
269
        return isset($this->output['data'], $this->output['data']['user']) ? $this->output['data']['user'] : [];
270
    }
271
272
    public function mobileIsValid()
273
    {
274
        return isset($this->output['success']);
275
    }
276
277
}
278