Skip to content

Commit

Permalink
Initial code commit
Browse files Browse the repository at this point in the history
Added README.md
  • Loading branch information
CrazyHackGUT committed Dec 5, 2021
0 parents commit 98b1654
Show file tree
Hide file tree
Showing 20 changed files with 400 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# XenForo directories
_data
_releases
46 changes: 46 additions & 0 deletions ConnectedAccount/Provider/HLMod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);

/**
* This file is a part of [HLModerators] Connected Account Add-On for XenForo v2.2.x.
* All rights reserved.
*
* Developed by HLModerators.
*/

namespace HLModerators\Auth\ConnectedAccount\Provider;


use XF\ConnectedAccount\Provider\AbstractProvider;
use XF\Entity\ConnectedAccountProvider;

class HLMod extends AbstractProvider
{
public function getOAuthServiceName(): string
{
return 'HLModerators\Auth:Service\HLMod';
}

public function getProviderDataClass(): string
{
return 'HLModerators\Auth:ProviderData\HLMod';
}

public function getDefaultOptions(): array
{
return [
'client_id' => '',
'client_secret' => ''
];
}

public function getOAuthConfig(ConnectedAccountProvider $provider, $redirectUri = null): array
{
return [
'key' => $provider->options['client_id'],
'secret' => $provider->options['client_secret'],
'scopes' => ['user:read'],
'redirect' => $redirectUri ?: $this->getRedirectUri($provider)
];
}
}
54 changes: 54 additions & 0 deletions ConnectedAccount/ProviderData/HLMod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);

/**
* This file is a part of [HLModerators] Connected Account Add-On for XenForo v2.2.x.
* All rights reserved.
*
* Developed by HLModerators.
*/

namespace HLModerators\Auth\ConnectedAccount\ProviderData;


use XF\ConnectedAccount\ProviderData\AbstractProviderData;

class HLMod extends AbstractProviderData
{
public function getDefaultEndpoint()
{
return 'me';
}

public function getProviderKey(): int
{
return $this->requestFromEndpoint('user_id');
}

public function getUsername(): string
{
return $this->requestFromEndpoint('username');
}

public function getProfileLink(): string
{
return $this->requestFromEndpoint('view_url');
}

public function getAvatarUrl(): string
{
return $this->requestFromEndpoint('avatar_urls')['o'];
}

public function requestFromEndpoint($key = null, $method = 'GET', $endpoint = null)
{
$endpoint = $endpoint ?: $this->getDefaultEndpoint();
$isDefaultEndpoint = ($endpoint == $this->getDefaultEndpoint());

$cacheKey = $isDefaultEndpoint ? 'me' : $key;
$response = parent::requestFromEndpoint($cacheKey, $method, $endpoint);

return $isDefaultEndpoint && $key !== null ?
$response[$key] : $response;
}
}
82 changes: 82 additions & 0 deletions ConnectedAccount/Service/HLMod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);

/**
* This file is a part of [HLModerators] Connected Account Add-On for XenForo v2.2.x.
* All rights reserved.
*
* Developed by HLModerators.
*/

namespace HLModerators\Auth\ConnectedAccount\Service;


use OAuth\Common\Consumer\CredentialsInterface;
use OAuth\Common\Http\Client\ClientInterface;
use OAuth\Common\Http\Exception\TokenResponseException;
use OAuth\Common\Http\Uri\Uri;
use OAuth\Common\Http\Uri\UriInterface;
use OAuth\Common\Storage\TokenStorageInterface;
use OAuth\Common\Token\TokenInterface;
use OAuth\OAuth2\Service\AbstractService;
use OAuth\OAuth2\Token\StdOAuth2Token;

