| 1 | ########################################### | ||
| 2 | package OAuth::Cmdline::Mojo; | ||
| 3 | ########################################### | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Mojo::Base 'Mojolicious'; | ||
| 7 | |||
| 8 | ########################################### | ||
| 9 | sub startup { | ||
| 10 | ########################################### | ||
| 11 | my( $self ) = @_; | ||
| 12 | |||
| 13 | my $renderer = $self->renderer(); | ||
| 14 | push @{$renderer->paths}, $self->template_path(); | ||
| 15 | |||
| 16 | $self->routes->get('/')->to('main#root'); | ||
| 17 | $self->routes->get('/callback')->to('main#callback'); | ||
| 18 | } | ||
| 19 | |||
| 20 | ########################################### | ||
| 21 | sub template_path { | ||
| 22 | ########################################### | ||
| 23 | my( $self ) = @_; | ||
| 24 | |||
| 25 | # point renderer to where our .html.ep | ||
| 26 | # templates are installed | ||
| 27 | my $dir = $INC{ 'OAuth/Cmdline.pm' }; | ||
| 28 | $dir =~ s/\.pm//; | ||
| 29 | $dir .= "/templates"; | ||
| 30 | |||
| 31 | return $dir; | ||
| 32 | } | ||
| 33 | |||
| 34 | ########################################### | ||
| 35 | package OAuth::Cmdline::Mojo::Main; | ||
| 36 | ########################################### | ||
| 37 | use Mojo::Base 'Mojolicious::Controller'; | ||
| 38 | |||
| 39 | ########################################### | ||
| 40 | sub root { | ||
| 41 | ########################################### | ||
| 42 | my ( $self ) = @_; | ||
| 43 | |||
| 44 | $self->stash->{ login_url } = $self->app->{ oauth }->full_login_uri(); | ||
| 45 | $self->stash->{ site } = $self->app->{ oauth }->site(); | ||
| 46 | |||
| 47 | $self->render( "main" ); | ||
| 48 | } | ||
| 49 | |||
| 50 | ########################################### | ||
| 51 | sub callback { | ||
| 52 | ########################################### | ||
| 53 | my ( $self ) = @_; | ||
| 54 | |||
| 55 | my $code = $self->param( "code" ); | ||
| 56 | |||
| 57 | $self->app->{ oauth }->tokens_collect( $code ); | ||
| 58 | |||
| 59 | $self->render( | ||
| 60 | text => "Tokens saved in " . $self->app->{ oauth }->cache_file_path, | ||
| 61 | layout => 'default' ); | ||
| 62 | }; | ||
| 63 | |||
| 64 | 1; | ||
| 65 | |||
| 66 | __END__ | ||
| 67 | |||
| 68 | =head1 NAME | ||
| 69 | |||
| 70 | OAuth::Cmdline::Mojolicious - Run a standalone token collector | ||
| 71 | |||
| 72 | =head1 SYNOPSIS | ||
| 73 | |||
| 74 | use OAuth::Cmdline::Mojolicious; | ||
| 75 | app->start(); | ||
| 76 | |||
| 77 | =head1 DESCRIPTION | ||
| 78 | |||
| 79 | OAuth::Cmdline::Mojolicious starts a web server, to which you should | ||
| 80 | point your browser, in order to go through the OAuth rigamarole and | ||
| 81 | collect the tokens for later use in command line scripts. | ||
| 82 | |||
| 83 | =head1 LEGALESE | ||
| 84 | |||
| 85 | Copyright 2014 by Mike Schilli, all rights reserved. | ||
| 86 | This program is free software, you can redistribute it and/or | ||
| 87 | modify it under the same terms as Perl itself. | ||
| 88 | |||
| 89 | =head1 AUTHOR | ||
| 90 | |||
| 91 | 2014, Mike Schilli <cpan@perlmeister.com> |