implement identity generation

This commit is contained in:
yggverse 2024-08-03 09:03:37 +03:00
parent 5e0ef1616e
commit 062d9ac02b

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Yggverse\Yoda\Entity\Browser\Container\Page; namespace Yggverse\Yoda\Entity\Browser\Container\Page;
use \Exception;
use \GtkButtonsType; use \GtkButtonsType;
use \GtkDialogFlags; use \GtkDialogFlags;
use \GtkMessageDialog; use \GtkMessageDialog;
@ -13,6 +14,8 @@ use \GtkResponseType;
use \Yggverse\Yoda\Entity\Browser\Container\Page; use \Yggverse\Yoda\Entity\Browser\Container\Page;
use \Yggverse\Yoda\Model\Identity\Gemini;
class Auth class Auth
{ {
// GTK // GTK
@ -26,8 +29,12 @@ class Auth
public const DIALOG_FORMAT_SECONDARY_TEXT = 'Select identity'; public const DIALOG_FORMAT_SECONDARY_TEXT = 'Select identity';
public const DIALOG_MESSAGE_FORMAT = 'Authorization'; public const DIALOG_MESSAGE_FORMAT = 'Authorization';
public const DIALOG_CONTENT_OPTION_LABEL_CREATE = 'Create new for this resource'; public const DIALOG_CONTENT_OPTION_LABEL_CREATE = 'Create new for this resource';
public const DIALOG_CONTENT_OPTION_LABEL_RECORD = '#%d (no name)';
public const DIALOG_CONTENT_OPTION_MARGIN = 8; public const DIALOG_CONTENT_OPTION_MARGIN = 8;
public const DIALOG_CONTENT_SPACING = 8; public const DIALOG_CONTENT_SPACING = 1;
// Extras
private array $_options = [];
public function __construct( public function __construct(
Page $page, Page $page,
@ -62,19 +69,91 @@ class Auth
$this::DIALOG_CONTENT_SPACING $this::DIALOG_CONTENT_SPACING
); );
$content->add( // Init new certificate option
$this->_option( $this->_options[0] = $this->_option(
_($this::DIALOG_CONTENT_OPTION_LABEL_CREATE) _($this::DIALOG_CONTENT_OPTION_LABEL_CREATE)
)
); );
$this->_options[0]->join_group(
$this->_options[0]
);
// Search database for auth records
foreach ($this->page->container->browser->database->auth->find() as $auth)
{
// Get related identity records
if ($identity = $this->page->container->browser->database->identity->get($auth->identity))
{
$this->_options[$identity->id] = $this->_option(
$identity->name ? $identity->name : sprintf(
_($this::DIALOG_CONTENT_OPTION_LABEL_RECORD),
$identity->id
)
);
$this->_options[$identity->id]->join_group(
$this->_options[0]
);
}
}
// Build options list
foreach ($this->_options as $option)
{
$content->add(
$option
);
}
// Render // Render
$this->gtk->show_all(); $this->gtk->show_all();
// Listen for chose // Listen for user chose
if (GtkResponseType::OK == $this->gtk->run()) if (GtkResponseType::OK == $this->gtk->run())
{ {
// @TODO // Get active option
foreach ($this->_options as $id => $option)
{
// Auth
if ($id)
{
// @TODO
}
// Generate new identity
else
{
// Detect driver
switch (true)
{
case mb_strtolower(
parse_url(
$this->page->navbar->request->getValue(),
PHP_URL_SCHEME
)
) == 'gemini':
// Init identity
$identity = new Gemini;
// Init auth record
$this->page->container->browser->database->auth->add(
$this->page->container->browser->database->identity->add(
$identity->crt(),
$identity->key()
),
$this->page->navbar->request->getValue()
);
break;
default:
throw new Exception;
}
}
}
$this->gtk->destroy(); $this->gtk->destroy();
return true; return true;
@ -91,12 +170,10 @@ class Auth
int $margin = self::DIALOG_CONTENT_OPTION_MARGIN int $margin = self::DIALOG_CONTENT_OPTION_MARGIN
): GtkRadioButton ): GtkRadioButton
{ {
$option = GtkRadioButton::new_with_label( $option = new GtkRadioButton;
$label
);
$option->set_margin_start( $option->set_label(
$margin $label
); );
$option->set_margin_start( $option->set_margin_start(
@ -111,8 +188,6 @@ class Auth
$margin $margin
); );
// @TODO connect signals?
return $option; return $option;
} }
} }