Auth.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. package Trog::Auth;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures state};
  6. use FindBin::libs;
  7. use Ref::Util qw{is_arrayref};
  8. use Digest::SHA 'sha256';
  9. use Trog::TOTP;
  10. use Imager::QRCode;
  11. use Trog::Utils;
  12. use Trog::Log qw{:all};
  13. use Trog::Config;
  14. use Trog::SQLite;
  15. use Trog::Data;
  16. =head1 Trog::Auth
  17. An SQLite3 authdb.
  18. =head1 Termination Conditions
  19. Throws exceptions in the event the session database cannot be accessed.
  20. =head1 FUNCTIONS
  21. =head2 session2user(STRING sessid) = STRING
  22. Translate a session UUID into a username.
  23. Returns empty string on no active session.
  24. =cut
  25. sub session2user ($sessid) {
  26. my $dbh = _dbh();
  27. my $rows = $dbh->selectall_arrayref( "SELECT name FROM sess_user WHERE session=?", { Slice => {} }, $sessid );
  28. return '' unless ref $rows eq 'ARRAY' && @$rows;
  29. return $rows->[0]->{name};
  30. }
  31. =head2 user_has_session
  32. Return whether the user has an active session.
  33. If the user has an active session, things like password reset requests should fail when not coming from said session.
  34. =cut
  35. sub user_has_session ($user) {
  36. my $dbh = _dbh();
  37. my $rows = $dbh->selectall_arrayref( "SELECT session FROM sess_user WHERE name=?", { Slice => {} }, $user );
  38. return 0 unless ref $rows eq 'ARRAY' && @$rows;
  39. return 1;
  40. }
  41. =head2 user_exists
  42. Return whether the user exists at all.
  43. =cut
  44. sub user_exists ($user) {
  45. my $dbh = _dbh();
  46. my $rows = $dbh->selectall_arrayref( "SELECT name FROM user WHERE name=?", { Slice => {} }, $user );
  47. return 0 unless ref $rows eq 'ARRAY' && @$rows;
  48. return 1;
  49. }
  50. =head2 primary_user
  51. Returns the oldest user with the admin ACL.
  52. =cut
  53. sub primary_user {
  54. my $dbh = _dbh();
  55. my $rows = $dbh->selectall_arrayref( "SELECT username FROM user_acl WHERE acl='admin' LIMIT 1", { Slice => {} } );
  56. return 0 unless ref $rows eq 'ARRAY' && @$rows;
  57. return $rows->[0]{username};
  58. }
  59. =head2 get_existing_user_data
  60. Fetch existing settings for a user.
  61. =cut
  62. sub get_existing_user_data ($user) {
  63. my $dbh = _dbh();
  64. my $rows = $dbh->selectall_arrayref( "SELECT hash, salt, totp_secret, display_name, contact_email FROM user WHERE name=?", { Slice => {} }, $user );
  65. return ( undef, undef, undef ) unless ref $rows eq 'ARRAY' && @$rows;
  66. return ( $rows->[0]{hash}, $rows->[0]{salt}, $rows->[0]{totp_secret}, $rows->[0]{display_name}, $rows->[0]{contact_email} );
  67. }
  68. =head2 email4user(STRING username) = STRING
  69. Return the associated contact email for the user.
  70. =cut
  71. sub email4user ($user) {
  72. my $dbh = _dbh();
  73. my $rows = $dbh->selectall_arrayref( "SELECT contact_email FROM user WHERE name=?", { Slice => {} }, $user );
  74. return '' unless ref $rows eq 'ARRAY' && @$rows;
  75. return $rows->[0]{contact_email};
  76. }
  77. sub display2username ($display_name) {
  78. my $dbh = _dbh();
  79. my $rows = $dbh->selectall_arrayref( "SELECT name FROM user WHERE display_name=?", { Slice => {} }, $display_name );
  80. return '' unless ref $rows eq 'ARRAY' && @$rows;
  81. return $rows->[0]{name};
  82. }
  83. sub username2display ($name) {
  84. my $dbh = _dbh();
  85. my $rows = $dbh->selectall_arrayref( "SELECT display_name FROM user WHERE name=?", { Slice => {} }, $name );
  86. return '' unless ref $rows eq 'ARRAY' && @$rows;
  87. return $rows->[0]{display_name};
  88. }
  89. sub username2classname ($name) {
  90. # Just return the user's post UUID.
  91. state $data;
  92. state $conf;
  93. $conf //= Trog::Config::get();
  94. $data //= Trog::Data->new($conf);
  95. state @userposts = $data->get( tags => ['about'], acls => [qw{admin}] );
  96. # Users are always self-authored, you see
  97. my $user_obj = List::Util::first { ( $_->{user} || '' ) eq $name } @userposts;
  98. my $NNname = $user_obj->{id} || '';
  99. $NNname =~ tr/-/_/;
  100. return "a_$NNname";
  101. }
  102. =head2 acls4user(STRING username) = ARRAYREF
  103. Return the list of ACLs belonging to the user.
  104. The function of ACLs are to allow you to access content tagged 'private' which are also tagged with the ACL name.
  105. The 'admin' ACL is the only special one, as it allows for authoring posts, configuring tCMS, adding series (ACLs) and more.
  106. =cut
  107. sub acls4user ($username) {
  108. my $dbh = _dbh();
  109. my $records = $dbh->selectall_arrayref( "SELECT acl FROM user_acl WHERE username = ?", { Slice => {} }, $username );
  110. return () unless ref $records eq 'ARRAY' && @$records;
  111. my @acls = map { $_->{acl} } @$records;
  112. return \@acls;
  113. }
  114. =head2 totp(user, domain)
  115. Enable TOTP 2fa for the specified user, or if already enabled return the existing info.
  116. Returns a QR code and URI for pasting into authenticator apps.
  117. =cut
  118. sub totp ( $user, $domain ) {
  119. my $totp = _totp();
  120. my $dbh = _dbh();
  121. my $failure = 0;
  122. my $message = "TOTP Secret generated successfully.";
  123. # Make sure we re-generate the same one in case the user forgot.
  124. my $secret;
  125. my $worked = $dbh->selectall_arrayref( "SELECT totp_secret FROM user WHERE name = ?", { Slice => {} }, $user );
  126. if ( ref $worked eq 'ARRAY' && @$worked ) {
  127. $secret = $worked->[0]{totp_secret};
  128. }
  129. $failure = -1 if $secret;
  130. # Generate a new secret if needed
  131. my $secret_is_generated = 0;
  132. if ( !$secret ) {
  133. $secret_is_generated = 1;
  134. $totp->_valid_secret();
  135. $secret = $totp->secret();
  136. }
  137. my $uri = $totp->generate_otp(
  138. user => "$user\@$domain",
  139. issuer => $domain,
  140. #XXX verifier apps will only do 30s :(
  141. period => 30,
  142. digits => 6,
  143. secret => $secret,
  144. );
  145. my $qr = "$user\@$domain.bmp";
  146. if ($secret_is_generated) {
  147. # Liquidate the QR code if it's already there
  148. unlink "totp/$qr" if -f "totp/$qr";
  149. $dbh->do( "UPDATE user SET totp_secret=? WHERE name=?", undef, $secret, $user ) or return ( undef, undef, 1, "Failed to store TOTP secret." );
  150. }
  151. # This is subsequently served via authenticated _serve() in TCMS.pm
  152. if ( !-f "totp/$qr" ) {
  153. my $qrcode = Imager::QRCode->new(
  154. size => 4,
  155. margin => 3,
  156. level => 'L',
  157. casesensitive => 1,
  158. lightcolor => Imager::Color->new( 255, 255, 255 ),
  159. darkcolor => Imager::Color->new( 0, 0, 0 ),
  160. );
  161. my $img = $qrcode->plot($uri);
  162. $img->write( file => "totp/$qr", type => "bmp" ) or return ( undef, undef, 1, "Could not write totp/$qr: " . $img->errstr );
  163. }
  164. return ( $uri, $qr, $failure, $message, $totp );
  165. }
  166. sub _totp {
  167. state $totp;
  168. if ( !$totp ) {
  169. $totp = Trog::TOTP->new();
  170. die "Cannot instantiate TOTP client!" unless $totp;
  171. $totp->{DEBUG} = 1 if is_debug();
  172. }
  173. return $totp;
  174. }
  175. =head2 clear_totp
  176. Clear the totp codes for provided user
  177. =cut
  178. sub clear_totp ($user) {
  179. my $dbh = _dbh();
  180. my $res = $dbh->do( "UPDATE user SET totp_secret=null WHERE name=?", undef, $user ) or die "Could not clear user TOTP secrets";
  181. return !!$res;
  182. }
  183. =head2 mksession(user, pass, token) = STRING
  184. Create a session for the user and waste all other sessions.
  185. Returns a session ID, or blank string in the event the user does not exist or incorrect auth was passed.
  186. =cut
  187. sub mksession ( $user, $pass, $token ) {
  188. my $dbh = _dbh();
  189. my $totp = _totp();
  190. # Check the password
  191. my $records = $dbh->selectall_arrayref( "SELECT salt FROM user WHERE name = ?", { Slice => {} }, $user );
  192. return '' unless ref $records eq 'ARRAY' && @$records;
  193. my $salt = $records->[0]->{salt};
  194. my $hash = sha256( $pass . $salt );
  195. my $worked = $dbh->selectall_arrayref( "SELECT name, totp_secret FROM user WHERE hash=? AND name = ?", { Slice => {} }, $hash, $user );
  196. if ( !( ref $worked eq 'ARRAY' && @$worked ) ) {
  197. INFO("Failed login for user $user");
  198. return '';
  199. }
  200. my $uid = $worked->[0]{name};
  201. my $secret = $worked->[0]{totp_secret};
  202. # Validate the 2FA Token. If we have no secret, allow login so they can see their QR code, and subsequently re-auth.
  203. if ($secret) {
  204. return '' unless $token;
  205. DEBUG( "TOTP Auth: Sent code $token, expect " . $totp->expected_totp_code(time) );
  206. #XXX we have to force the secret into compliance, otherwise it generates one on the fly, oof
  207. $totp->{secret} = $secret;
  208. my $rc = $totp->validate_otp( otp => $token, secret => $secret, tolerance => 3, period => 30, digits => 6 );
  209. INFO("TOTP Auth failed for user $user") unless $rc;
  210. return '' unless $rc;
  211. }
  212. # Issue cookie
  213. my $uuid = Trog::Utils::uuid();
  214. $dbh->do( "INSERT OR REPLACE INTO session (id,username) VALUES (?,?)", undef, $uuid, $uid ) or return '';
  215. return $uuid;
  216. }
  217. =head2 killsession(user) = BOOL
  218. Delete the provided user's session from the auth db.
  219. =cut
  220. sub killsession ($user) {
  221. my $dbh = _dbh();
  222. $dbh->do( "DELETE FROM session WHERE username=?", undef, $user );
  223. return 1;
  224. }
  225. =head2 useradd(user, displayname, pass, acls, contactemail) = BOOL
  226. Adds a user identified by the provided password into the auth DB.
  227. Also used to alter users.
  228. Returns True or False (likely false when user already exists).
  229. =cut
  230. sub useradd ( $user, $displayname, $pass, $acls, $contactemail ) {
  231. # See if the user exists already, keep pw if nothing's passed
  232. my ( $hash, $salt, $t_secret, $dn, $ce ) = get_existing_user_data($user);
  233. $displayname //= $dn;
  234. $contactemail //= $ce;
  235. die "No username set!" unless $user;
  236. die "No display name set!" unless $displayname;
  237. die "Username and display name cannot be the same" if $user eq $displayname;
  238. die "No password set for user!" if !$pass && !$hash;
  239. die "ACLs must be array" unless is_arrayref($acls);
  240. die "No contact email set for user!" unless $contactemail;
  241. my $dbh = _dbh();
  242. if ($pass) {
  243. $salt = Trog::Utils::uuid();
  244. $hash = sha256( $pass . $salt );
  245. }
  246. my $res = $dbh->do( "INSERT OR REPLACE INTO user (name, display_name, salt, hash, contact_email, totp_secret) VALUES (?,?,?,?,?,?)", undef, $user, $displayname, $salt, $hash, $contactemail, $t_secret );
  247. return unless $res && ref $acls eq 'ARRAY';
  248. #XXX this is clearly not normalized with an ACL mapping table, will be an issue with large number of users
  249. foreach my $acl (@$acls) {
  250. return unless $dbh->do( "INSERT OR REPLACE INTO user_acl (username,acl) VALUES (?,?)", undef, $user, $acl );
  251. }
  252. return 1;
  253. }
  254. sub add_change_request (%args) {
  255. my $dbh = _dbh();
  256. my $res = $dbh->do( "INSERT INTO change_request (username,token,type,secret) VALUES (?,?,?,?)", undef, $args{user}, $args{token}, $args{type}, $args{secret} );
  257. return !!$res;
  258. }
  259. sub process_change_request ($token) {
  260. my $dbh = _dbh();
  261. my $rows = $dbh->selectall_arrayref( "SELECT username, display_name, type, secret, contact_email FROM change_request_full WHERE processed=0 AND token=?", { Slice => {} }, $token );
  262. return 0 unless ref $rows eq 'ARRAY' && @$rows;
  263. my $user = $rows->[0]{username};
  264. my $display = $rows->[0]{display_name};
  265. my $type = $rows->[0]{type};
  266. my $secret = $rows->[0]{secret};
  267. my $contactemail = $rows->[0]{contact_email};
  268. state %dispatch = (
  269. reset_pass => sub {
  270. my ( $user, $pass ) = @_;
  271. #XXX The fact that this is an INSERT OR REPLACE means all the entries in change_request for this user will get cascade wiped. Which is good, as the secrets aren't salted.
  272. # This is also why we have to snag the user's ACLs or they will be wiped.
  273. my @acls = acls4user($user);
  274. useradd( $user, $display, $pass, \@acls, $contactemail ) or do {
  275. return '';
  276. };
  277. killsession($user);
  278. return "Password set to $pass for $user";
  279. },
  280. clear_totp => sub {
  281. my ($user) = @_;
  282. clear_totp($user) or do {
  283. return '';
  284. };
  285. killsession($user);
  286. return "TOTP auth turned off for $user";
  287. },
  288. );
  289. my $res = $dispatch{$type}->( $user, $secret );
  290. $dbh->do( "UPDATE change_request SET processed=1 WHERE token=?", undef, $token ) or do {
  291. FATAL("Could not set job with token $token to completed!");
  292. };
  293. return $res;
  294. }
  295. # Ensure the db schema is OK, and give us a handle
  296. sub _dbh {
  297. my $file = 'schema/auth.schema';
  298. my $dbname = "config/auth.db";
  299. return Trog::SQLite::dbh( $file, $dbname );
  300. }
  301. 1;