class HLMod extends AbstractService
{
const SCOPE_USER_READ = 'user:read';

public function __construct(CredentialsInterface $credentials, ClientInterface $httpClient,
TokenStorageInterface $storage, $scopes = array(), UriInterface $baseApiUri = null,
$stateParameterInAutUrl = false, $apiVersion = "")
{
parent::__construct($credentials, $httpClient, $storage, $scopes,
$baseApiUri, $stateParameterInAutUrl, $apiVersion);

$this->baseApiUri = new Uri('https://hlmod.ru/api/');
}

protected function parseAccessTokenResponse($responseBody): TokenInterface
{
$data = json_decode($responseBody, true);
if (!is_array($data) || !($data['success'] ?? false))
{
throw new TokenResponseException('Unable to parse response.');
}
elseif (isset($data['error']))
{
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
}

$token = new StdOAuth2Token();
$token->setAccessToken($data['access_token']);
$token->setRefreshToken($data['refresh_token']);
$token->setEndOfLife($data['server_time'] + $data['expires_in']);

return $token;
}

public function getAuthorizationEndpoint(): UriInterface
{
return new Uri('https://hlmod.ru/login/oauth/');
}

protected function getAuthorizationMethod(): int
{
return -1;
}

public function getAccessTokenEndpoint(): UriInterface
{
return new Uri('https://hlmod.ru/api/auth/hlm-oauth/token/');
}

protected function getExtraApiHeaders(): array
{
$token = $this->storage->retrieveAccessToken($this->service());
// We're already know: this token isn't expired (checked in `request()` method).

return [
'XF-Api-Key' => $token->getAccessToken()
];
}
}
67 changes: 67 additions & 0 deletions Setup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);

/**
* This file is a part of [HLModerators] Connected Account Add-On for XenForo v2.2.x.
* All rights reserved.
*
* Developed by HLModerators.
*/

namespace HLModerators\Auth;


use XF\AddOn\AbstractSetup;
use XF\AddOn\StepRunnerInstallTrait;
use XF\AddOn\StepRunnerUninstallTrait;
use XF\AddOn\StepRunnerUpgradeTrait;
use XF\Entity\ConnectedAccountProvider;

