Skip to content
This repository was archived by the owner on May 31, 2022. It is now read-only.

Commit 5dbe6de

Browse files
committed
Initial commit
0 parents  commit 5dbe6de

36 files changed

+1024
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.zip

LICENSE

Lines changed: 459 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# OpenAuth.dev Provider for XenForo 2
2+
3+
<div align=center>
4+
5+
![openauth-icon](https://user-images.githubusercontent.com/81188/87192541-f9fbe600-c2f6-11ea-9c8c-aebe7813d154.png)
6+
7+
8+
### OpenAuth.dev Provider for XenForo 2
9+
10+
</div>
11+
12+
---
13+
14+
### Table of contents
15+
16+
* [About the project](#about-the-project)
17+
* [Getting Started](#getting-started)
18+
* [Configuration](#configuration)
19+
* [Contributing](#contributing)
20+
* [Versioning](#versioning)
21+
* [Authors](#authors)
22+
* [License](#license)
23+
24+
## About the project
25+
26+
WIP
27+
28+
## Prerequisites
29+
30+
You need:
31+
32+
- A XenForo installation (2.0.4 or newer)
33+
- PHP (5.4 or newer)
34+
- A free user account on [OpenAuth.dev](https://www.openauth.dev), which has been authorized as a developer
35+
36+
## Getting started
37+
38+
Download the latest release from the [releases section](https://github.com/openauth-dev/XF-OA-OpenAuth/releases) and upload it in your XenForo installation.
39+
40+
That's it!
41+
42+
## Configuration
43+
44+
Common to all vendors is that you have to create an "application" for the respective vendor, and get an ID and secret key, which must be entered into the settings (Administration > Setup > Connected Accounts) of your community.
45+
46+
To obtain a key pair from OpenAuth.dev, you need to [create an application](https://www.openauth.dev/developer/app-create/) first. After successful creation, find your newly created application in the list of [your applications](https://www.openauth.dev/developer/my-apps/) and click the "Edit" button. At the bottom of that page, you'll find the Client ID and the corresponding Client Secret. Copy both and paste them into the provider settings.
47+
48+
Under normal circumstances, you should now be able to register/log in using OpenAuth.dev.
49+
50+
## Contributing
51+
52+
There are many ways to help this open source project. Write tutorials, improve documentation, share bugs with others, make feature requests, or just write code. We look forward to every contribution.
53+
54+
## Versioning
55+
56+
We use [SemVer](http://semver.org/) for versioning. For available versions, see the [tags on this repository](https://github.com/openauth-dev/XF-OA-OpenAuth/tags).
57+
58+
## Authors
59+
60+
* **Sascha Greuel** - *Main development* - [SoftCreatR](https://github.com/SoftCreatR)
61+
62+
See also the list of [contributors](https://github.com/openauth-dev/XF-OA-OpenAuth/graphs/contributors) who participated in this project.
63+
64+
## License
65+
66+
This project is licensed under the LGPL-2.1 License - see the [LICENSE](LICENSE) file for details.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/*
3+
* Copyright by The OpenAuth.dev Team.
4+
* This file is part of XF/OA/OpenAuth.
5+
*
6+
* License: GNU Lesser General Public License v2.1
7+
*
8+
* THIS LIBRARY IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR
9+
* MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC
10+
* LICENSE AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION; EITHER
11+
* VERSION 2.1 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
12+
*
13+
* THIS LIBRARY IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
14+
* BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
15+
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
16+
* LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
17+
*
18+
* YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC
19+
* LICENSE ALONG WITH THIS LIBRARY; IF NOT, WRITE TO THE FREE SOFTWARE
20+
* FOUNDATION, INC., 51 FRANKLIN STREET, FIFTH FLOOR, BOSTON, MA 02110-1301 USA
21+
*
22+
* The above copyright notice and this disclaimer notice shall be included in all
23+
* copies or substantial portions of the Software.
24+
*/
25+
26+
namespace OA\OpenAuth\ConnectedAccount\Provider;
27+
28+
use XF\ConnectedAccount\Provider\AbstractProvider;
29+
use XF\Entity\ConnectedAccountProvider;
30+
31+
class OpenAuth extends AbstractProvider
32+
{
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function getOAuthServiceName()
37+
{
38+
return 'OA\OpenAuth:Service\OpenAuth';
39+
}
40+
41+
/**
42+
* @return string
43+
*/
44+
public function getProviderDataClass()
45+
{
46+
return 'OA\OpenAuth:ProviderData\OpenAuth';
47+
}
48+
49+
/**
50+
* @return string[]
51+
*/
52+
public function getDefaultOptions()
53+
{
54+
return [
55+
'client_id' => '',
56+
'client_secret' => ''
57+
];
58+
}
59+
60+
/**
61+
* @param ConnectedAccountProvider $provider
62+
* @param null $redirectUri
63+
* @return array
64+
*/
65+
public function getOAuthConfig(ConnectedAccountProvider $provider, $redirectUri = null)
66+
{
67+
return [
68+
'key' => $provider->options['client_id'],
69+
'secret' => $provider->options['client_secret'],
70+
'scopes' => [
71+
\OA\OpenAuth\ConnectedAccount\Service\OpenAuth::SCOPE_OPENID,
72+
\OA\OpenAuth\ConnectedAccount\Service\OpenAuth::SCOPE_NICKNAME,
73+
\OA\OpenAuth\ConnectedAccount\Service\OpenAuth::SCOPE_PROFILE,
74+
\OA\OpenAuth\ConnectedAccount\Service\OpenAuth::SCOPE_EMAIL,
75+
\OA\OpenAuth\ConnectedAccount\Service\OpenAuth::SCOPE_EMAIL_VERIFIED,
76+
\OA\OpenAuth\ConnectedAccount\Service\OpenAuth::SCOPE_PICTURE
77+
],
78+
'redirect_uri' => $redirectUri ?: $this->getRedirectUri($provider)
79+
];
80+
}
81+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/*
3+
* Copyright by The OpenAuth.dev Team.
4+
* This file is part of XF/OA/OpenAuth.
5+
*
6+
* License: GNU Lesser General Public License v2.1
7+
*
8+
* THIS LIBRARY IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR
9+
* MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC
10+
* LICENSE AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION; EITHER
11+
* VERSION 2.1 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
12+
*
13+
* THIS LIBRARY IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
14+
* BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
15+
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
16+
* LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
17+
*
18+
* YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC
19+
* LICENSE ALONG WITH THIS LIBRARY; IF NOT, WRITE TO THE FREE SOFTWARE
20+
* FOUNDATION, INC., 51 FRANKLIN STREET, FIFTH FLOOR, BOSTON, MA 02110-1301 USA
21+
*
22+
* The above copyright notice and this disclaimer notice shall be included in all
23+
* copies or substantial portions of the Software.
24+
*/
25+
26+
namespace OA\OpenAuth\ConnectedAccount\ProviderData;
27+
28+
use XF\ConnectedAccount\ProviderData\AbstractProviderData;
29+
30+
class OpenAuth extends AbstractProviderData
31+
{
32+
public function getDefaultEndpoint()
33+
{
34+
return '/me';
35+
}
36+
37+
public function getProviderKey()
38+
{
39+
return $this->requestFromEndpoint('sub');
40+
}
41+
42+
public function getUsername()
43+
{
44+
return $this->requestFromEndpoint('nickname');
45+
}
46+
47+
public function getEmail()
48+
{
49+
return $this->requestFromEndpoint('email_verified') ?: $this->requestFromEndpoint('email');
50+
}
51+
52+
public function getAvatarUrl()
53+
{
54+
return $this->requestFromEndpoint('picture');
55+
}
56+
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
<?php
2+
/*
3+
* Copyright by The OpenAuth.dev Team.
4+
* This file is part of XF/OA/OpenAuth.
5+
*
6+
* License: GNU Lesser General Public License v2.1
7+
*
8+
* THIS LIBRARY IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR
9+
* MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC
10+
* LICENSE AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION; EITHER
11+
* VERSION 2.1 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
12+
*
13+
* THIS LIBRARY IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
14+
* BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
15+
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
16+
* LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
17+
*
18+
* YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC
19+
* LICENSE ALONG WITH THIS LIBRARY; IF NOT, WRITE TO THE FREE SOFTWARE
20+
* FOUNDATION, INC., 51 FRANKLIN STREET, FIFTH FLOOR, BOSTON, MA 02110-1301 USA
21+
*
22+
* The above copyright notice and this disclaimer notice shall be included in all
23+
* copies or substantial portions of the Software.
24+
*/
25+
26+
namespace OA\OpenAuth\ConnectedAccount\Service;
27+
28+
use OAuth\Common\Consumer\CredentialsInterface;
29+
use OAuth\Common\Http\Client\ClientInterface;
30+
use OAuth\Common\Http\Exception\TokenResponseException;
31+
use OAuth\Common\Http\Uri\Uri;
32+
use OAuth\Common\Http\Uri\UriInterface;
33+
use OAuth\Common\Storage\TokenStorageInterface;
34+
use OAuth\OAuth2\Service\AbstractService;
35+
use OAuth\OAuth2\Token\StdOAuth2Token;
36+
37+
class OpenAuth extends AbstractService
38+
{
39+
/**
40+
* @string
41+
*/
42+
const SCOPE_OPENID = 'openid';
43+
44+
/**
45+
* @string
46+
*/
47+
const SCOPE_NICKNAME = 'nickname';
48+
49+
/**
50+
* @string
51+
*/
52+
const SCOPE_PROFILE = 'profile';
53+
54+
/**
55+
* @string
56+
*/
57+
const SCOPE_EMAIL = 'email';
58+
59+
/**
60+
* @string
61+
*/
62+
const SCOPE_EMAIL_VERIFIED = 'email_verified';
63+
64+
/**
65+
* @string
66+
*/
67+
const SCOPE_PICTURE = 'picture';
68+
69+
/**
70+
* {@inheritdoc}
71+
*/
72+
public function __construct(
73+
CredentialsInterface $credentials,
74+
ClientInterface $httpClient,
75+
TokenStorageInterface $storage,
76+
$scopes = [],
77+
UriInterface $baseApiUri = null
78+
) {
79+
parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true);
80+
81+
if (null === $baseApiUri) {
82+
$this->baseApiUri = new Uri('https://www.openauth.dev');
83+
}
84+
}
85+
86+
/**
87+
* {@inheritdoc}
88+
*/
89+
protected function getAuthorizationMethod()
90+
{
91+
return static::AUTHORIZATION_METHOD_HEADER_BEARER;
92+
}
93+
94+
/**
95+
* {@inheritdoc}
96+
*/
97+
protected function parseAccessTokenResponse($responseBody)
98+
{
99+
$data = json_decode($responseBody, true);
100+
101+
if (null === $data || !is_array($data)) {
102+
throw new TokenResponseException('Unable to parse response.');
103+
}
104+
105+
if (isset($data['error'])) {
106+
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
107+
}
108+
109+
$token = new StdOAuth2Token();
110+
111+
$token->setAccessToken($data['access_token']);
112+
unset($data['access_token']);
113+
114+
if (isset($data['expires_in'])) {
115+
$token->setLifeTime($data['expires_in']);
116+
unset($data['expires_in']);
117+
}
118+
119+
if (isset($data['refresh_token'])) {
120+
$token->setRefreshToken($data['refresh_token']);
121+
unset($data['refresh_token']);
122+
}
123+
124+
$token->setExtraParams($data);
125+
126+
return $token;
127+
}
128+
129+
/**
130+
* {@inheritdoc}
131+
*/
132+
public function getAuthorizationEndpoint()
133+
{
134+
return new Uri($this->baseApiUri . '/oauth2-authorize');
135+
}
136+
137+
/**
138+
* {@inheritdoc}
139+
*/
140+
public function getAccessTokenEndpoint()
141+
{
142+
return new Uri($this->baseApiUri . '/oauth2-token');
143+
}
144+
145+
/**
146+
* {@inheritdoc}
147+
*/
148+
public function getAuthorizationUri(array $additionalParameters = [])
149+
{
150+
$parameters = array_merge(
151+
$additionalParameters,
152+
[
153+
'client_id' => $this->credentials->getConsumerId(),
154+
'redirect_uri' => $this->credentials->getCallbackUrl(),
155+
'response_type' => 'code',
156+
]
157+
);
158+
159+
if ($this->needsStateParameterInAuthUrl()) {
160+
if (!isset($parameters['state'])) {
161+
$parameters['state'] = $this->generateAuthorizationState();
162+
}
163+
164+
$this->storeAuthorizationState($parameters['state']);
165+
}
166+
167+
$parameters['scope'] = implode(' ', $this->scopes);
168+
169+
// Build the url
170+
$url = clone $this->getAuthorizationEndpoint();
171+
172+
foreach ($parameters as $key => $val) {
173+
$url->addToQuery($key, $val);
174+
}
175+
176+
return $url;
177+
}
178+
}

0 commit comments

Comments
 (0)