Skip to content

Commit

Permalink
✨ Drop AuthMethod classes
Browse files Browse the repository at this point in the history
  • Loading branch information
CrazyHackGUT committed Jan 2, 2020
1 parent 59b97ba commit ee3fa54
Show file tree
Hide file tree
Showing 19 changed files with 99 additions and 351 deletions.

This file was deleted.

19 changes: 0 additions & 19 deletions upload/src/addons/SModders/TelegramCore/AuthMethod/Direct.php

This file was deleted.

24 changes: 0 additions & 24 deletions upload/src/addons/SModders/TelegramCore/AuthMethod/OAuth.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ public function handleAuthorization(Controller $controller, ConnectedAccountProv

/** @var \SModders\TelegramCore\SubContainer\Telegram $telegram */
$telegram = $app['smodders.telegram'];
$authMethod = $telegram->authMethod($provider->options['method'], $this);

return $authMethod->setController($controller)
->setProviderEntity($provider)
->handle();

$viewParams = [
'botName' => $provider->options['name'],
'redirectUri' => $this->getRedirectUri($provider),
];

return $controller->view('SModders\TelegramCore:AuthMethod\OAuth', 'public:smodders_tgcore__auth_page', $viewParams);
}

public function requestProviderToken(StorageState $storageState, Request $request, &$error = null, $skipStoredToken = false)
Expand Down Expand Up @@ -127,29 +129,6 @@ public function verifyConfig(array &$options, &$error = null)

return true;
}

public function renderConfig(ConnectedAccountProvider $provider)
{
$app = \XF::app();
$authMethods = [];

/**
* @var string $authMethodId
* @var array $data
*/
foreach ($app['smodders.telegram']['authMethods'] as $authMethodId => $data)
{
$authMethods[$authMethodId] = [
'name' => \XF::phraseDeferred($data['phrase']),
'explain' => \XF::phraseDeferred($data['phrase'] . '_explain'),
];
}

return $app->templater()->renderTemplate('admin:connected_account_provider_' . $provider->provider_id, [
'options' => $this->getEffectiveOptions($provider->options),
'authMethods' => $authMethods,
]);
}

protected function isValidHash(array $data)
{
Expand Down
51 changes: 34 additions & 17 deletions upload/src/addons/SModders/TelegramCore/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use TelegramBot\Api\Types\Message;
use XF\App;
use XF\Container;
use XF\Import\Manager;
use XF\SubContainer\Import;

class Listener
Expand Down Expand Up @@ -43,25 +42,19 @@ public static function app_setup(App $app)
*/
public static function smodders_tgcore__client_setup(Client $client)
{
$client->command('start', function (Message $message) use ($client)
$options = \XF::options();
$templater = \XF::app()->templater();

// Define function for sending authentication messages.
$sendAuthMessage = function(Message $message)
use ($client, $options, $templater)
{
// \XF::dump($message); exit();
$chat = $message->getChat();
if ($chat->getType() != 'private')
{
return;
}

if ($message->getText() != '/start smodders_tgcore__authenticate')
{
return;
}

$options = \XF::options();

$client->call('sendMessage', [
'chat_id' => $chat->getId(),
'parse_mode' => 'HTML',

'reply_markup' => json_encode([
'inline_keyboard' => [
[
Expand All @@ -75,15 +68,39 @@ public static function smodders_tgcore__client_setup(Client $client)
]
]
]),

'text' => \XF::app()->templater()->renderTemplate('public:smodders_tgcore__directauth_message', [
'text' => $templater->renderTemplate('public:smodders_tgcore__directauth_message', [
'message' => $message,
'board' => [
'url' => $options->boardUrl,
'title' => $options->boardTitle,
],
]),
]);
};

// Register our command listeners.
$client->command('auth', function (Message $message)
use ($sendAuthMessage)
{
$sendAuthMessage($message);
});

$client->command('start', function (Message $message)
use ($sendAuthMessage)
{
$chat = $message->getChat();
if ($chat->getType() != 'private')
{
return;
}

if ($message->getText() != '/start smodders_tgcore__authenticate')
{
return;
}

$sendAuthMessage($message);
});
}

Expand Down
53 changes: 0 additions & 53 deletions upload/src/addons/SModders/TelegramCore/SubContainer/Telegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,53 +26,10 @@ public function initialize()
{
$container = $this->container;

$this->initializeAuthMethods($container);
$this->initializeApi($container);
$this->initializeBotData($container);
}

/**
* Initializes a factory for auth methods and map.
* @param Container $container
*/
protected function initializeAuthMethods(Container $container)
{
$container->factory('authMethod', function($type, array $params, Container $c)
{
/** @var array $map */
$map = $c['authMethods'];
if (isset($map[$type]))
{
$type = $map[$type]['provider'];
}

$class = \XF::stringToClass($type, '%s\AuthMethod\%s');
$class = $this->extendClass($class);

if (!class_exists($class))
{
throw new \InvalidArgumentException("Unknown auth method class '$class'");
}

return new $class($this->app, $params['provider']);
});

$container['authMethods'] = function (Container $c)
{
return [
'direct' => [
'provider' => 'SModders\TelegramCore:Direct',
'phrase' => 'smodders_tgcore.authMethod_direct',
],

'oauth' => [
'provider' => 'SModders\TelegramCore:OAuth',
'phrase' => 'smodders_tgcore.authMethod_oauth'
],
];
};
}

/**
* Initializes a items in container for API accessing.
*
Expand Down Expand Up @@ -155,16 +112,6 @@ protected function initializeBotData(Container $container)
return $c['bot']['token'];
};
}

/**
* @param $type
* @param AbstractProvider $provider
* @return \SModders\TelegramCore\AuthMethod\AbstractAuthMethod
*/
public function authMethod($type, AbstractProvider $provider)
{
return $this->container->create('authMethod', $type, ['provider' => $provider]);
}

/**
* @param null|string $token
Expand Down
Loading

0 comments on commit ee3fa54

Please sign in to comment.