File Coverage

blib/lib/Web/Solid/Auth/Listener.pm
Criterion Covered Total %
statement 15 51 29.4
branch 0 6 0.0
condition 0 9 0.0
subroutine 5 8 62.5
pod 0 2 0.0
total 20 76 26.3


line stmt bran cond sub pod time code
1             package Web::Solid::Auth::Listener;
2              
3 2     2   105119 use Moo;
  2         15291  
  2         17  
4 2     2   3435 use Log::Any ();
  2         13348  
  2         40  
5 2     2   2023 use Plack::Request;
  2         99352  
  2         71  
6 2     2   1126 use Plack::Response;
  2         3095  
  2         82  
7 2     2   1661 use HTTP::Server::PSGI;
  2         46833  
  2         1280  
8              
9             our $VERSION = "0.9";
10              
11             has host => (
12             is => 'ro' ,
13             default => sub { 'localhost' }
14             );
15             has port => (
16             is => 'ro' ,
17             default => sub { '3000' }
18             );
19             has scheme => (
20             is => 'ro' ,
21             default => sub { 'http' }
22             );
23             has path => (
24             is => 'ro',
25             default => sub { '/callback' }
26             );
27             has log => (
28             is => 'ro',
29             default => sub { Log::Any->get_logger },
30             );
31              
32             sub redirect_uri {
33 0     0 0   my $self = shift;
34              
35 0           return sprintf "%s://%s:%s%s"
36             , $self->scheme
37             , $self->host
38             , $self->port
39             , $self->path;
40             }
41              
42             sub run {
43 0     0 0   my ($self,$auth) = @_;
44 0   0       $auth //= $self->auth;
45              
46 0           my $host = $self->host;
47 0           my $port = $self->port;
48 0           my $path = $self->path;
49              
50 0           $self->log->info("starting callback server on $host:$port$path");
51              
52 0           my $server = HTTP::Server::PSGI->new(
53             host => $host,
54             port => $port,
55             timeout => 120,
56             );
57              
58             $server->run(
59             sub {
60 0     0     my $env = shift;
61              
62 0           my $req = Plack::Request->new($env);
63 0           my $param = $req->parameters;
64 0           my $state = $auth->{state};
65              
66 0           $self->log->debugf("received: %s (%s) -> %s", $req->method, $req->path, $req->query_string);
67              
68             # Check if we got the correct path
69 0 0         unless ($req->path eq $path) {
70 0           my $res = Plack::Response->new(404);
71 0           $res->content_type("text/plain");
72 0           $res->body("No such path");
73 0           return $res->finalize;
74             }
75              
76             # Check if we got the correct state
77 0 0 0       unless ($req->method eq 'GET' && $param->{code} && $param->{state} eq $state ) {
      0        
78 0           my $res = Plack::Response->new(404);
79 0           $res->content_type("text/plain");
80 0           $res->body("Failed to get an access_token");
81 0           return $res->finalize;
82             }
83              
84 0           my $data = $auth->make_access_token($param->{code});
85              
86 0 0         if ($data) {
87 0           print "Ok stored you can close this program\n";
88 0           my $res = Plack::Response->new(200);
89 0           $res->content_type("text/plain");
90 0           $res->body("You are logged in :)");
91 0           return $res->finalize;
92             }
93             else {
94 0           my $res = Plack::Response->new(404);
95 0           $res->content_type("text/plain");
96 0           $res->body("Failed to get an access_token");
97 0           return $res->finalize;
98             }
99             }
100 0           );
101             }
102              
103             1;