Plack/Middleware/DoormanAuth0.pm untested

filter: Statementstmt 0.0% 0 / 97 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 181 CC 10 SCAR 21.4
CC
10
Cov
0%
call
30.0
_exchange_auth0_code_for_token
12.0
_fetch_auth0_profile
12.0
CRAP 8.5
1 package Plack::Middleware::DoormanAuth0;
2 use 5.010;
3 use parent 'Doorman::PlackMiddleware';
4 use strict;
5 use LWP::UserAgent;
6 use JSON qw/decode_json/;
7 use namespace::autoclean;
8  
9 our $VERSION = "0.01";
10 our $AUTHORITY = "https://recollect.net";
11  
12 use feature qw(say);
13 use Plack::Request;
14 use URI;
15  
16 sub auth0_client_id { shift->{auth0_client_id} }
17 sub auth0_client_secret { shift->{auth0_client_secret} }
18 sub auth0_domain { shift->{auth0_domain} }
19 sub auth0_redirect_uri {
20 my $self = shift;
21 return $self->{root_url} . '/' . $self->scope . '/sign_in/auth0';
22 }
23  
24 sub session_hash {
25 my ($self) = @_;
26 my $session = $self->{env}->{'psgix.session'};
27 my $k = "doorman.@{[ $self->scope ]}.auth0";
28  
29 return unless $session && $session->{$k};
30 return $session->{$k};
31 }
32  
33 sub auth0_email {
34 my ($self) = @_;
35 my $sh = $self->session_hash or return;
36 return $sh->{"email"};
37 }
38  
39 sub auth0_user_id {
40 my ($self) = @_;
41 my $sh = $self->session_hash or return;
42 return $sh->{"user_id"};
43 }
44  
45 sub is_sign_in {
46 my ($self) = @_;
47 return defined $self->auth0_email;
48 }
49  
50 sub sign_out_path {
51 my ($self) = @_;
52 my $path = $self->SUPER::sign_out_path();
53 return $path .= '/auth0';
54 }
55  
56 sub call {
57 my ($self, $env) = @_;
58  
59 $self->prepare_call($env);
60  
61 $env->{"doorman.@{[ $self->scope ]}.auth0"} = $self;
62  
63 my $request = Plack::Request->new($env);
64 my $session = $env->{'psgix.session'} or die "Session is required for Auth0.";
65 if ($request->method eq 'GET') {
66 if ($request->path eq $self->sign_in_path . '/auth0') {
67 my $code = $request->parameters->{code};
68 $self->process_sign_in($session, $code);
69 }
70 elsif ($request->path eq $self->sign_out_path) {
71 if ($session) {
72 delete $session->{"doorman.@{[$self->scope]}.auth0"};
73 }
74 }
75 }
76  
77 return $self->app->($env);
78 }
79  
80 sub process_sign_in {
81 my $self = shift;
82 my $session = shift;
83 my $code = shift or return;
84 my $access_token = $self->_exchange_auth0_code_for_token($code) or return;
85 my $profile = $self->_fetch_auth0_profile($access_token) or return;
86  
87 $session->{"doorman.@{[ $self->scope ]}.auth0"} = {
88 (map { $_ => $profile->{$_} }
89 qw/user_id email identities
90 name given_name family_name nickname picture/),
91 };
92 }
93  
94 sub _exchange_auth0_code_for_token {
95 my $self = shift;
96 my $code = shift;
97 my $url = 'https://' . $self->auth0_domain . '/oauth/token';
98 my $resp = $self->_ua->post( $url,
99 'Content-type' => 'application/x-www-form-urlencoded',
100 Content => {
101 client_id => $self->auth0_client_id,
102 redirect_uri => $self->auth0_redirect_uri,
103 client_secret => $self->auth0_client_secret,
104 code => $code,
105 grant_type => 'authorization_code',
106 },
107 );
108  
109 if ($resp->is_success) {
110 my $data = eval { decode_json($resp->content) } || {};
111 if ($@) {
112 warn "Auth0: Got invalid JSON in Auth exchange: "
113 . "code=$code status_line=" . $resp->status_line
114 . " error=$@";
115 return undef;
116 }
117 return $data->{access_token};
118 }
119  
120 warn "Auth0: Failed request for access token"
121 . "code=$code status_line=" . $resp->status_line;
122 return undef;
123 }
124  
125 sub _fetch_auth0_profile {
126 my $self = shift;
127 my $access_token = shift;
128 my $resp = $self->_ua->get(
129 'https://' . $self->auth0_domain . '/userinfo',
130 Authorization => "Bearer $access_token",
131 );
132 if ($resp->is_success) {
133 my $data = eval { decode_json($resp->content) } || {};
134 if ($@) {
135 warn "Auth0: Got invalid JSON in /userinfo: "
136 . "token=$access_token response="
137 . $resp->status_line . " error=$@";
138 return undef;
139 }
140 return $data;
141 }
142 warn "Auth0: Failed request for /userinfo: "
143 . "token=$access_token response=" . $resp->status_line;
144 return undef;
145 }
146  
147 sub _ua { LWP::UserAgent->new(agent => "DoormanAuth0/$VERSION") }
148  
149 1;
150  
151 __END__