Skip to content

Commit fc43c0f

Browse files
authored
Merge pull request appwrite#662 from rsneh/master
Added Wordpress OAuth provider
2 parents 5ede00e + 8b57c15 commit fc43c0f

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

app/config/providers.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,5 +224,14 @@
224224
'form' => false,
225225
'beta' => false,
226226
'mock' => true,
227+
],
228+
'wordpress' => [
229+
'name' => 'WordPress',
230+
'developers' => 'https://developer.wordpress.com/docs/oauth2/',
231+
'icon' => 'icon-wordpress',
232+
'enabled' => true,
233+
'form' => false,
234+
'beta' => false,
235+
'mock' => false
227236
]
228237
];

public/images/oauth2/wordpress.png

4.71 KB
Loading
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
namespace Appwrite\Auth\OAuth2;
4+
5+
use Appwrite\Auth\OAuth2;
6+
7+
// Reference Material
8+
// https://developer.wordpress.com/docs/wpcc/
9+
10+
class WordPress extends OAuth2
11+
{
12+
/**
13+
* @var array
14+
*/
15+
protected $user = [];
16+
17+
/**
18+
* @var array
19+
*/
20+
protected $scopes = [
21+
'auth',
22+
];
23+
24+
/**
25+
* @return string
26+
*/
27+
public function getName():string
28+
{
29+
return 'wordpress';
30+
}
31+
32+
/**
33+
* @return string
34+
*/
35+
public function getLoginURL():string
36+
{
37+
return 'https://public-api.wordpress.com/oauth2/authorize?'. \http_build_query([
38+
'client_id' => $this->appID,
39+
'redirect_uri' => $this->callback,
40+
'response_type' => 'code',
41+
'scope' => $this->getScopes(),
42+
'state' => \json_encode($this->state)
43+
]);
44+
}
45+
46+
/**
47+
* @param string $code
48+
*
49+
* @return string
50+
*/
51+
public function getAccessToken(string $code):string
52+
{
53+
$accessToken = $this->request(
54+
'POST',
55+
'https://public-api.wordpress.com/oauth2/token',
56+
[],
57+
\http_build_query([
58+
'client_id' => $this->appID,
59+
'redirect_uri' => $this->callback,
60+
'client_secret' => $this->appSecret,
61+
'grant_type' => 'authorization_code',
62+
'code' => $code
63+
])
64+
);
65+
66+
$accessToken = \json_decode($accessToken, true);
67+
68+
if (isset($accessToken['access_token'])) {
69+
return $accessToken['access_token'];
70+
}
71+
72+
return '';
73+
}
74+
75+
/**
76+
* @param $accessToken
77+
*
78+
* @return string
79+
*/
80+
public function getUserID(string $accessToken):string
81+
{
82+
$user = $this->getUser($accessToken);
83+
84+
if (isset($user['ID'])) {
85+
return $user['ID'];
86+
}
87+
88+
return '';
89+
}
90+
91+
/**
92+
* @param $accessToken
93+
*
94+
* @return string
95+
*/
96+
public function getUserEmail(string $accessToken):string
97+
{
98+
$user = $this->getUser($accessToken);
99+
100+
if (isset($user['email']) && $user['verified']) {
101+
return $user['email'];
102+
}
103+
104+
return '';
105+
}
106+
107+
/**
108+
* @param $accessToken
109+
*
110+
* @return string
111+
*/
112+
public function getUserName(string $accessToken):string
113+
{
114+
$user = $this->getUser($accessToken);
115+
116+
if (isset($user['username'])) {
117+
return $user['username'];
118+
}
119+
120+
return '';
121+
}
122+
123+
/**
124+
* @param string $accessToken
125+
*
126+
* @return array
127+
*/
128+
protected function getUser(string $accessToken)
129+
{
130+
if (empty($this->user)) {
131+
$this->user = \json_decode($this->request('GET', 'https://public-api.wordpress.com/rest/v1/me', ['Authorization: Bearer '.$accessToken]), true);
132+
}
133+
134+
return $this->user;
135+
}
136+
}

0 commit comments

Comments
 (0)