-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathoauth2.php
143 lines (130 loc) · 3.95 KB
/
oauth2.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<!--
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Sample code for authenticating to Gmail with OAuth2. See
* https://github.com/google/gmail-oauth2-tools/wiki
* for documentation.
* -->
<html>
<head>
<title>OAuth2 IMAP example with Gmail</title>
</head>
<body>
<?php
require_once 'Zend/Mail/Protocol/Imap.php';
require_once 'Zend/Mail/Storage/Imap.php';
/**
* Builds an OAuth2 authentication string for the given email address and access
* token.
*/
function constructAuthString($email, $accessToken) {
return base64_encode("user=$email\1auth=Bearer $accessToken\1\1");
}
/**
* Given an open IMAP connection, attempts to authenticate with OAuth2.
*
* $imap is an open IMAP connection.
* $email is a Gmail address.
* $accessToken is a valid OAuth 2.0 access token for the given email address.
*
* Returns true on successful authentication, false otherwise.
*/
function oauth2Authenticate($imap, $email, $accessToken) {
$authenticateParams = array('XOAUTH2',
constructAuthString($email, $accessToken));
$imap->sendRequest('AUTHENTICATE', $authenticateParams);
while (true) {
$response = "";
$is_plus = $imap->readLine($response, '+', true);
if ($is_plus) {
error_log("got an extra server challenge: $response");
// Send empty client response.
$imap->sendRequest('');
} else {
if (preg_match('/^NO /i', $response) ||
preg_match('/^BAD /i', $response)) {
error_log("got failure response: $response");
return false;
} else if (preg_match("/^OK /i", $response)) {
return true;
} else {
// Some untagged response, such as CAPABILITY
}
}
}
}
/**
* Given an open and authenticated IMAP connection, displays some basic info
* about the INBOX folder.
*/
function showInbox($imap) {
/**
* Print the INBOX message count and the subject of all messages
* in the INBOX
*/
$storage = new Zend_Mail_Storage_Imap($imap);
include 'header.php';
echo '<h1>Total messages: ' . $storage->countMessages() . "</h1>\n";
/**
* Retrieve first 5 messages. If retrieving more, you'll want
* to directly use Zend_Mail_Protocol_Imap and do a batch retrieval,
* plus retrieve only the headers
*/
echo 'First five messages: <ul>';
for ($i = 1; $i <= $storage->countMessages() && $i <= 5; $i++ ){
echo '<li>' . htmlentities($storage->getMessage($i)->subject) . "</li>\n";
}
echo '</ul>';
}
/**
* Tries to login to IMAP and show inbox stats.
*/
function tryImapLogin($email, $accessToken) {
/**
* Make the IMAP connection and send the auth request
*/
$imap = new Zend_Mail_Protocol_Imap('imap.gmail.com', '993', true);
if (oauth2Authenticate($imap, $email, $accessToken)) {
echo '<h1>Successfully authenticated!</h1>';
showInbox($imap);
} else {
echo '<h1>Failed to login</h1>';
}
}
/**
* Displays a form to collect the email address and access token.
*/
function displayForm($email, $accessToken) {
echo <<<END
<form method="POST" action="oauth2.php">
<h1>Please enter your e-mail address: </h1>
<input type="text" name="email" value="$email"/>
<p>
<h1>Please enter your access token: </h1>
<input type="text" name="access_token" value="$accessToken"/>
<input type="submit"/>
</form>
<hr>
END;
}
$email = $_POST['email'];
$accessToken = $_POST['access_token'];
displayForm($email, $accessToken);
if ($email && $accessToken) {
tryImapLogin($email, $accessToken);
}
?>
</body>
</html>