File Coverage

lib/OAuthomatic/UserInteraction/ViaMicroWeb.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package OAuthomatic::UserInteraction::ViaMicroWeb;
2             # ABSTRACT: User is led using forms in the browser
3              
4              
5 1     1   743 use Moose;
  0            
  0            
6             use namespace::sweep;
7             use Proc::Background;
8             use Carp;
9              
10              
11             has 'micro_web' => (is=>'ro', isa=>'OAuthomatic::Internal::MicroWeb', required=>1,
12             handles => ['server', 'config']);
13              
14             sub prepare_to_work {
15             my $self = shift;
16             $self->micro_web->start_using;
17             return;
18             }
19              
20             sub cleanup_after_work {
21             my $self = shift;
22             $self->micro_web->finish_using;
23             return;
24             }
25              
26              
27             sub prompt_client_credentials {
28             my ($self) = @_;
29              
30             my $mweb = $self->micro_web;
31              
32             my $enter_url = $mweb->client_key_url;
33             $self->_open_in_browser($enter_url, <<"END");
34             Fill application tokens in the browser form.
35             This script will continue once they are submitted.
36             END
37             my $client_cred = $mweb->wait_for_client_cred();
38              
39             return $client_cred;
40             }
41              
42             sub visit_oauth_authorize_page {
43             my ($self, $url) = @_;
44              
45             my $site_name = $self->server->site_name;
46              
47             $self->_open_in_browser($url, <<"END");
48             Accept application authorization in the browser form (page from $site_name),
49             or just notification about succesfull authorization if you already authorized
50             in the past.
51             This script will continue once you authorize application.
52             END
53              
54             return;
55             }
56              
57             sub _open_in_browser {
58             my ($self, $url, $comment) = @_;
59              
60             my $browser = $self->config->browser;
61             unless($browser) {
62             print <<"END";
63             Open your browser on
64             $url
65             $comment
66             END
67             } else {
68             print <<"END";
69             Spawning browser ($browser) on
70             $url
71             (open this page manually if for some reason it is not displayed automatically).
72              
73             $comment
74             END
75             my $bgr = Proc::Background->new($browser, $url);
76             # The process sometimes will end immediately (page in existing browser), sometimes will
77             # continue. Let's just ignore it (and, therefore, ignore returned object)
78             undef $bgr;
79             }
80             return;
81             }
82              
83             with 'OAuthomatic::UserInteraction';
84              
85             1;
86              
87             __END__
88              
89             =pod
90              
91             =encoding UTF-8
92              
93             =head1 NAME
94              
95             OAuthomatic::UserInteraction::ViaMicroWeb - User is led using forms in the browser
96              
97             =head1 VERSION
98              
99             version 0.0201
100              
101             =head1 DESCRIPTION
102              
103             Simple (local) web forms will be used to ask user for client
104             credentials and provide him with instructions.
105              
106             =head1 PARAMETERS
107              
108             =head2 micro_web
109              
110             Embedded web server object. Here it will be used to show forms.
111              
112             =head1 METHODS
113              
114             =head2 prompt_client_credentials() => ClientCred(...)
115              
116             Asks the user to visit appropriate remote page and provide application
117             (or developer) keys. Here use web form as an aid.
118              
119             =head1 AUTHOR
120              
121             Marcin Kasperski <Marcin.Kasperski@mekk.waw.pl>
122              
123             =head1 COPYRIGHT AND LICENSE
124              
125             This software is copyright (c) 2015 by Marcin Kasperski.
126              
127             This is free software; you can redistribute it and/or modify it under
128             the same terms as the Perl 5 programming language system itself.
129              
130             =cut