1<?php
2/**
3 * Copyright 2013-2017 Horde LLC (http://www.horde.org/)
4 *
5 * See the enclosed file COPYING for license information (LGPL). If you
6 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
7 *
8 * @category  Horde
9 * @copyright 2013-2017 Horde LLC
10 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
11 * @package   Smtp
12 */
13
14/**
15 * Generates an OAuth 2.0 authentication token as used in the Gmail XOAUTH2
16 * authentication mechanism.
17 *
18 * See: https://developers.google.com/gmail/xoauth2_protocol
19 *
20 * @author    Michael Slusarz <slusarz@horde.org>
21 * @category  Horde
22 * @copyright 2013-2017 Horde LLC
23 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
24 * @package   Smtp
25 * @since     1.3.0
26 */
27class Horde_Smtp_Password_Xoauth2 implements Horde_Smtp_Password
28{
29    /**
30     * Access token.
31     *
32     * @var string
33     */
34    public $access_token;
35
36    /**
37     * Username.
38     *
39     * @var string
40     */
41    public $username;
42
43    /**
44     * Constructor.
45     *
46     * @param string $username      The username.
47     * @param string $access_token  The access token.
48     */
49    public function __construct($username, $access_token)
50    {
51        $this->username = $username;
52        $this->access_token = $access_token;
53    }
54
55    /**
56     * Return the password to use for the server connection.
57     *
58     * @return string  The password.
59     */
60    public function getPassword()
61    {
62        // base64("user=" {User} "^Aauth=Bearer " {Access Token} "^A^A")
63        // ^A represents a Control+A (\001)
64        return base64_encode(
65            'user=' . $this->username . "\1" .
66            'auth=Bearer ' . $this->access_token . "\1\1"
67        );
68    }
69
70}
71