« blib/lib/auto/share/dist/Beekeeper/chat/lib/MyApp/Service/Chat.pm
↑ Summary
blib/lib/auto/share/dist/Beekeeper/chat/lib/MyApp/Service/Base.pm »
| 1 | package MyApp::Service::Auth::Worker; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use MyApp::Service::Base; | ||
| 7 | use base 'MyApp::Service::Base'; | ||
| 8 | |||
| 9 | use Beekeeper::Worker::Extension::RemoteSession; | ||
| 10 | use MyApp::Service::Chat; | ||
| 11 | |||
| 12 | |||
| 13 | sub authorize_request { | ||
| 14 | my ($self, $req) = @_; | ||
| 15 | |||
| 16 | # Make an exception over the MyApp::Service::Base rule of requiring a logged user | ||
| 17 | return BKPR_REQUEST_AUTHORIZED if $req->{method} eq 'myapp.auth.login'; | ||
| 18 | |||
| 19 | return $self->SUPER::authorize_request($req); | ||
| 20 | } | ||
| 21 | |||
| 22 | sub on_startup { | ||
| 23 | my $self = shift; | ||
| 24 | |||
| 25 | $self->init_persistent_connections; | ||
| 26 | |||
| 27 | $self->accept_remote_calls( | ||
| 28 | 'myapp.auth.login' => 'login', | ||
| 29 | 'myapp.auth.logout' => 'logout', | ||
| 30 | 'myapp.auth.kick' => 'kick', | ||
| 31 | ); | ||
| 32 | |||
| 33 | log_info "Ready"; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub on_shutdown { | ||
| 37 | my $self = shift; | ||
| 38 | |||
| 39 | log_info "Stopped"; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub login { | ||
| 43 | my ($self, $params) = @_; | ||
| 44 | |||
| 45 | my $username = $params->{username} || die "No username"; | ||
| 46 | my $password = $params->{password}; | ||
| 47 | |||
| 48 | # For simplicity, this example avoids resolving username <--> uuid | ||
| 49 | # mapping, and username and password are not verified at all | ||
| 50 | my $uuid = $username; | ||
| 51 | |||
| 52 | # The authentication data will be present on all subsequent requests | ||
| 53 | $self->set_authentication_data( $uuid ); | ||
| 54 | |||
| 55 | # Make the authentication data persist and assign an arbitrary address to the user | ||
| 56 | $self->bind_remote_session( address => "frontend.user-$uuid" ); | ||
| 57 | |||
| 58 | MyApp::Service::Chat->send_notice( | ||
| 59 | to_uuid => $uuid, | ||
| 60 | message => "Welcome $username", | ||
| 61 | ); | ||
| 62 | |||
| 63 | return 1; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub logout { | ||
| 67 | my ($self, $params) = @_; | ||
| 68 | |||
| 69 | my $uuid = $self->get_authentication_data; | ||
| 70 | |||
| 71 | MyApp::Service::Chat->send_notice( | ||
| 72 | to_uuid => $uuid, | ||
| 73 | message => "Bye!", | ||
| 74 | ); | ||
| 75 | |||
| 76 | $self->unbind_remote_session; | ||
| 77 | |||
| 78 | return 1; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub kick { | ||
| 82 | my ($self, $params) = @_; | ||
| 83 | |||
| 84 | # For simplicity, this example avoids resolving username <--> uuid | ||
| 85 | my $kick_uuid = $params->{'username'}; | ||
| 86 | |||
| 87 | MyApp::Service::Chat->send_notice( | ||
| 88 | to_uuid => $kick_uuid, | ||
| 89 | message => "Sorry, you were kicked", | ||
| 90 | ); | ||
| 91 | |||
| 92 | $self->unbind_remote_address( address => "frontend.user-$kick_uuid" ); | ||
| 93 | |||
| 94 | return 1; | ||
| 95 | } | ||
| 96 | |||
| 97 | 1; |