Snippets

Brian Medley 7RxGE: Untitled snippet

Created by Brian Medley last modified
package SmokeMachine::Plugin::Access;

use Mojo::Base 'Mojolicious::Plugin';

sub access_slash {
    # Test if the header X-AUTH-KEY is a valid key
    # and add a User DBIC obj on $c->stash->{user}
}

sub access_api {
    # Test if the header X-API-KEY is a valid key
    # and add a Client DBIC obj on $c->stash->{client}
}

sub access_admin {
    # Test if the user has a admin role
}

sub register {
    my ($self, $app) = @_;
    
    $app->helper("access_slash" => \&access_slash);
    $app->helper("access_api" => \&access_api);
    $app->helper("access_admin" => \&access_admin);

    $app->helper("access" => sub {
        my ($c, @args) = @_;

        my $name = @args ? $args[0] : $c->current_route;

        my %access = (
            admin_get_user => "access_admin",
            get_obj => "access_slash",
        );

        my $method = $access->{$name};

        return $c->$method();
    });
}

1;
package SmokeMachine;

use Mojo::Base 'Mojolicious';

sub data {
    my $c = shift;
    my $route_name = shift;

    if ("get_obj") {
        return $c->stash->{user}->find_related($c->stash->{obj_id})->data;
    }
}

# This method will run once at server start
sub startup {
    my $self = shift;

    $self->plugin('SmokeMachine::Plugin::Access');
    
    # Router
    my $r = $self->routes;
    
    my $auth = $r->under (sub {
        my $c = shift;

        return $c->access($c);
    });

    $self->helper(data => \&data);

    ###
    $auth->get('/' => sub {
        my $c = shift;
    });

    $auth->get('/object/:obj_id' => sub {
        my $c = shift;
        
        $c->render(json => $c->data($c->current_route);
    } => "get_obj");

    ###
    $auth->get('/admin/user/:user_id' => sub {
        my $c = shift;

        $c->render(json => $c->data($c->current_route);
    } => "admin_get_user");

    ###
    $auth->get('/api/client_data' => sub {
        my $c = shift;

        $c->render(json => $c->data($c->current_route);
    } => "client_data");
    
    ###
    $r->get('/ping' => sub {
        shift()->render(json => "pong");
    } => "ping");

    ###
    $r->websocket('/ws' => sub{
            my $c = shift;

            $c->on(json => sub{
                    # example: {
                    #       "route":        "admin_get_user",
                    #       "headers":      {
                    #               "X-API-KEY":    "1234567890123456789012345678901234567890",
                    #               "X-AUTH-KEY":   "1234567890123456789012345678901234567890"
                    #       },
                    #       "stash":        {
                    #               "user_id":      42
                    #       }
                    #}
                    
                    my $tx  = shift;
                    my $msg = shift;

                    # PseudoCode
                    eval {
                        die unless $c->access($msg->{route});
                    };
                    if ($@) {
                        warn("No access");
                    }
                    else {
                        my $hash = $c->data($msg->{route});
                    }
            });     
    });
}

1;

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.