[dancer-users] REST plugin

Rick Leir richard.leir at canadiana.ca
Tue Oct 13 15:10:56 BST 2015


On Tue, Oct 13, 2015 at 7:00 AM, <dancer-users-request at dancer.pm> wrote:

> I see Dancer2::Plugin::REST, is that the recommended one? It looks good?
> any
>   experiences with it?
>

My code serves REST without the use of that plugin.  It was good, but it
did not seem to help me much.

I have been writing a simple decorator plugin to help with JWT JSON Web
Tokens. So my route can look like this:

check_auth ['post', 'put'] => '/cr_user' => sub {
    my ($jwt_sub) = @_;
    my $dir = _connect_dir();

    my $role_valid ;
    my $ret;
    if( $jwt_sub) {
        my $admin_user = $dir->get_user({ username => $jwt_sub });

The plugin has this:

use Crypt::JWT qw(encode_jwt decode_jwt);
...
register 'check_auth' => sub {
    my ( $dsl, $pattern, @rest ) = @_;

    my $default_methods = [ 'get', 'post' ];

    # If the given pattern is an ArrayRef, we override the defaults
    # and pass these onto to DSL->any()
    if( ref($pattern) eq "ARRAY" ) {
        $default_methods = $pattern;
        $pattern = shift(@rest);
    }
    my $code;
    for my $e (@rest) { $code = $e if ( ref($e) eq 'CODE' ) }

    my $content_type = plugin_setting->{content_type} || 'text/xml';

    my $authenticated_route = sub {
        # Default response content type
        $dsl->response->header('Content-Type')
          or $dsl->response->content_type( $content_type );
        # must be authenticated
        # get token from header
        my $jwt_sub;
        my $token = $dsl->request->header('authorization');
        if ( $token) {
          # check the JWT of this request
          my $secret  = plugin_setting->{'jwt_secret'};
          try{
            my $data = decode_jwt(token=>$token, key=>$secret);

Much of this is copied from Dancer2::Plugin::Auth::Tiny by D.A. Golden. Now
I should try Yanick's Plugin2.

Cheers -- Rick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20151013/fe2333e5/attachment.html>


More information about the dancer-users mailing list