Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev: Make command to create new OAuth #35

Merged
merged 11 commits into from
Apr 18, 2023
33 changes: 28 additions & 5 deletions src/Commands/Generators/NewShieldOauthGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Datamweb\ShieldOAuth\Commands\Generators;

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\GeneratorTrait;

class NewShieldOauthGenerator extends BaseCommand
Expand Down Expand Up @@ -63,19 +64,41 @@ class NewShieldOauthGenerator extends BaseCommand
* @var array<string, string>
*/
protected $options = [
'-f' => 'Force overwrite existing file.',
'--force' => 'Force overwrite existing file.',
datamweb marked this conversation as resolved.
Show resolved Hide resolved
];

/**
* Actually execute a command.
*/
public function run(array $params): void
public function run(array $params): int
{
$this->component = 'Library';
$this->directory = 'Libraries/ShieldOAuth';
$this->template = 'newoauth.tpl.php';
// The name param is required
if (empty($params[0])) {
CLI::error('Please enter the name of the OAuth.', 'light_gray', 'red');

return 1;
}

$this->component = 'Library';
$this->directory = 'Libraries\ShieldOAuth';
$this->template = 'newoauth.tpl.php';
$this->classNameLang = 'CLI.generator.className.library';
$this->setHasClassName(false);

// Disable the suffix option
$this->setEnabledSuffixing(false);

// The proper class name should contain the `OAuth` suffix if it doesn't exist
$class = str_ireplace('oauth', 'OAuth', $params[0]);
if (strpos($class, 'OAuth') === false) {
$class .= 'OAuth';
}

$params[0] = $class;

// @TODO execute() is deprecated in CI v4.3.0.
$this->execute($params); // @phpstan-ignore-line suppress deprecated error.

return 0;
}
}
93 changes: 93 additions & 0 deletions src/Commands/Generators/Views/newoauth.tpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<@php

declare(strict_types=1);

namespace {namespace};

use Datamweb\ShieldOAuth\Libraries\Basic\AbstractOAuth;

class {class} extends AbstractOAuth
{
/** @var string $API_CODE_URL The API URL to get the Auth Code. */
private static $API_CODE_URL = '';

/** @var string $API_TOKEN_URL The API URL to get the Auth token. */
private static $API_TOKEN_URL = '';

/** @var string $API_USER_INFO_URL The API URL to get the user info. */
private static $API_USER_INFO_URL = '';

/** @var string $APPLICATION_NAME The name of this application. */
private static $APPLICATION_NAME = 'ShieldOAuth';

protected string $token;
protected string $client_id;
protected string $client_secret;
protected string $callback_url;

/**
* Class construct
*
* @see https://github.com/datamweb/shield-oauth/blob/develop/docs/add_other_oauth.md#writing-class-yahoo-oauth
* @param string $token
*/
public function __construct(string $token = '')
{
// your code here
sammyskills marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Create a link to transfer the user to the new provider.
*
* @see https://github.com/datamweb/shield-oauth/blob/develop/docs/add_other_oauth.md#writing-class-yahoo-oauth
* @param string $state
* @return string
*/
public function makeGoLink(string $state): string
{
// your code here
sammyskills marked this conversation as resolved.
Show resolved Hide resolved
return '';
}

/**
* Try to get the value of `access_token` according to the code
* received from the `makeGoLink()` method.
*
* @see https://github.com/datamweb/shield-oauth/blob/develop/docs/add_other_oauth.md#writing-class-yahoo-oauth
* @param array $allGet
* @return mixed
*/
public function fetchAccessTokenWithAuthCode(array $allGet): void
sammyskills marked this conversation as resolved.
Show resolved Hide resolved
{
// your code here
sammyskills marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Try to receive user information (including first name,
* last name, email, etc) according to the token code set
* in the previous step.
*
* @see https://github.com/datamweb/shield-oauth/blob/develop/docs/add_other_oauth.md#writing-class-yahoo-oauth
* @return object
*/
public function fetchUserInfoWithToken(): object
{
// your code here
sammyskills marked this conversation as resolved.
Show resolved Hide resolved
return json_decode('');
}

/**
* Set the fields received from each service OAuth to be recorded
* in each column of the table.
*
* @see https://github.com/datamweb/shield-oauth/blob/develop/docs/add_other_oauth.md#writing-class-yahoo-oauth
* @param string $nameOfProcess
* @param object $userInfo
* @return array
*/
public function setColumnsName(string $nameOfProcess, object $userInfo): array
{
// your code here
sammyskills marked this conversation as resolved.
Show resolved Hide resolved
return [];
}
}
sammyskills marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 12 additions & 0 deletions src/Config/Registrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,16 @@ public static function View(): array
],
];
}

/**
* Register the ShieldOAuth make:oauth generator template.
*/
public static function Generators(): array
{
return [
'views' => [
'make:oauth' => 'Datamweb\ShieldOAuth\Commands\Generators\Views\newoauth.tpl.php',
],
];
}
}