Completed
Push — master ( a81350...e6a872 )
by Wanderson
05:44
created

OAuth::getToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHPMailer - PHP email creation and transport class.
4
 * PHP Version 5.5.
5
 *
6
 * @see       https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project
7
 *
8
 * @author    Marcus Bointon (Synchro/coolbru) <[email protected]>
9
 * @author    Jim Jagielski (jimjag) <[email protected]>
10
 * @author    Andy Prevost (codeworxtech) <[email protected]>
11
 * @author    Brent R. Matzelle (original founder)
12
 * @copyright 2012 - 2015 Marcus Bointon
13
 * @copyright 2010 - 2012 Jim Jagielski
14
 * @copyright 2004 - 2009 Andy Prevost
15
 * @license   http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
16
 * @note      This program is distributed in the hope that it will be useful - WITHOUT
17
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18
 * FITNESS FOR A PARTICULAR PURPOSE.
19
 */
20
21
namespace PHPMailer\PHPMailer;
22
23
use League\OAuth2\Client\Grant\RefreshToken;
24
use League\OAuth2\Client\Provider\AbstractProvider;
25
use League\OAuth2\Client\Token\AccessToken;
26
27
/**
28
 * OAuth - OAuth2 authentication wrapper class.
29
 * Uses the oauth2-client package from the League of Extraordinary Packages.
30
 *
31
 * @see     http://oauth2-client.thephpleague.com
32
 *
33
 * @author  Marcus Bointon (Synchro/coolbru) <[email protected]>
34
 */
35
class OAuth
36
{
37
    /**
38
     * An instance of the League OAuth Client Provider.
39
     *
40
     * @var AbstractProvider
41
     */
42
    protected $provider = null;
43
44
    /**
45
     * The current OAuth access token.
46
     *
47
     * @var AccessToken
48
     */
49
    protected $oauthToken = null;
50
51
    /**
52
     * The user's email address, usually used as the login ID
53
     * and also the from address when sending email.
54
     *
55
     * @var string
56
     */
57
    protected $oauthUserEmail = '';
58
59
    /**
60
     * The client secret, generated in the app definition of the service you're connecting to.
61
     *
62
     * @var string
63
     */
64
    protected $oauthClientSecret = '';
65
66
    /**
67
     * The client ID, generated in the app definition of the service you're connecting to.
68
     *
69
     * @var string
70
     */
71
    protected $oauthClientId = '';
72
73
    /**
74
     * The refresh token, used to obtain new AccessTokens.
75
     *
76
     * @var string
77
     */
78
    protected $oauthRefreshToken = '';
79
80
    /**
81
     * OAuth constructor.
82
     *
83
     * @param array $options Associative array containing
84
     *                       `provider`, `userName`, `clientSecret`, `clientId` and `refreshToken` elements
85
     */
86
    public function __construct($options)
87
    {
88
        $this->provider = $options['provider'];
89
        $this->oauthUserEmail = $options['userName'];
90
        $this->oauthClientSecret = $options['clientSecret'];
91
        $this->oauthClientId = $options['clientId'];
92
        $this->oauthRefreshToken = $options['refreshToken'];
93
    }
94
95
    /**
96
     * Get a new RefreshToken.
97
     *
98
     * @return RefreshToken
99
     */
100
    protected function getGrant()
101
    {
102
        return new RefreshToken();
103
    }
104
105
    /**
106
     * Get a new AccessToken.
107
     *
108
     * @return AccessToken
109
     */
110
    protected function getToken()
111
    {
112
        return $this->provider->getAccessToken(
113
            $this->getGrant(),
114
            ['refresh_token' => $this->oauthRefreshToken]
115
        );
116
    }
117
118
    /**
119
     * Generate a base64-encoded OAuth token.
120
     *
121
     * @return string
122
     */
123
    public function getOauth64()
124
    {
125
        // Get a new token if it's not available or has expired
126
        if (null === $this->oauthToken or $this->oauthToken->hasExpired()) {
127
            $this->oauthToken = $this->getToken();
128
        }
129
130
        return base64_encode(
131
            'user=' .
132
            $this->oauthUserEmail .
133
            "\001auth=Bearer " .
134
            $this->oauthToken .
135
            "\001\001"
136
        );
137
    }
138
}
139