| 1 | package Web::Solid::Auth; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use Crypt::JWT; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use Data::UUID; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | use Digest::SHA; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 1 | ▶ | use HTTP::Link; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use HTTP::Request; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 1 | ▶ | use HTTP::Server::PSGI; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 1 | ▶ | use Log::Any (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 1 | ▶ | use JSON; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 1 | ▶ | use MIME::Base64; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 1 | ▶ | use Path::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 1 | ▶ | use URI::Escape; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 1 | ▶ | use Plack::Request; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 1 | ▶ | use Plack::Response; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 1 | ▶ | use Web::Solid::Auth::Listener; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 1 | ▶ | use Web::Solid::Auth::Util; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 1 | our $VERSION = "0.91"; | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 1 | has webid => ( | |||||||||||||||||||
| 25 | is => 'ro' , | ||||||||||||||||||||
| 26 | required => 1 | ||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||
| 28 | 1 | has redirect_uri => ( | |||||||||||||||||||
| 29 | is => 'ro' | ||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||
| 31 | has cache => ( | ||||||||||||||||||||
| 32 | is => 'ro' , | ||||||||||||||||||||
| 33 | 0 | ▶ | default => sub { $ENV{HOME} . "/.solid"} | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 34 | 1 | ); | |||||||||||||||||||
| 35 | has log => ( | ||||||||||||||||||||
| 36 | is => 'ro', | ||||||||||||||||||||
| 37 | 0 | ▶ | default => sub { Log::Any->get_logger }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 38 | 1 | ); | |||||||||||||||||||
| 39 | 1 | has agent => ( | |||||||||||||||||||
| 40 | is => 'lazy' | ||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||
| 42 | 1 | has listener => ( | |||||||||||||||||||
| 43 | is => 'lazy' | ||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||
| 45 | 1 | has issuer => ( | |||||||||||||||||||
| 46 | is => 'lazy' | ||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||
| 48 | 1 | has client_id => ( | |||||||||||||||||||
| 49 | is => 'ro', | ||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 0 | ▶ | sub _build_agent { | ||||||||||||||||||
|
Subroutine
_build_agent: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 53 | 0 | my $ua = LWP::UserAgent->new(agent => "Web::Solid::Auth/$VERSION"); | |||||||||||||||||||
| 54 | 0 | $ua; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 0 | ▶ | sub _build_listener { | ||||||||||||||||||
|
Subroutine
_build_listener: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 58 | 0 | Web::Solid::Auth::Listener->new; | |||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 0 | ▶ | sub _build_issuer { | ||||||||||||||||||
|
Subroutine
_build_issuer: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 62 | 0 | shift->get_openid_provider(); | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 0 | ▶ | sub BUILD { | ||||||||||||||||||
|
Subroutine
BUILD: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 66 | 0 | my $self = shift; | |||||||||||||||||||
| 67 | 0 | ▶ | $self->{redirect_uri} //= $self->listener->redirect_uri; | ||||||||||||||||||
|
Condition0%0/3
$self->{"redirect_uri"} //= $self->listener->redirect_uri
MC/DC0%0/2
$self->{"redirect_uri"} //= $self->listener->redirect_uri
$self->{"redirect_uri"}
$self->listener->redirect_uri
Truth table0%0/3
A //= B
A= $self->{"redirect_uri"}B= $self->listener->redirect_uri
| |||||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 0 | ▶ | sub listen { | ||||||||||||||||||
|
Subroutine
listen: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 71 | 0 | my $self = shift; | |||||||||||||||||||
| 72 | 0 | $self->listener->run($self); | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 0 | ▶ | sub has_access_token { | ||||||||||||||||||
|
Subroutine
has_access_token: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 76 | 0 | my $self = shift; | |||||||||||||||||||
| 77 | 0 | my $cache_dir = $self->get_cache_dir; | |||||||||||||||||||
| 78 | 0 | my $access = path($cache_dir)->child("access.json"); | |||||||||||||||||||
| 79 | 0 | $access->exists; | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 0 | ▶ | sub make_clean { | ||||||||||||||||||
|
Subroutine
make_clean: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 83 | 0 | my $self = shift; | |||||||||||||||||||
| 84 | 0 | my $cache_dir = $self->get_cache_dir; | |||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 0 | $self->log->info("cleaning cache directory $cache_dir"); | |||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 0 | my $openid = path($cache_dir)->child("openid.json"); | |||||||||||||||||||
| 89 | 0 | ▶ | $openid->remove if $openid->exists; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 0 | my $client = path($cache_dir)->child("client.json"); | |||||||||||||||||||
| 92 | 0 | ▶ | $client->remove if $client->exists; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 0 | my $access = path($cache_dir)->child("access.json"); | |||||||||||||||||||
| 95 | 0 | ▶ | $access->remove if $access->exists; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 0 | $self; | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 0 | ▶ | sub make_authorization_request { | ||||||||||||||||||
|
Subroutine
make_authorization_request: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 101 | 0 | my $self = shift; | |||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 0 | my $redirect_uri = $self->redirect_uri; | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 0 | my $registration_conf = $self->get_client_configuration; | |||||||||||||||||||
| 106 | 0 | my $openid_conf = $self->get_openid_configuration; | |||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 0 | my $authorization_endpoint = $openid_conf->{authorization_endpoint}; | |||||||||||||||||||
| 109 | 0 | my $client_id = $registration_conf->{client_id}; | |||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 0 | my $code_verifier = $self->make_random_string; | |||||||||||||||||||
| 112 | 0 | my $code_challenge = MIME::Base64::encode_base64url(Digest::SHA::sha256($code_verifier),''); | |||||||||||||||||||
| 113 | 0 | $code_challenge =~ s{=}{}; | |||||||||||||||||||
| 114 | 0 | my $state = $self->make_random_string; | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 0 | my $url = $self->make_url( | |||||||||||||||||||
| 117 | $authorization_endpoint, { | ||||||||||||||||||||
| 118 | code_challenge => $code_challenge , | ||||||||||||||||||||
| 119 | code_challenge_method => 'S256' , | ||||||||||||||||||||
| 120 | state => $state , | ||||||||||||||||||||
| 121 | scope => 'openid profile offline_access' , | ||||||||||||||||||||
| 122 | client_id => $client_id , | ||||||||||||||||||||
| 123 | response_type => 'code' , | ||||||||||||||||||||
| 124 | redirect_uri => $redirect_uri , | ||||||||||||||||||||
| 125 | }); | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 0 | $self->{state} = $state; | |||||||||||||||||||
| 128 | 0 | $self->{code_verifier} = $code_verifier; | |||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 0 | $self->log->info("generating authorization request: $url"); | |||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 0 | return $url; | |||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 0 | ▶ | sub make_access_token { | ||||||||||||||||||
|
Subroutine
make_access_token: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||
| 136 | 0 | my ($self,$code) = @_; | |||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 0 | ▶ | die "need code" unless $code; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 0 | my $redirect_uri = $self->redirect_uri; | |||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 0 | my $openid_conf = $self->get_openid_configuration; | |||||||||||||||||||
| 143 | 0 | my $registration_conf = $self->get_client_configuration; | |||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 0 | my $token_endpoint = $openid_conf->{token_endpoint}; | |||||||||||||||||||
| 146 | 0 | ▶ | my $token_endpoint_auth_methods_supported = $openid_conf->{token_endpoint_auth_methods_supported} // []; | ||||||||||||||||||
|
Condition0%0/2
$openid_conf->{"token_endpoint_auth_methods_supported"} // []
MC/DC0%0/1
$openid_conf->{"token_endpoint_auth_methods_supported"} // []
$openid_conf->{"token_endpoint_auth_methods_supported"}
Truth table0%0/2
A
A= $openid_conf->{"token_endpoint_auth_methods_supported"}
| |||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | # Make an array out of an string... | ||||||||||||||||||||
| 149 | 0 | ▶ | $token_endpoint_auth_methods_supported = | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 150 | ref($token_endpoint_auth_methods_supported) eq 'ARRAY' ? | ||||||||||||||||||||
| 151 | $token_endpoint_auth_methods_supported : | ||||||||||||||||||||
| 152 | [$token_endpoint_auth_methods_supported]; | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 0 | my $client_id = $registration_conf->{client_id}; | |||||||||||||||||||
| 155 | 0 | my $client_secret = $registration_conf->{client_secret}; | |||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 0 | my $dpop_token = $self->make_token_for($token_endpoint,'POST'); | |||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | 0 | $self->log->info("requesting access token at $token_endpoint"); | |||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | my $token_request = { | ||||||||||||||||||||
| 162 | grant_type => 'authorization_code' , | ||||||||||||||||||||
| 163 | client_id => $client_id , | ||||||||||||||||||||
| 164 | redirect_uri => $redirect_uri , | ||||||||||||||||||||
| 165 | code => $code , | ||||||||||||||||||||
| 166 | code_verifier => $self->{code_verifier} | ||||||||||||||||||||
| 167 | 0 | }; | |||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | 0 | my %headers = ( | |||||||||||||||||||
| 170 | 'Content-Type' => 'application/x-www-form-urlencoded' , | ||||||||||||||||||||
| 171 | DPoP => $dpop_token | ||||||||||||||||||||
| 172 | ); | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 0 | ▶ | if (grep(/^client_secret_basic/, @$token_endpoint_auth_methods_supported)) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 175 | 0 | $self->log->info('using client_secret_basic'); | |||||||||||||||||||
| 176 | 0 | $headers{'Authorization'} = 'Basic ' . MIME::Base64::encode_base64url("$client_id:$client_secret"); | |||||||||||||||||||
| 177 | } | ||||||||||||||||||||
| 178 | elsif (grep(/^client_secret_post/, @$token_endpoint_auth_methods_supported)) { | ||||||||||||||||||||
| 179 | 0 | $self->log->info('using client_secret_post'); | |||||||||||||||||||
| 180 | 0 | $token_request->{client_secret} = $client_secret; | |||||||||||||||||||
| 181 | } | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | 0 | my $data = $self->post( $token_endpoint, $token_request , %headers ); | |||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | 0 | ▶ | return undef unless $data; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | 0 | $data = decode_json($data); | |||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | 0 | $self->log->infof("received: %s", $data); | |||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | 0 | my $cache_dir = $self->get_cache_dir; | |||||||||||||||||||
| 192 | 0 | ▶ | path($cache_dir)->mkpath unless -d $cache_dir; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | 0 | my $cache_file = path($cache_dir)->child("access.json")->stringify; | |||||||||||||||||||
| 195 | 0 | path($cache_file)->spew(encode_json($data)); | |||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | 0 | return $data; | |||||||||||||||||||
| 198 | } | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | 0 | ▶ | sub make_authentication_headers { | ||||||||||||||||||
|
Subroutine
make_authentication_headers: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 201 | 0 | my ($self, $uri, $method) = @_; | |||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | 0 | my $access = $self->get_access_token; | |||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | 0 | ▶ | return undef unless $access; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | my $headers = { | ||||||||||||||||||||
| 208 | Authorization => 'DPoP ' . $access->{access_token} , | ||||||||||||||||||||
| 209 | 0 | DPoP => $self->make_token_for($uri,$method) | |||||||||||||||||||
| 210 | }; | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | 0 | return $headers; | |||||||||||||||||||
| 213 | } | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | 0 | ▶ | sub get_cache_dir { | ||||||||||||||||||
|
Subroutine
get_cache_dir: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 216 | 0 | my $self = shift; | |||||||||||||||||||
| 217 | 0 | my $webid = $self->webid; | |||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | 0 | ▶ | die "No webid set" unless $webid; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | 0 | my $webid_sha = Digest::SHA::sha1_hex($webid); | |||||||||||||||||||
| 222 | 0 | my $cache_dir = sprintf "%s/%s" | |||||||||||||||||||
| 223 | , $self->cache | ||||||||||||||||||||
| 224 | , Digest::SHA::sha1_hex($webid); | ||||||||||||||||||||
| 225 | 0 | return $cache_dir; | |||||||||||||||||||
| 226 | } | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | 0 | ▶ | sub get_access_token { | ||||||||||||||||||
|
Subroutine
get_access_token: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 229 | 0 | my $self = shift; | |||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | 0 | my $cache_dir = $self->get_cache_dir; | |||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | 0 | ▶ | return undef unless path($cache_dir)->child("access.json")->exists; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | 0 | my $cache_file = path($cache_dir)->child("access.json")->stringify; | |||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | 0 | $self->log->debug("reading $cache_file"); | |||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | 0 | my $json = path("$cache_file")->slurp; | |||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | 0 | ▶ | return undef unless $json; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 242 | |||||||||||||||||||||
| 243 | 0 | return decode_json($json); | |||||||||||||||||||
| 244 | } | ||||||||||||||||||||
| 245 | |||||||||||||||||||||
| 246 | 0 | ▶ | sub get_openid_provider { | ||||||||||||||||||
|
Subroutine
get_openid_provider: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||
| 247 | 0 | my ($self, $webid) = @_; | |||||||||||||||||||
| 248 | 0 | ▶ | $webid //= $self->webid; | ||||||||||||||||||
|
Condition0%0/3
$webid //= $self->webid
MC/DC0%0/2
$webid //= $self->webid
$webid
$self->webid
Truth table0%0/3
A //= B
A= $webidB= $self->webid
| |||||||||||||||||||||
| 249 | |||||||||||||||||||||
| 250 | 0 | my $res = $self->options($webid); | |||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | 0 | ▶ | return undef unless $res; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 253 | |||||||||||||||||||||
| 254 | 0 | my $link = $res->header('Link'); | |||||||||||||||||||
| 255 | |||||||||||||||||||||
| 256 | 0 | my @links = HTTP::Link->parse($link); | |||||||||||||||||||
| 257 | |||||||||||||||||||||
| 258 | 0 | my $issuer; | |||||||||||||||||||
| 259 | |||||||||||||||||||||
| 260 | 0 | for (@links) { | |||||||||||||||||||
| 261 | 0 | ▶ | if ($_->{relation} eq 'http://openid.net/specs/connect/1.0/issuer') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 262 | 0 | $issuer = $_->{iri}; | |||||||||||||||||||
| 263 | } | ||||||||||||||||||||
| 264 | } | ||||||||||||||||||||
| 265 | |||||||||||||||||||||
| 266 | 0 | ▶ | if ($issuer) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 267 | 0 | return $issuer; | |||||||||||||||||||
| 268 | } | ||||||||||||||||||||
| 269 | else { | ||||||||||||||||||||
| 270 | # Try the webid to find the issuer | ||||||||||||||||||||
| 271 | 0 | return $self->get_webid_openid_provider($webid); | |||||||||||||||||||
| 272 | } | ||||||||||||||||||||
| 273 | } | ||||||||||||||||||||
| 274 | |||||||||||||||||||||
| 275 | 0 | ▶ | sub get_webid_openid_provider { | ||||||||||||||||||
|
Subroutine
get_webid_openid_provider: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 276 | 0 | my ($self, $webid) = @_; | |||||||||||||||||||
| 277 | 0 | ▶ | $webid //= $self->webid; | ||||||||||||||||||
|
Condition0%0/3
$webid //= $self->webid
MC/DC0%0/2
$webid //= $self->webid
$webid
$self->webid
Truth table0%0/3
A //= B
A= $webidB= $self->webid
| |||||||||||||||||||||
| 278 | |||||||||||||||||||||
| 279 | # Lets try plain JSON parsing for fun.. | ||||||||||||||||||||
| 280 | 0 | my $res = $self->get($webid, 'Accept' => 'text/turtle'); | |||||||||||||||||||
| 281 | |||||||||||||||||||||
| 282 | 0 | ▶ | return undef unless $res; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 283 | |||||||||||||||||||||
| 284 | 0 | my $util = Web::Solid::Auth::Util->new; | |||||||||||||||||||
| 285 | 0 | my $model = $util->parse_turtle($res); | |||||||||||||||||||
| 286 | |||||||||||||||||||||
| 287 | 0 | my $sparql =<<EOF; | |||||||||||||||||||
| 288 | SELECT ?oidcIssuer { | ||||||||||||||||||||
| 289 | ?subject <http://www.w3.org/ns/solid/terms#oidcIssuer> ?oidcIssuer . | ||||||||||||||||||||
| 290 | } | ||||||||||||||||||||
| 291 | EOF | ||||||||||||||||||||
| 292 | |||||||||||||||||||||
| 293 | 0 | my $issuer; | |||||||||||||||||||
| 294 | 0 | ▶ | $util->sparql($model, $sparql, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 295 | 0 | my $res = shift; | |||||||||||||||||||
| 296 | 0 | $issuer = $res->value('oidcIssuer')->as_string; | |||||||||||||||||||
| 297 | 0 | }); | |||||||||||||||||||
| 298 | |||||||||||||||||||||
| 299 | 0 | return $issuer; | |||||||||||||||||||
| 300 | } | ||||||||||||||||||||
| 301 | |||||||||||||||||||||
| 302 | 0 | ▶ | sub get_client_configuration { | ||||||||||||||||||
|
Subroutine
get_client_configuration: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||
| 303 | 0 | my $self = shift; | |||||||||||||||||||
| 304 | |||||||||||||||||||||
| 305 | 0 | my $cache_dir = $self->get_cache_dir; | |||||||||||||||||||
| 306 | 0 | ▶ | path($cache_dir)->mkpath unless -d $cache_dir; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 307 | |||||||||||||||||||||
| 308 | 0 | my $openid_conf = $self->get_openid_configuration; | |||||||||||||||||||
| 309 | 0 | my $redirect_uri = $self->redirect_uri; | |||||||||||||||||||
| 310 | 0 | my $registration_endpoint = $openid_conf->{registration_endpoint}; | |||||||||||||||||||
| 311 | |||||||||||||||||||||
| 312 | 0 | my $cache_file = path($cache_dir)->child("client.json")->stringify; | |||||||||||||||||||
| 313 | |||||||||||||||||||||
| 314 | 0 | ▶ | unless (-f $cache_file) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 315 | 0 | ▶ | if ($self->client_id) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 316 | 0 | $self->log->info("using client document at " . $self->client_id); | |||||||||||||||||||
| 317 | |||||||||||||||||||||
| 318 | 0 | my $data = $self->get_json($self->client_id); | |||||||||||||||||||
| 319 | 0 | $self->log->debug("generating $cache_file"); | |||||||||||||||||||
| 320 | |||||||||||||||||||||
| 321 | 0 | path("$cache_file")->spew(encode_json($data)); | |||||||||||||||||||
| 322 | } | ||||||||||||||||||||
| 323 | else { | ||||||||||||||||||||
| 324 | 0 | $self->log->info("registering client at $registration_endpoint"); | |||||||||||||||||||
| 325 | |||||||||||||||||||||
| 326 | # Dynamic register the client. We request the openid and profile | ||||||||||||||||||||
| 327 | # scopes that are default for OpenID. The offline_access is | ||||||||||||||||||||
| 328 | # to be able to request refresh_tokens (not yet implemented). | ||||||||||||||||||||
| 329 | # The only safe response type is 'code' all other options send | ||||||||||||||||||||
| 330 | # sensitive data over the front channel and shouldn't be used. | ||||||||||||||||||||
| 331 | 0 | my $data = $self->post_json($registration_endpoint, { | |||||||||||||||||||
| 332 | grant_types => ["authorization_code", "refresh_token"], | ||||||||||||||||||||
| 333 | redirect_uris => [ $redirect_uri ] , | ||||||||||||||||||||
| 334 | scope => "openid profile offline_access" , | ||||||||||||||||||||
| 335 | response_types => ["code"] | ||||||||||||||||||||
| 336 | }); | ||||||||||||||||||||
| 337 | |||||||||||||||||||||
| 338 | 0 | ▶ | return undef unless $data; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 339 | |||||||||||||||||||||
| 340 | 0 | $self->log->infof("received %s", $data); | |||||||||||||||||||
| 341 | |||||||||||||||||||||
| 342 | 0 | $self->log->debug("generating $cache_file"); | |||||||||||||||||||
| 343 | |||||||||||||||||||||
| 344 | 0 | path("$cache_file")->spew(encode_json($data)); | |||||||||||||||||||
| 345 | } | ||||||||||||||||||||
| 346 | } | ||||||||||||||||||||
| 347 | |||||||||||||||||||||
| 348 | 0 | $self->log->debug("reading $cache_file"); | |||||||||||||||||||
| 349 | |||||||||||||||||||||
| 350 | 0 | my $json = path("$cache_file")->slurp; | |||||||||||||||||||
| 351 | |||||||||||||||||||||
| 352 | 0 | ▶ | return undef unless $json; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 353 | |||||||||||||||||||||
| 354 | 0 | return decode_json($json); | |||||||||||||||||||
| 355 | } | ||||||||||||||||||||
| 356 | |||||||||||||||||||||
| 357 | 0 | ▶ | sub get_openid_configuration { | ||||||||||||||||||
|
Subroutine
get_openid_configuration: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||
| 358 | 0 | my ($self) = @_; | |||||||||||||||||||
| 359 | |||||||||||||||||||||
| 360 | 0 | my $issuer = $self->issuer; | |||||||||||||||||||
| 361 | |||||||||||||||||||||
| 362 | # remove trailing slash (we will add it) | ||||||||||||||||||||
| 363 | 0 | $issuer =~ s{\/$}{}; | |||||||||||||||||||
| 364 | |||||||||||||||||||||
| 365 | 0 | my $cache_dir = $self->get_cache_dir; | |||||||||||||||||||
| 366 | 0 | ▶ | path($cache_dir)->mkpath unless -d $cache_dir; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 367 | |||||||||||||||||||||
| 368 | 0 | my $cache_file = path($cache_dir)->child("openid.json")->stringify; | |||||||||||||||||||
| 369 | |||||||||||||||||||||
| 370 | 0 | ▶ | unless (-f $cache_file) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 371 | 0 | my $url = "$issuer/.well-known/openid-configuration"; | |||||||||||||||||||
| 372 | |||||||||||||||||||||
| 373 | 0 | $self->log->info("reading openid configruation from $url"); | |||||||||||||||||||
| 374 | |||||||||||||||||||||
| 375 | # Get the well known openid | ||||||||||||||||||||
| 376 | 0 | my $data = $self->get_json($url); | |||||||||||||||||||
| 377 | |||||||||||||||||||||
| 378 | 0 | ▶ | return undef unless $data; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 379 | |||||||||||||||||||||
| 380 | 0 | $self->log->infof("received %s", $data); | |||||||||||||||||||
| 381 | |||||||||||||||||||||
| 382 | 0 | $self->log->debug("generating $cache_file"); | |||||||||||||||||||
| 383 | |||||||||||||||||||||
| 384 | 0 | path($cache_file)->spew(encode_json($data)); | |||||||||||||||||||
| 385 | } | ||||||||||||||||||||
| 386 | |||||||||||||||||||||
| 387 | 0 | $self->log->debug("reading $cache_file"); | |||||||||||||||||||
| 388 | |||||||||||||||||||||
| 389 | 0 | my $json = path($cache_file)->slurp; | |||||||||||||||||||
| 390 | |||||||||||||||||||||
| 391 | 0 | ▶ | return undef unless $json; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 392 | |||||||||||||||||||||
| 393 | 0 | return decode_json($json); | |||||||||||||||||||
| 394 | } | ||||||||||||||||||||
| 395 | |||||||||||||||||||||
| 396 | 0 | ▶ | sub get_key_configuration { | ||||||||||||||||||
|
Subroutine
get_key_configuration: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||
| 397 | 0 | my ($self) = @_; | |||||||||||||||||||
| 398 | |||||||||||||||||||||
| 399 | 0 | my $cache_dir = $self->get_cache_dir; | |||||||||||||||||||
| 400 | 0 | ▶ | path($cache_dir)->mkpath unless -d $cache_dir; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 401 | |||||||||||||||||||||
| 402 | 0 | my $cache_file = path($cache_dir)->child("key.json")->stringify; | |||||||||||||||||||
| 403 | |||||||||||||||||||||
| 404 | 0 | ▶ | unless (-f $cache_file) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 405 | # Create an P-256 elliptic curve key we will use in DPoP | ||||||||||||||||||||
| 406 | # headers. | ||||||||||||||||||||
| 407 | 0 | my $pk = Crypt::PK::ECC->new(); | |||||||||||||||||||
| 408 | 0 | $pk->generate_key('secp256r1'); | |||||||||||||||||||
| 409 | |||||||||||||||||||||
| 410 | 0 | $self->log->debug("generating $cache_file"); | |||||||||||||||||||
| 411 | |||||||||||||||||||||
| 412 | 0 | path($cache_file)->spew(encode_json({ | |||||||||||||||||||
| 413 | public => $pk->export_key_jwk('public') , | ||||||||||||||||||||
| 414 | private => $pk->export_key_jwk('private') | ||||||||||||||||||||
| 415 | })); | ||||||||||||||||||||
| 416 | } | ||||||||||||||||||||
| 417 | |||||||||||||||||||||
| 418 | 0 | $self->log->debug("reading $cache_file"); | |||||||||||||||||||
| 419 | |||||||||||||||||||||
| 420 | 0 | my $json = path($cache_file)->slurp; | |||||||||||||||||||
| 421 | |||||||||||||||||||||
| 422 | 0 | ▶ | return undef unless $json; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 423 | |||||||||||||||||||||
| 424 | 0 | my $pk = Crypt::PK::ECC->new(); | |||||||||||||||||||
| 425 | 0 | my $priv = decode_json($json)->{private}; | |||||||||||||||||||
| 426 | 0 | $pk->import_key(\$priv); | |||||||||||||||||||
| 427 | |||||||||||||||||||||
| 428 | 0 | return $pk; | |||||||||||||||||||
| 429 | } | ||||||||||||||||||||
| 430 | |||||||||||||||||||||
| 431 | ## Networking | ||||||||||||||||||||
| 432 | |||||||||||||||||||||
| 433 | 0 | ▶ | sub get { | ||||||||||||||||||
|
Subroutine
get: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 434 | 0 | my ($self, $url, %opts) = @_; | |||||||||||||||||||
| 435 | |||||||||||||||||||||
| 436 | 0 | my $response = $self->agent->get($url, %opts); | |||||||||||||||||||
| 437 | |||||||||||||||||||||
| 438 | 0 | ▶ | unless ($response->is_success) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 439 | 0 | $self->log->errorf("failed to GET($url): %s" , $response); | |||||||||||||||||||
| 440 | 0 | return undef; | |||||||||||||||||||
| 441 | } | ||||||||||||||||||||
| 442 | |||||||||||||||||||||
| 443 | 0 | return $response->decoded_content; | |||||||||||||||||||
| 444 | } | ||||||||||||||||||||
| 445 | |||||||||||||||||||||
| 446 | 0 | ▶ | sub get_json { | ||||||||||||||||||
|
Subroutine
get_json: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 447 | 0 | my ($self, $url, %opts) = @_; | |||||||||||||||||||
| 448 | 0 | return decode_json($self->get($url, %opts)); | |||||||||||||||||||
| 449 | } | ||||||||||||||||||||
| 450 | |||||||||||||||||||||
| 451 | 0 | ▶ | sub post { | ||||||||||||||||||
|
Subroutine
post: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 452 | 0 | my ($self, $url, $data, %opts) = @_; | |||||||||||||||||||
| 453 | |||||||||||||||||||||
| 454 | 0 | my $response = $self->agent->post($url, | |||||||||||||||||||
| 455 | %opts, | ||||||||||||||||||||
| 456 | Content => $data | ||||||||||||||||||||
| 457 | ); | ||||||||||||||||||||
| 458 | |||||||||||||||||||||
| 459 | 0 | ▶ | unless ($response->is_success) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 460 | 0 | $self->log->errorf("failed to POST($url): %s",$response); | |||||||||||||||||||
| 461 | 0 | return undef; | |||||||||||||||||||
| 462 | } | ||||||||||||||||||||
| 463 | |||||||||||||||||||||
| 464 | 0 | return $response->decoded_content; | |||||||||||||||||||
| 465 | } | ||||||||||||||||||||
| 466 | |||||||||||||||||||||
| 467 | 0 | ▶ | sub post_json { | ||||||||||||||||||
|
Subroutine
post_json: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 468 | 0 | my ($self, $url, $data, %opts) = @_; | |||||||||||||||||||
| 469 | |||||||||||||||||||||
| 470 | 0 | ▶ | $opts{'Content-Type'} //= 'application/json'; | ||||||||||||||||||
|
Condition0%0/2
$opts{"Content-Type"} //= "application/json"
MC/DC0%0/1
$opts{"Content-Type"} //= "application/json"
$opts{"Content-Type"}
Truth table0%0/2
A
A= $opts{"Content-Type"}
| |||||||||||||||||||||
| 471 | |||||||||||||||||||||
| 472 | 0 | my $response = $self->agent->post($url, | |||||||||||||||||||
| 473 | %opts , | ||||||||||||||||||||
| 474 | Content => encode_json($data) | ||||||||||||||||||||
| 475 | ); | ||||||||||||||||||||
| 476 | |||||||||||||||||||||
| 477 | 0 | ▶ | unless ($response->is_success) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 478 | 0 | $self->log->errorf("failed to POST($url): %s",$response); | |||||||||||||||||||
| 479 | 0 | return undef; | |||||||||||||||||||
| 480 | } | ||||||||||||||||||||
| 481 | |||||||||||||||||||||
| 482 | 0 | return decode_json($response->decoded_content); | |||||||||||||||||||
| 483 | } | ||||||||||||||||||||
| 484 | |||||||||||||||||||||
| 485 | 0 | ▶ | sub options { | ||||||||||||||||||
|
Subroutine
options: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 486 | 0 | my ($self, $url) = @_; | |||||||||||||||||||
| 487 | |||||||||||||||||||||
| 488 | 0 | my $response = $self->agent->request( | |||||||||||||||||||
| 489 | HTTP::Request->new(OPTIONS => $url) | ||||||||||||||||||||
| 490 | ); | ||||||||||||||||||||
| 491 | |||||||||||||||||||||
| 492 | 0 | ▶ | unless ($response->is_success) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 493 | 0 | $self->log->errorf("failed to OPTIONS($url): %s" , $response); | |||||||||||||||||||
| 494 | 0 | return undef; | |||||||||||||||||||
| 495 | } | ||||||||||||||||||||
| 496 | |||||||||||||||||||||
| 497 | 0 | return $response; | |||||||||||||||||||
| 498 | } | ||||||||||||||||||||
| 499 | |||||||||||||||||||||
| 500 | 0 | ▶ | sub make_url { | ||||||||||||||||||
|
Subroutine
make_url: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||
| 501 | 0 | my ($self, $url,$params) = @_; | |||||||||||||||||||
| 502 | |||||||||||||||||||||
| 503 | 0 | my @qparam = (); | |||||||||||||||||||
| 504 | |||||||||||||||||||||
| 505 | 0 | ▶ | for my $key (keys %{$params // {} }) { | ||||||||||||||||||
|
Condition0%0/2
$params // {}
MC/DC0%0/1
$params // {}
$params
Truth table0%0/2
A
A= $params
| |||||||||||||||||||||
| 506 | 0 | my $value = URI::Escape::uri_escape($params->{$key}); | |||||||||||||||||||
| 507 | 0 | push @qparam , "$key=$value"; | |||||||||||||||||||
| 508 | } | ||||||||||||||||||||
| 509 | |||||||||||||||||||||
| 510 | 0 | ▶ | if (@qparam) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 511 | 0 | $url .= "?" . join("&", @qparam); | |||||||||||||||||||
| 512 | } | ||||||||||||||||||||
| 513 | |||||||||||||||||||||
| 514 | 0 | $url; | |||||||||||||||||||
| 515 | } | ||||||||||||||||||||
| 516 | |||||||||||||||||||||
| 517 | # Crypto | ||||||||||||||||||||
| 518 | |||||||||||||||||||||
| 519 | 0 | ▶ | sub make_random_string { | ||||||||||||||||||
|
Subroutine
make_random_string: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 520 | 0 | my $self = shift; | |||||||||||||||||||
| 521 | 0 | my $str = MIME::Base64::encode_base64url( | |||||||||||||||||||
| 522 | Data::UUID->new->create() . | ||||||||||||||||||||
| 523 | Data::UUID->new->create() . | ||||||||||||||||||||
| 524 | Data::UUID->new->create() | ||||||||||||||||||||
| 525 | ); | ||||||||||||||||||||
| 526 | 0 | $str; | |||||||||||||||||||
| 527 | } | ||||||||||||||||||||
| 528 | |||||||||||||||||||||
| 529 | 0 | ▶ | sub make_token_for { | ||||||||||||||||||
|
Subroutine
make_token_for: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 530 | 0 | my ($self, $uri, $method) = @_; | |||||||||||||||||||
| 531 | |||||||||||||||||||||
| 532 | # With DPoP headers access_tokens can be protected. When requesting | ||||||||||||||||||||
| 533 | # an access_token from a token_endpoint a DPoP headers is included | ||||||||||||||||||||
| 534 | # which contains our public key (inside the signed token header). | ||||||||||||||||||||
| 535 | # Our public key will then be part of the returned access_token. | ||||||||||||||||||||
| 536 | # | ||||||||||||||||||||
| 537 | # When later on you will send the access_token to a resource provider | ||||||||||||||||||||
| 538 | # it can check the signed DPoP header in combination with our public | ||||||||||||||||||||
| 539 | # key in the access_token that you are in posession of the private key | ||||||||||||||||||||
| 540 | # that matches the public key in the access_token. | ||||||||||||||||||||
| 541 | # | ||||||||||||||||||||
| 542 | # In this way, when some evil resource provider steals your access_token | ||||||||||||||||||||
| 543 | # it can't be reused without your private key. | ||||||||||||||||||||
| 544 | |||||||||||||||||||||
| 545 | 0 | my $pk = $self->get_key_configuration; | |||||||||||||||||||
| 546 | |||||||||||||||||||||
| 547 | 0 | my $header = { | |||||||||||||||||||
| 548 | typ => 'dpop+jwt' , | ||||||||||||||||||||
| 549 | alg => 'ES256' , | ||||||||||||||||||||
| 550 | jwk => JSON::decode_json($pk->export_key_jwk('public')) , | ||||||||||||||||||||
| 551 | }; | ||||||||||||||||||||
| 552 | |||||||||||||||||||||
| 553 | 0 | $self->log->debugf("DPoP(header) %s" , $header); | |||||||||||||||||||
| 554 | |||||||||||||||||||||
| 555 | 0 | my $payload = { | |||||||||||||||||||
| 556 | # A jti is a random string that protects the token_endpoint server | ||||||||||||||||||||
| 557 | # against replay attacks | ||||||||||||||||||||
| 558 | jti => $self->make_random_string, | ||||||||||||||||||||
| 559 | # Limits the DPoP token only to this method | ||||||||||||||||||||
| 560 | htm => $method , | ||||||||||||||||||||
| 561 | # Limits the DPop token only to this uri | ||||||||||||||||||||
| 562 | htu => $uri , | ||||||||||||||||||||
| 563 | # The time this token was issued | ||||||||||||||||||||
| 564 | iat => time , | ||||||||||||||||||||
| 565 | }; | ||||||||||||||||||||
| 566 | |||||||||||||||||||||
| 567 | 0 | $self->log->debugf("DPoP(payload) %s" , $payload); | |||||||||||||||||||
| 568 | |||||||||||||||||||||
| 569 | 0 | my $token = Crypt::JWT::encode_jwt( | |||||||||||||||||||
| 570 | payload => $payload , | ||||||||||||||||||||
| 571 | key => $pk , | ||||||||||||||||||||
| 572 | alg => 'ES256' , | ||||||||||||||||||||
| 573 | extra_headers => $header | ||||||||||||||||||||
| 574 | ); | ||||||||||||||||||||
| 575 | |||||||||||||||||||||
| 576 | 0 | return $token; | |||||||||||||||||||
| 577 | } | ||||||||||||||||||||
| 578 | |||||||||||||||||||||
| 579 | 1 | 1; | |||||||||||||||||||
| 580 | |||||||||||||||||||||
| 581 | __END__ | ||||||||||||||||||||