view release on metacpan or search on metacpan
lib/WWW/Suffit/AuthDB/Model.pm view on Meta::CPAN
"name" CHAR(255) DEFAULT NULL, -- Full user name
"email" CHAR(255) DEFAULT NULL, -- Email address
"password" CHAR(255) NOT NULL, -- Password hash
"algorithm" CHAR(64) DEFAULT NULL, -- Password hash Algorithm (SHA256)
"role" CHAR(255) DEFAULT NULL, -- Role name
"flags" INTEGER DEFAULT 0, -- Flags
"created" INTEGER DEFAULT NULL, -- Created at
"not_before" INTEGER DEFAULT NULL, -- Not Before
"not_after" INTEGER DEFAULT NULL, -- Not After
"public_key" TEXT DEFAULT NULL, -- Public Key (RSA/X509)
"private_key" TEXT DEFAULT NULL, -- Private Key (RSA/X509)
"attributes" TEXT DEFAULT NULL, -- Attributes (JSON)
"comment" TEXT DEFAULT NULL -- Comment
);
CREATE TABLE IF NOT EXISTS "groups" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
"groupname" CHAR(64) NOT NULL UNIQUE, -- Group name
"description" TEXT DEFAULT NULL -- Description
);
CREATE TABLE IF NOT EXISTS "realms" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
lib/WWW/Suffit/AuthDB/Model.pm view on Meta::CPAN
`name` VARCHAR(255) DEFAULT NULL, -- Full user name
`email` VARCHAR(255) DEFAULT NULL, -- Email address
`password` VARCHAR(255) NOT NULL, -- Password hash
`algorithm` VARCHAR(64) DEFAULT NULL, -- Password hash Algorithm (SHA256)
`role` VARCHAR(255) DEFAULT NULL, -- Role name
`flags` INT(11) DEFAULT 0, -- Flags
`created` INT(11) DEFAULT NULL, -- Created at
`not_before` INT(11) DEFAULT NULL, -- Not Before
`not_after` INT(11) DEFAULT NULL, -- Not After
`public_key` TEXT DEFAULT NULL, -- Public Key (RSA/X509)
`private_key` TEXT DEFAULT NULL, -- Private Key (RSA/X509)
`attributes` TEXT DEFAULT NULL, -- Attributes (JSON)
`comment` TEXT DEFAULT NULL, -- Comment
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE IF NOT EXISTS `groups` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`groupname` VARCHAR(64) NOT NULL, -- Group name
`description` TEXT DEFAULT NULL, -- Description
PRIMARY KEY (`id`),
lib/WWW/Suffit/AuthDB/Model.pm view on Meta::CPAN
name => "Administrator",
email => 'root@localhost',
password => "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918",
algorithm => "SHA256",
role => "System administrator",
flags => 0,
created => time(),
not_before => time(),
not_after => undef,
public_key => "",
private_key => "",
attributes => qq/{"disabled": 0}/,
comment => "This user added by default",
) or die($model->error);
Add new user recored
=head2 user_del
$model->user_del("admin") or die($model->error);
lib/WWW/Suffit/AuthDB/Model.pm view on Meta::CPAN
username => "admin",
name => "Administrator",
email => 'root@localhost',
password => "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918",
algorithm => "SHA256",
role => "System administrator",
flags => 0,
not_before => time(),
not_after => undef,
public_key => "",
private_key => "",
attributes => qq/{"disabled": 0}/,
comment => "This user added by default",
) or die($model->error);
Update recored by username
=head2 user_setkeys
$model->user_setkeys(
id => 123,
public_key => $public_key,
private_key => $private_key,
) or die($model->error);
Sets keys to user's data
=head2 user_tokens
my @table = $model->user_tokens($username);
Returns all tokens for user
lib/WWW/Suffit/AuthDB/Model.pm view on Meta::CPAN
use constant DML_STAT_SET => <<'DML';
UPDATE `stats`
SET `address` = ?, `username` =?, `dismiss` = ?, `updated` = ?
WHERE `id` = ?
DML
# User DMLs
use constant DML_USER_ADD => <<'DML';
INSERT INTO `users`
(`username`,`name`,`email`,`password`,`algorithm`,`role`,`flags`,`created`,
`not_before`,`not_after`,`public_key`,`private_key`,`attributes`,`comment`
)
VALUES
(?,?,?,?,?,?,?,?,?,?,?,?,?,?)
DML
use constant DML_USER_GET => <<'DML';
SELECT `id`,`username`,`name`,`email`,`password`,`algorithm`,`role`,`flags`,`created`,
`not_before`,`not_after`,`public_key`,`private_key`,`attributes`,`comment`
FROM `users`
WHERE `username` = ?
DML
use constant DML_USER_SET => <<'DML';
UPDATE `users`
SET `name` = ?, `email` = ?, `password` = ?, `algorithm` = ?, `role` = ?, `flags` = ?,
`not_before` = ?, `not_after` = ?, `public_key` = ?, `private_key` = ?,
`attributes` = ?, `comment` = ?
WHERE `username` = ?
DML
use constant DML_USER_DEL => <<'DML';
DELETE FROM `users` WHERE `username` = ?
DML
use constant DML_USER_GETALL => <<'DML';
SELECT `id`,`username`,`name`,`email`,`password`,`algorithm`,`role`,`flags`,`created`,
`not_before`,`not_after`,`public_key`,`private_key`,`attributes`,`comment`
FROM `users`
ORDER BY `username` ASC
DML
use constant DML_PASSWD => <<'DML';
UPDATE `users`
SET `password` = ?
WHERE `username` = ?
DML
use constant DML_USER_SEARCH => <<'DML';
SELECT `id`,`username`,`name`,`role`
lib/WWW/Suffit/AuthDB/Model.pm view on Meta::CPAN
ORDER BY
grpsusrs.`groupname` ASC
DML
use constant DML_USER_EDIT => <<'DML';
UPDATE `users`
SET `name` = ?, `email` = ?, `role` = ?, `comment` = ?
WHERE `id` = ?
DML
use constant DML_USER_SETKEYS => <<'DML';
UPDATE `users`
SET `public_key` = ?, `private_key` = ?
WHERE `id` = ?
DML
# Group DMLs
use constant DML_GROUP_ADD => <<'DML';
INSERT INTO `groups` (`groupname`,`description`)
VALUES (?,?)
DML
use constant DML_GROUP_GET => <<'DML';
SELECT `id`,`groupname`,`description`
lib/WWW/Suffit/AuthDB/Model.pm view on Meta::CPAN
sub user_add {
my $self = shift;
my %data = @_;
return 0 unless $self->ping;
# Add
$self->query(DML_USER_ADD,
$data{username}, $data{name}, $data{email}, $data{password},
uc($data{algorithm} || DEFAULT_ALGORITHM), $data{role}, $data{flags},
$data{created} || time(), $data{not_before} || time(), $data{not_after},
$data{public_key}, $data{private_key}, $data{attributes},
$data{comment},
) or return 0;
# Ok
return 1;
}
sub user_set { # set by username
my $self = shift;
my %data = @_;
return 0 unless $self->ping;
unless (length($data{username} // '')) {
$self->error("No username specified");
return 0;
}
# Set
$self->query(DML_USER_SET,
$data{name}, $data{email}, $data{password},
uc($data{algorithm} || DEFAULT_ALGORITHM), $data{role}, $data{flags},
$data{not_before} || time(), $data{not_after},
$data{public_key}, $data{private_key}, $data{attributes},
$data{comment},
$data{username},
) or return 0;
# Ok
return 1;
}
sub user_edit { # set by id
my $self = shift;
my %data = @_;
lib/WWW/Suffit/AuthDB/Model.pm view on Meta::CPAN
sub user_setkeys {
my $self = shift;
my %data = @_;
return 0 unless $self->ping;
unless ($data{id}) {
$self->error("No id of user specified");
return 0;
}
# Set
$self->query(DML_USER_SETKEYS, $data{public_key}, $data{private_key}, $data{id}) or return 0;
# Ok
return 1;
}
# Group CRUDs
sub group_add {
my $self = shift;
my %data = @_;
return 0 unless $self->ping;
lib/WWW/Suffit/AuthDB/Model.pm view on Meta::CPAN
"name" CHAR(255) DEFAULT NULL, -- Full user name
"email" CHAR(255) DEFAULT NULL, -- Email address
"password" CHAR(255) NOT NULL, -- Password hash
"algorithm" CHAR(64) DEFAULT NULL, -- Password hash Algorithm (SHA256)
"role" CHAR(255) DEFAULT NULL, -- Role name
"flags" INTEGER DEFAULT 0, -- Flags
"created" INTEGER DEFAULT NULL, -- Created at
"not_before" INTEGER DEFAULT NULL, -- Not Before
"not_after" INTEGER DEFAULT NULL, -- Not After
"public_key" TEXT DEFAULT NULL, -- Public Key (RSA/X509)
"private_key" TEXT DEFAULT NULL, -- Private Key (RSA/X509)
"attributes" TEXT DEFAULT NULL, -- Attributes (JSON)
"comment" TEXT DEFAULT NULL -- Comment
) ;
CREATE TABLE IF NOT EXISTS "groups" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
"groupname" CHAR(64) NOT NULL UNIQUE, -- Group name
"description" TEXT DEFAULT NULL -- Description
) ;
CREATE TABLE IF NOT EXISTS "realms" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
lib/WWW/Suffit/AuthDB/Model.pm view on Meta::CPAN
`name` VARCHAR(255) DEFAULT NULL, -- Full user name
`email` VARCHAR(255) DEFAULT NULL, -- Email address
`password` VARCHAR(255) NOT NULL, -- Password hash
`algorithm` VARCHAR(64) DEFAULT NULL, -- Password hash Algorithm (SHA256)
`role` VARCHAR(255) DEFAULT NULL, -- Role name
`flags` INT(11) DEFAULT 0, -- Flags
`created` INT(11) DEFAULT NULL, -- Created at
`not_before` INT(11) DEFAULT NULL, -- Not Before
`not_after` INT(11) DEFAULT NULL, -- Not After
`public_key` TEXT DEFAULT NULL, -- Public Key (RSA/X509)
`private_key` TEXT DEFAULT NULL, -- Private Key (RSA/X509)
`attributes` TEXT DEFAULT NULL, -- Attributes (JSON)
`comment` TEXT DEFAULT NULL, -- Comment
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE IF NOT EXISTS `groups` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`groupname` VARCHAR(64) NOT NULL, -- Group name
`description` TEXT DEFAULT NULL, -- Description
PRIMARY KEY (`id`),
lib/WWW/Suffit/AuthDB/Model.pm view on Meta::CPAN
name VARCHAR(255) DEFAULT NULL, -- Full user name
email VARCHAR(255) DEFAULT NULL, -- Email address
password VARCHAR(255) NOT NULL, -- Password hash
algorithm VARCHAR(64) DEFAULT NULL, -- Password hash Algorithm (SHA256)
role VARCHAR(255) DEFAULT NULL, -- Role name
flags INT DEFAULT 0, -- Flags
created INT DEFAULT NULL, -- Created at
not_before INT DEFAULT NULL, -- Not Before
not_after INT DEFAULT NULL, -- Not After
public_key TEXT DEFAULT NULL, -- Public Key (RSA/X509)
private_key TEXT DEFAULT NULL, -- Private Key (RSA/X509)
attributes TEXT DEFAULT NULL, -- Attributes (JSON)
comment TEXT DEFAULT NULL, -- Comment
PRIMARY KEY (id),
CONSTRAINT username UNIQUE (username)
) ;
CREATE TABLE IF NOT EXISTS groups (
id INT NOT NULL GENERATED ALWAYS AS IDENTITY,
groupname VARCHAR(64) NOT NULL, -- Group name
description TEXT DEFAULT NULL, -- Description
PRIMARY KEY (id),
lib/WWW/Suffit/AuthDB/Role/CRUD.pm view on Meta::CPAN
username => "foo",
name => "Test User",
email => 'test@localhost',
password => "098f6bcd4621d373cade4e832627b4f6",
algorithm => "MD5",
role => "Test user",
flags => 0,
not_before => time(),
not_after => undef,
public_key => "",
private_key => "",
attributes => qq/{"disabled": 0}/,
comment => "This user added for test",
) or die $authdb->error;
This method adds new user or doing update data of existing user in pure mode
=head2 user_pure_set
This method is deprecated! See L</user_pset>
lib/WWW/Suffit/AuthDB/Role/CRUD.pm view on Meta::CPAN
username => "foo",
name => "Test User",
email => 'test@localhost',
password => "MyPassword", # Unsafe password
algorithm => "SHA256",
role => "Test user",
flags => 0,
not_before => time(),
not_after => undef,
public_key => "",
private_key => "",
attributes => qq/{"disabled": 0}/,
comment => "This user added for test",
) or die $authdb->error;
This method adds new user or doing update data of existing user
=head2 user_setkeys
$authdb->user_setkeys(
username => "foo",
public_key => $public_key,
private_key => $private_key,
) or die $authdb->error;
This method sets keys for user
=head2 user_tokens
my @tokens = $authdb->user_tokens( $username );
This method returns all tokens of specified user
lib/WWW/Suffit/AuthDB/Role/CRUD.pm view on Meta::CPAN
name => $user->{"name"} // '',
email => $user->{"email"} // '',
password => $user->{"password"} // '',
algorithm => $user->{"algorithm"} // '',
role => $user->{"role"} // '',
flags => $user->{"flags"} || 0,
created => $now,
not_before => $now,
not_after => is_true_flag($user->{"disabled"}) ? $now : undef,
public_key => $user->{"public_key"} // '',
private_key => $user->{"private_key"} // '',
attributes => $user->{"attributes"} // '',
comment => $user->{"comment"} // '',
) or return;
# Add groups to grpsusrs
my $groups = $user->{"groups"} || [];
$groups = [] unless is_array_ref($groups);
foreach my $g (@$groups) {
$grpsusrs{"$g:$username"} = {
groupname => $g,
lib/WWW/Suffit/AuthDB/User.pm view on Meta::CPAN
Sets or returns the time before which user data is considered invalid
=head2 password
$user = $user->password(sha256_hex('MyNewPassphrase'));
my $password = $user->password;
Sets and returns hex notation of user password digest (sha256, eg.).
See L</algorithm> attribute
=head2 private_key
$user = $user->private_key('...');
my $private_key = $user->private_key;
Sets and returns private key of user
=head2 public_key
$user = $user->public_key('...');
my $public_key = $user->public_key;
Sets and returns public key of user
lib/WWW/Suffit/AuthDB/User.pm view on Meta::CPAN
has email => '';
has error => '';
has expires => 0;
has flags => 0;
has groups => sub { return [] };
has id => 0;
has name => '';
has not_after => undef;
has not_before => undef;
has password => '';
has private_key => '';
has public_key => '';
has role => 'Regular user';
has username => undef;
has is_cached => 0; # 0 or 1
has cached => 0; # steady_time() of cached
has cachekey => '';
has is_authorized => 0;
sub is_valid {
my $self = shift;
src/authdb.json view on Meta::CPAN
"users": [
{
"username": "admin",
"name": "Administrator",
"email": "root@localhost",
"algorithm": "SHA256",
"password": "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918",
"role": "System administrator",
"flags": 0,
"public_key": "",
"private_key": "",
"attributes": "",
"comment": "Password of the user is `admin`. Please change it immediately!",
"disabled": false,
"groups": []
},
{
"username": "test",
"name": "Test",
"email": "test@localhost",
"algorithm": "SHA256",
t/02-authdb.t view on Meta::CPAN
# Add new user
ok($authdb->user_set(
username => "foo",
name => "Foo",
email => 'foo@localhost',
password => "test",
algorithm => "MD5",
role => "Test foo user",
flags => 0,
public_key => "",
private_key => "",
attributes => qq/{"disabled": 0}/,
comment => "User for test only",
), "Add new user") or diag $authdb->error;
# Edit the user data directly, without preprocessing
ok($authdb->user_pset(
username => "foo",
name => "Foo",
email => 'foo@localhost',
password => "098f6bcd4621d373cade4e832627b4f6",
algorithm => "MD5",
role => "Test foo user",
flags => 0,
public_key => "",
private_key => "",
attributes => qq/{"disabled": 0}/,
comment => "User for test only (edited)",
), "Edit the user data directly") or diag $authdb->error;
# Change password
ok($authdb->user_passwd(
username => "foo",
password => "password",
), "Change password") or diag $authdb->error;
# Set keys pair
ok($authdb->user_setkeys(
username => "foo",
public_key => 'public_key',
private_key => 'private_key',
), "Set keys pair") or diag $authdb->error;
# Get data
my %data = $authdb->user_get("foo");
ok(!$authdb->error, "Get user data") or diag $authdb->error;
#note explain \%data;
# Delete user
ok($authdb->user_del( "foo" ), "Delete foo user") or diag $authdb->error;
t/03-model.t view on Meta::CPAN
name => "Administrator",
email => 'root@localhost',
password => "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918",
algorithm => "SHA256",
role => "System administrator",
flags => 0,
created => time(),
not_before => time(),
not_after => undef,
public_key => "",
private_key => "",
attributes => qq/{"disabled": 0}/,
comment => "This user added by default",
), "Add new user") or diag $model->error;
}
# Get user's data
{
my %data = $model->user_get("admin");
ok(!$model->error, "Get user's data") or diag $model->error;
#note(explain(\%data));
t/03-model.t view on Meta::CPAN
username => "admin",
name => "Administrator",
email => 'root@localhost',
password => "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918",
algorithm => "SHA256",
role => "System administrator",
flags => 0,
not_before => time(),
not_after => undef,
public_key => "",
private_key => "",
attributes => qq/{"disabled": 0}/,
comment => "This user was modified",
), "Set user's data") or diag $model->error;
}
# Get all records
{
my @all = $model->user_getall();
ok(scalar(@all), "Get all users") or diag $model->error;
#note(explain(\@all));