class Setup extends AbstractSetup
{
use StepRunnerInstallTrait;
use StepRunnerUpgradeTrait;
use StepRunnerUninstallTrait;

public function installStep1()
{
/** @var ConnectedAccountProvider $provider */
$provider = $this->app->em()->create('XF:ConnectedAccountProvider');
$provider->bulkSet($this->getProviderDetails());
$provider->save();
}

public function uninstallStep1()
{
/** @var ConnectedAccountProvider $provider */
$provider = $this->app->em()->findOne('XF:ConnectedAccountProvider', $this->getProviderField('provider_id'));
if (!$provider)
{
// Damn. Something already removed our record.
return;
}

$provider->delete();
}

/**
* @return array
*/
protected function getProviderDetails(): array
{
return [
'provider_id' => 'hlmod',
'provider_class' => 'HLModerators\Auth:Provider\HLMod',
'display_order' => 200,
'options' => [],
];
}

/**
* @param string $field
*/
protected function getProviderField($field)
{
return $this->getProviderDetails()[$field] ?? null;
}
}
2 changes: 2 additions & 0 deletions _no_upload/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# [HLModerators] Connected Account
Allows for users on your board perform authentication via [HLMod.ru](https://hlmod.ru/?utm_source=readme&utm_campaign=hlmod_connected_account).
26 changes: 26 additions & 0 deletions _output/phrases/_metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"con_acc.hlmod.txt": {
"global_cache": false,
"version_id": 1000010,
"version_string": "1.0.0 Alpha",
"hash": "4cbe2f7cb0ce9a9ba1aba7d2deb3c48d"
},
"con_acc_desc.hlmod.txt": {
"global_cache": false,
"version_id": 1000010,
"version_string": "1.0.0 Alpha",
"hash": "d41d8cd98f00b204e9800998ecf8427e"
},
"con_acc_hlmod_client_id_explain.txt": {
"global_cache": false,
"version_id": 1000010,
"version_string": "1.0.0 Alpha",
"hash": "950155009b135822ddc3c10498c9e0e1"
},
"con_acc_hlmod_client_secret_explain.txt": {
"global_cache": false,
"version_id": 1000010,
"version_string": "1.0.0 Alpha",
"hash": "9b6b1b1d1033f8b3df22d334e5daafb1"
}
}
1 change: 1 addition & 0 deletions _output/phrases/con_acc.hlmod.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
HLMod
Empty file.
1 change: 1 addition & 0 deletions _output/phrases/con_acc_hlmod_client_id_explain.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The client ID that is associated with your <a href="https://hlmod.ru/hlmoauth_applications/" target="_blank">HLMod developer application</a> for this domain.
1 change: 1 addition & 0 deletions _output/phrases/con_acc_hlmod_client_secret_explain.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The secret for the HLMod application you created for this domain.
5 changes: 5 additions & 0 deletions _output/template_modifications/_metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"public/DAB78876_BB4A_4F1B_92E5_D31D257B1075.json": {
"hash": "4e7472f95a3056d8e43be1f82015b2e7"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"template": "core.less",
"description": "Include .less for auth button styling",
"execution_order": 10,
"enabled": true,
"action": "str_replace",
"find": "{{ include('core_button.less') }}",
"replace": "$0\n{{ include('hlmod_auth_button.less') }}"
}
22 changes: 22 additions & 0 deletions _output/templates/_metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"admin/connected_account_provider_hlmod.html": {
"version_id": 1000010,
"version_string": "1.0.0 Alpha",
"hash": "ca635d211f934045a9492d998f31ee49"
},
"admin/connected_account_provider_test_hlmod.html": {
"version_id": 1000010,
"version_string": "1.0.0 Alpha",
"hash": "52a7aa4d5aa6bdc0d1a03eae3812ee7c"
},
"public/connected_account_associated_hlmod.html": {
"version_id": 1000010,
"version_string": "1.0.0 Alpha",
"hash": "3cbac69d6470a9416f9fb5986e33e8a6"
},
"public/hlmod_auth_button.less": {
"version_id": 1000010,
"version_string": "1.0.0 Alpha",
"hash": "090618c2a2f51cc9381d0c0122f02eef"
}
}
9 changes: 9 additions & 0 deletions _output/templates/admin/connected_account_provider_hlmod.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<xf:textboxrow name="options[client_id]" value="{$options.client_id}"
label="{{ phrase('client_id') }}"
hint="{{ phrase('required') }}"
explain="{{ phrase('con_acc_hlmod_client_id_explain') }}" />

<xf:textboxrow name="options[client_secret]" value="{$options.client_secret}"
label="{{ phrase('client_secret') }}"
hint="{{ phrase('required') }}"
explain="{{ phrase('con_acc_hlmod_client_secret_explain') }}" />
14 changes: 14 additions & 0 deletions _output/templates/admin/connected_account_provider_test_hlmod.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<xf:if is="!$providerData">
<xf:macro template="connected_account_provider_test_macros" name="explain"
arg-providerTitle="{$provider.title}"
arg-keyName="{{ phrase('client_id') }}"
arg-keyValue="{$provider.options.client_id}" />
<xf:else />
<xf:macro template="connected_account_provider_test_macros" name="success" />

<xf:macro template="connected_account_provider_test_macros" name="display_name"
arg-name="{$providerData.username}" />

<xf:macro template="connected_account_provider_test_macros" name="picture"
arg-url="{$providerData.avatar_url}" />
</xf:if>
21 changes: 21 additions & 0 deletions _output/templates/public/connected_account_associated_hlmod.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<xf:if is="$providerData.profile_link">
<xf:if is="$providerData.avatar_url">
<a href="{$providerData.profile_link}" target="_blank">
<img src="{$providerData.avatar_url}" width="48" alt="" />
</a>
</xf:if>

<div>
<a href="{$providerData.profile_link}" target="_blank">
{$providerData.username}
</a>
</div>
<xf:else />
<xf:if is="$providerData.avatar_url">
<img src="{$providerData.avatar_url}" width="48" alt="" />
</xf:if>

<div>
{$providerData.username}
</div>
</xf:if>
Loading

0 comments on commit 98b1654

Please sign in to comment.