OAuth2Authenticator   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 37
ccs 0
cts 17
cp 0
rs 10
c 1
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A authenticate() 0 15 1
1
<?php
2
3
namespace Kodus\Mail\SMTP\Authenticator;
4
5
use Kodus\Mail\SMTP\SMTPAuthenticator;
6
use Kodus\Mail\SMTP\SMTPClient;
7
8
/**
9
 * TODO finish this untested, partial implementation
10
 *
11
 * @see https://github.com/kodus/mail/issues/1
12
 *
13
 * @see https://tools.ietf.org/html/rfc6750
14
 */
15
class OAuth2Authenticator implements SMTPAuthenticator
16
{
17
    /**
18
     * @var string
19
     */
20
    private $user;
21
22
    /**
23
     * @var string
24
     */
25
    private $token;
26
27
    /**
28
     * @param string $user
29
     * @param string $token
30
     */
31
    public function __construct(string $user, string $token)
32
    {
33
        $this->user = $user;
34
        $this->token = $token;
35
    }
36
37
    public function authenticate(SMTPClient $client): void
38
    {
39
        // NOTE: I don't know if any of this is correct or not - it was ported from somewhere else
40
41
        $auth_str = sprintf("user=%s%sauth=Bearer %s%s%s",
42
            $this->user,
43
            chr(1),
44
            $this->token,
45
            chr(1),
46
            chr(1)
47
        );
48
49
        $auth_str = base64_encode($auth_str);
50
51
        $client->sendCommand("AUTH XOAUTH2 {$auth_str}", "235");
52
    }
53
}
54