weblogin.pm 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. use Digest::SHA qw(sha1_hex);
  2. {
  3. on_load => sub {
  4. my $heap = \%BotIrc::heap;
  5. $heap->{websessions} = {};
  6. my $ws = $heap->{websessions};
  7. $heap->{websessions_cleanup} = sub {
  8. for (keys %$ws) {
  9. next if time() - $ws->{$_}{last_used} < $BotIrc::config->{http_sessionpurge}
  10. }
  11. };
  12. },
  13. before_unload => sub {
  14. delete $BotIrc::heap{websessions};
  15. },
  16. control_commands => {
  17. login => sub {
  18. my ($client, $data, @args) = @_;
  19. my $heap = $BotIrc::heap{websessions};
  20. if (!exists $heap->{$args[0]}) {
  21. BotCtl::send($client, "invalid");
  22. return;
  23. }
  24. my $session = $heap->{$args[0]};
  25. if (time() - $session->{last_used} > $BotIrc::config->{http_sessionexpire}) {
  26. BotCtl::send($client, "expired");
  27. delete $heap->{$args[0]};
  28. return;
  29. }
  30. BotCtl::set_level($data, $session->{username});
  31. $data->{session_id} = $args[0];
  32. $session->{last_used} = time();
  33. BotCtl::send($client, "ok", $session->{username});
  34. },
  35. logout => sub {
  36. if (!exists $data->{session_id}) {
  37. BotCtl::send($client, "invalid");
  38. return;
  39. }
  40. delete $heap->{$data->{session_id}};
  41. delete $data->{session_id};
  42. BotCtl::set_level($data, "!guest");
  43. BotCtl::send($client, "ok");
  44. },
  45. },
  46. irc_commands => {
  47. weblogin => sub {
  48. my ($source, $targets, $args, $account) = @_;
  49. BotIrc::check_ctx(authed => 1, wisdom_public => 0) or return;
  50. # evil!
  51. my $auth = sha1_hex("$account:$$:".int(rand(1_000_000)).":".time());
  52. $BotIrc::heap{websessions}{$auth} = {
  53. username => lc($account),
  54. last_used => time()
  55. };
  56. BotIrc::send_wisdom("Please go to $BotIrc::config->{http_loginurl}$auth to log in (session cookies must be allowed).");
  57. },
  58. },
  59. };