Auth.pm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package Trog::Auth;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures};
  6. use UUID::Tiny ':std';
  7. use Digest::SHA 'sha256';
  8. use Trog::SQLite;
  9. =head1 Trog::Auth
  10. An SQLite3 authdb.
  11. =head1 Termination Conditions
  12. Throws exceptions in the event the session database cannot be accessed.
  13. =head1 FUNCTIONS
  14. =head2 session2user(sessid) = (STRING, INT)
  15. Translate a session UUID into a username and id.
  16. Returns empty strings on no active session.
  17. =cut
  18. sub session2user ($sessid) {
  19. my $dbh = _dbh();
  20. my $rows = $dbh->selectall_arrayref("SELECT name,id FROM sess_user WHERE session=?",{ Slice => {} }, $sessid);
  21. return ('','') unless ref $rows eq 'ARRAY' && @$rows;
  22. return ($rows->[0]->{name},$rows->[0]->{id});
  23. }
  24. =head2 acls4user(user_id) = ARRAYREF
  25. Return the list of ACLs belonging to the user.
  26. The function of ACLs are to allow you to access content tagged 'private' which are also tagged with the ACL name.
  27. The 'admin' ACL is the only special one, as it allows for authoring posts, configuring tCMS, adding series (ACLs) and more.
  28. =cut
  29. sub acls4user($user_id) {
  30. my $dbh = _dbh();
  31. my $records = $dbh->selectall_arrayref("SELECT acl FROM user_acl WHERE user_id = ?", { Slice => {} }, $user_id);
  32. return () unless ref $records eq 'ARRAY' && @$records;
  33. my @acls = map { $_->{acl} } @$records;
  34. return \@acls;
  35. }
  36. =head2 mksession(user, pass) = STRING
  37. Create a session for the user and waste all other sessions.
  38. Returns a session ID, or blank string in the event the user does not exist or incorrect auth was passed.
  39. =cut
  40. sub mksession ($user,$pass) {
  41. my $dbh = _dbh();
  42. my $records = $dbh->selectall_arrayref("SELECT salt FROM user WHERE name = ?", { Slice => {} }, $user);
  43. return '' unless ref $records eq 'ARRAY' && @$records;
  44. my $salt = $records->[0]->{salt};
  45. my $hash = sha256($pass.$salt);
  46. my $worked = $dbh->selectall_arrayref("SELECT id FROM user WHERE hash=? AND name = ?", { Slice => {} }, $hash, $user);
  47. return '' unless ref $worked eq 'ARRAY' && @$worked;
  48. my $uid = $worked->[0]->{id};
  49. my $uuid = create_uuid_as_string(UUID_V1, UUID_NS_DNS);
  50. $dbh->do("INSERT OR REPLACE INTO session (id,user_id) VALUES (?,?)", undef, $uuid, $uid) or return '';
  51. return $uuid;
  52. }
  53. =head2 killsession(user) = BOOL
  54. Delete the provided user's session from the auth db.
  55. =cut
  56. sub killsession ($user) {
  57. my $dbh = _dbh();
  58. $dbh->do("DELETE FROM session WHERE user_id IN (SELECT id FROM user WHERE name=?)",undef,$user);
  59. return 1;
  60. }
  61. =head2 useradd(user, pass) = BOOL
  62. Adds a user identified by the provided password into the auth DB.
  63. Returns True or False (likely false when user already exists).
  64. =cut
  65. sub useradd ($user, $pass, $acls) {
  66. my $dbh = _dbh();
  67. my $salt = create_uuid();
  68. my $hash = sha256($pass.$salt);
  69. my $res = $dbh->do("INSERT OR REPLACE INTO user (name,salt,hash) VALUES (?,?,?)", undef, $user, $salt, $hash);
  70. return unless $res && ref $acls eq 'ARRAY';
  71. #XXX this is clearly not normalized with an ACL mapping table, will be an issue with large number of users
  72. foreach my $acl (@$acls) {
  73. return unless $dbh->do("INSERT OR REPLACE INTO user_acl (user_id,acl) VALUES ((SELECT id FROM user WHERE name=?),?)", undef, $user, $acl);
  74. }
  75. return 1;
  76. }
  77. # Ensure the db schema is OK, and give us a handle
  78. sub _dbh {
  79. my $file = 'schema/auth.schema';
  80. my $dbname = "$ENV{HOME}/.tcms/auth.db";
  81. return Trog::SQLite::dbh($file,$dbname);
  82. }
  83. 1;