/**
  * Creates new preshared secret
  *
  * @param string|NULL note
  * @param DateTime|NULL expiration date
  * @return string[16]
  * @throws Nette\InvalidArgumentException if invalid expiration date
  */
 public function createPsk($expiration = NULL, $note = NULL)
 {
     if ($expiration !== NULL && !$expiration instanceof \DateTime) {
         throw new Nette\InvalidArgumentException("Expected NULL or DateTime instance");
     }
     while (true) {
         $psk = Strings::randomHumanToken(16);
         $values = array($this->fieldName[self::PSK] => $psk, $this->fieldName[self::EXPIRATION] => $expiration, $this->fieldName[self::NOTE] => $note);
         try {
             $this->db->insert($this->tableName, $values)->execute();
             break;
         } catch (\DibiException $e) {
             if ($e->getCode() != 1062) {
                 throw $e;
             }
         }
     }
     return $psk;
 }
Beispiel #2
0
 /**
  * Constructor.
  * Takes translator service and prepares URL for save requests
  */
 function __construct(vBuilder\Localization\Translator $translator, Nette\Http\Request $httpRequest, Nette\Http\Session $session, Nette\DI\Container $container)
 {
     $this->translator = $translator;
     $this->httpRequest = $httpRequest;
     $this->basePath = $container->parameters['wwwDir'] . '/..';
     if (!isset(self::$registeredRoute)) {
         throw new Nette\InvalidStateException(__CLASS__ . "::register not called?");
     }
     // Creates unique authorization token (basic CSRF prevention)
     $session = $session->getSection(strtr(__CLASS__, '\\', '.'));
     if (!isset($session->authToken)) {
         $session->authToken = vBuilder\Utils\Strings::randomHumanToken(8);
     }
     $this->authToken = $session->authToken;
 }
Beispiel #3
0
// TODO: Podpora pro uziv. role
use vBuilder\Security\User, vBuilder\Utils\CliArgsParser, vBuilder\Utils\Strings, vBuilder\Security\IdentityFactory;
$container = (require __DIR__ . '/bootstrap.php');
// -----------------------------------------------------------------------------
// ARGUMENTS
// -----------------------------------------------------------------------------
$args = new CliArgsParser();
$args->addOption('password', 'secret', 'user password', NULL)->setNumRequiredArgs(1, 1)->setArgumentHelp('username');
if (!$args->parse()) {
    echo "\n" . $args->getErrorMsg() . "\n\n";
    $args->printUsage();
    echo "\n";
    exit;
}
list($user) = $args->getArguments();
$password = $args->get('password') !== FALSE ? $args->get('password') : Strings::randomHumanToken();
// -----------------------------------------------------------------------------
// INIT
// -----------------------------------------------------------------------------
$db = $container->getByType('DibiConnection');
$authn = $container->user->getAuthenticator(User::AUTHN_METHOD_PASSWORD, User::AUTHN_SOURCE_DATABASE);
$rolesTable = $authn->identityFactory->getTableName(IdentityFactory::TABLE_ROLES);
$roles = array('Administrator');
// -----------------------------------------------------------------------------
// DB
// -----------------------------------------------------------------------------
$data = array($authn->getColumn($authn::USERNAME) => $user, $authn->getColumn($authn::PASSWORD) => $authn->getPasswordHasher()->hashPassword($password));
try {
    $db->query("INSERT INTO %n", $authn->tableName, $data);
    $uid = $db->getInsertId();
    echo "ID noveho uzivatele: {$uid}\n";
Beispiel #4
0
 /**
  * This method will be called when the component (or component's parent)
  * becomes attached to a monitored object. Do not call this method yourself.
  *
  * @param  Nette\ComponentModel\IComponent
  * @return void
  */
 protected function attached($parent)
 {
     parent::attached($parent);
     // Creates session storage for each instance of DataTable
     $context = $parent->getPresenter()->getContext();
     $sessionSection = $context->session->getSection(strtr(__CLASS__, '\\', '.'));
     $this->session =& $sessionSection->{$this->getUniqueId()};
     if ($this->session == NULL) {
         $this->session = new \StdClass();
     }
     // Creates unique authorization token (CSRF prevention)
     if (!isset($this->session->authToken)) {
         $this->session->authToken = vBuilder\Utils\Strings::randomHumanToken(8);
     }
 }