| 1 | package Plack::Auth::SSO::OIDC; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use feature qw(:5.10); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Data::Util qw(:check); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Data::UUID; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Plack::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Plack::Session; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use URI; | ||||||||||||||||||||||||||
|
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 Crypt::JWT; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use MIME::Base64; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Digest::SHA; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | our $VERSION = "0.015"; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | with "Plack::Auth::SSO"; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | has scope => ( | ||||||||||||||||||||||||||||
| 24 | is => "lazy", | ||||||||||||||||||||||||||||
| 25 | 0 | ▶ | isa => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | is_string($_[0]) or die("scope should be string"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | index($_[0], "openid") >= 0 or die("default scope openid not included"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | }, | ||||||||||||||||||||||||||||
| 29 | 0 | ▶ | default => sub { "openid profile email" }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 30 | 1 | required => 1 | |||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | has client_id => ( | ||||||||||||||||||||||||||||
| 34 | is => "ro", | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | isa => sub { is_string($_[0]) or die("client_id should be string"); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 1 | required => 1 | |||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | has client_secret => ( | ||||||||||||||||||||||||||||
| 40 | is => "ro", | ||||||||||||||||||||||||||||
| 41 | 0 | ▶ | isa => sub { is_string($_[0]) or die("client_secret should be string"); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 1 | required => 1 | |||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | has openid_uri => ( | ||||||||||||||||||||||||||||
| 46 | is => "ro", | ||||||||||||||||||||||||||||
| 47 | 0 | ▶ | isa => sub { is_string($_[0]) or die("openid_uri should be string"); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 1 | required => 1 | |||||||||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | has uid_key => ( | ||||||||||||||||||||||||||||
| 52 | is => "ro", | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | isa => sub { is_string($_[0]) or die("uid_key should be string"); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 1 | required => 1 | |||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | has authorize_params => ( | ||||||||||||||||||||||||||||
| 58 | is => "ro", | ||||||||||||||||||||||||||||
| 59 | 0 | ▶ | isa => sub { is_hash_ref($_[0]) or die("authorize_params should be hash reference"); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | lazy => 1, | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | default => sub { +{}; }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 62 | 1 | required => 1 | |||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | has allowed_authorize_params => ( | ||||||||||||||||||||||||||||
| 66 | is => "ro", | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | isa => sub { is_array_ref($_[0]) or die("allowed_authorize_params should be array reference"); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | lazy => 1, | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | default => sub { []; }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 70 | 1 | required => 1 | |||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | has store_oidc_response => ( | ||||||||||||||||||||||||||||
| 74 | is => "ro", | ||||||||||||||||||||||||||||
| 75 | 0 | ▶ | default => sub { 1; }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 76 | 1 | ); | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # internal (non overwritable) moo attributes | ||||||||||||||||||||||||||||
| 80 | has json => ( | ||||||||||||||||||||||||||||
| 81 | is => "ro", | ||||||||||||||||||||||||||||
| 82 | lazy => 1, | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 84 | 0 | JSON->new->utf8(1); | |||||||||||||||||||||||||||
| 85 | }, | ||||||||||||||||||||||||||||
| 86 | 1 | init_arg => undef | |||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | has ua => ( | ||||||||||||||||||||||||||||
| 90 | is => "ro", | ||||||||||||||||||||||||||||
| 91 | lazy => 1, | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 93 | 0 | LWP::UserAgent->new(); | |||||||||||||||||||||||||||
| 94 | }, | ||||||||||||||||||||||||||||
| 95 | 1 | init_arg => undef | |||||||||||||||||||||||||||
| 96 | ); | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 1 | has openid_configuration => ( | |||||||||||||||||||||||||||
| 99 | is => "lazy", | ||||||||||||||||||||||||||||
| 100 | init_arg => undef | ||||||||||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 1 | has jwks => ( | |||||||||||||||||||||||||||
| 104 | is => "lazy", | ||||||||||||||||||||||||||||
| 105 | init_arg => undef | ||||||||||||||||||||||||||||
| 106 | ); | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub get_json { | ||||||||||||||||||||||||||
|
Subroutine
get_json: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | my ($self, $url) = @_; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | my $res = $self->ua->get($url); | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | if ( $res->code ne "200" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | $self->log->errorf("url $url returned invalid status code %s", $res->code); | |||||||||||||||||||||||||||
| 117 | 0 | return undef, "INVALID_HTTP_STATUS"; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if ( index($res->content_type, "json") < 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | $self->log->errorf("url $url returned invalid content type %s", $res->content_type); | |||||||||||||||||||||||||||
| 124 | 0 | return undef, "INVALID_HTTP_CONTENT_TYPE"; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | my $data; | |||||||||||||||||||||||||||
| 129 | my $data_error; | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 131 | 0 | $data = $self->json->decode($res->content); | |||||||||||||||||||||||||||
| 132 | 0 | ▶ | } catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 133 | 0 | $data_error = $_; | |||||||||||||||||||||||||||
| 134 | 0 | }; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | if ( defined($data_error) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | $self->log->error("could not decode json returned from $url"); | |||||||||||||||||||||||||||
| 139 | 0 | return undef, "INVALID_HTTP_CONTENT"; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | $data; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | sub _build_openid_configuration { | ||||||||||||||||||||||||||
|
Subroutine
_build_openid_configuration: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | my $self = $_[0]; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | my $url = $self->openid_uri; | |||||||||||||||||||||||||||
| 152 | 0 | my ($data, @errors) = $self->get_json($url); | |||||||||||||||||||||||||||
| 153 | 0 | ▶ | die("unable to retrieve openid configuration from $url: ".join(", ",@errors)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | unless defined($data); | ||||||||||||||||||||||||||||
| 155 | 0 | $data; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | # https://auth0.com/blog/navigating-rs256-and-jwks/ | ||||||||||||||||||||||||||||
| 160 | 0 | ▶ | sub _build_jwks { | ||||||||||||||||||||||||||
|
Subroutine
_build_jwks: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | my $self = $_[0]; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 0 | my $jwks_uri = $self->openid_configuration->{jwks_uri}; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | ▶ | die("attribute jwks_uri not found in openid_configuration") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | unless is_string($jwks_uri); | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 0 | my ($data, @errors) = $self->get_json($jwks_uri); | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | ▶ | die("unable to retrieve jwks from ".$self->openid_configuration->{jwks_uri}.":".join(", ", @errors)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | if scalar(@errors); | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | $data; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 0 | ▶ | sub redirect_uri { | ||||||||||||||||||||||||||
|
Subroutine
redirect_uri: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | my ($self, $request) = @_; | |||||||||||||||||||||||||||
| 181 | 0 | my $redirect_uri = $self->uri_base().$request->request_uri(); | |||||||||||||||||||||||||||
| 182 | 0 | my $idx = index( $redirect_uri, "?" ); | |||||||||||||||||||||||||||
| 183 | 0 | ▶ | if ( $idx >= 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | $redirect_uri = substr( $redirect_uri, 0, $idx ); | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | 0 | $redirect_uri; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | ▶ | sub make_random_string { | ||||||||||||||||||||||||||
|
Subroutine
make_random_string: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 0 | MIME::Base64::encode_base64url( | |||||||||||||||||||||||||||
| 195 | Data::UUID->new->create() . | ||||||||||||||||||||||||||||
| 196 | Data::UUID->new->create() . | ||||||||||||||||||||||||||||
| 197 | Data::UUID->new->create() | ||||||||||||||||||||||||||||
| 198 | ); | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | ▶ | sub generate_authorization_uri { | ||||||||||||||||||||||||||
|
Subroutine
generate_authorization_uri: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 0 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 0 | my $request = $args{request}; | |||||||||||||||||||||||||||
| 207 | 0 | my $session = $args{session}; | |||||||||||||||||||||||||||
| 208 | 0 | my $query_params = $request->query_parameters(); | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 0 | my $openid_conf = $self->openid_configuration; | |||||||||||||||||||||||||||
| 211 | 0 | my $authorization_endpoint = $openid_conf->{authorization_endpoint}; | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | # cf. https://developers.onelogin.com/openid-connect/guides/auth-flow-pkce | ||||||||||||||||||||||||||||
| 214 | # Note: minimum of 43 characters! | ||||||||||||||||||||||||||||
| 215 | 0 | my $code_verifier = $self->make_random_string(); | |||||||||||||||||||||||||||
| 216 | 0 | my $code_challenge = MIME::Base64::encode_base64url(Digest::SHA::sha256($code_verifier),""); | |||||||||||||||||||||||||||
| 217 | 0 | my $state = $self->make_random_string(); | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 0 | my %query; | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | # merge in allowed params from current url | ||||||||||||||||||||||||||||
| 222 | 0 | for my $key( @{ $self->allowed_authorize_params } ){ | |||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 0 | my $val = $query_params->get($key); | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 0 | ▶ | next unless is_string($val); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 0 | $query{$key} = $val; | |||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | %query = ( | ||||||||||||||||||||||||||||
| 233 | %query, | ||||||||||||||||||||||||||||
| 234 | 0 | %{ $self->authorize_params }, | |||||||||||||||||||||||||||
| 235 | code_challenge => $code_challenge, | ||||||||||||||||||||||||||||
| 236 | code_challenge_method => "S256", | ||||||||||||||||||||||||||||
| 237 | state => $state, | ||||||||||||||||||||||||||||
| 238 | scope => $self->scope(), | ||||||||||||||||||||||||||||
| 239 | client_id => $self->client_id, | ||||||||||||||||||||||||||||
| 240 | response_type => "code", | ||||||||||||||||||||||||||||
| 241 | redirect_uri => $self->redirect_uri($request) | ||||||||||||||||||||||||||||
| 242 | ); | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 0 | my $uri = URI->new($authorization_endpoint); | |||||||||||||||||||||||||||
| 245 | 0 | $uri->query_form(%query); | |||||||||||||||||||||||||||
| 246 | 0 | $self->set_csrf_token($session, $state); | |||||||||||||||||||||||||||
| 247 | 0 | $session->set("auth_sso_oidc_code_verifier", $code_verifier); | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 0 | $uri->as_string; | |||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 0 | ▶ | around cleanup => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 0 | my ($orig, $self, $session) = @_; | |||||||||||||||||||||||||||
| 256 | 0 | $self->$orig($session); | |||||||||||||||||||||||||||
| 257 | 0 | $session->remove("auth_sso_oidc_code_verifier"); | |||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 1 | }; | |||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | # extract_claims_from_id_token(id_token) : claims_as_hash | ||||||||||||||||||||||||||||
| 262 | 0 | ▶ | sub extract_claims_from_id_token { | ||||||||||||||||||||||||||
|
Subroutine
extract_claims_from_id_token: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 0 | my ($self, $id_token) = @_; | |||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 0 | my ($jose_header, $payload, $s) = split(/\./o, $id_token); | |||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | # '{ "alg": "RS256", "kid": "my-key-id" }' | ||||||||||||||||||||||||||||
| 269 | 0 | $jose_header = $self->json->decode(MIME::Base64::decode($jose_header)); | |||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | #{ "keys": [{ "kid": "my-key-id", "alg": "RS256", "use": "sig" .. }] } | ||||||||||||||||||||||||||||
| 272 | 0 | my $jwks = $self->jwks(); | |||||||||||||||||||||||||||
| 273 | 0 | my ($key) = grep { $_->{kid} eq $jose_header->{kid} } | |||||||||||||||||||||||||||
| 274 | 0 | @{ $jwks->{keys} }; | |||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 0 | my $claims; | |||||||||||||||||||||||||||
| 277 | my $claims_error; | ||||||||||||||||||||||||||||
| 278 | 0 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 279 | 0 | $claims = Crypt::JWT::decode_jwt(token => $id_token, key => $key); | |||||||||||||||||||||||||||
| 280 | 0 | ▶ | } catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 281 | 0 | $claims_error = $_; | |||||||||||||||||||||||||||
| 282 | 0 | }; | |||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 0 | ▶ | $self->log->errorf("error occurred while decoding JWS: %s", $claims_error) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | if defined $claims_error; | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 0 | $claims; | |||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 0 | ▶ | sub exchange_code_for_tokens { | ||||||||||||||||||||||||||
|
Subroutine
exchange_code_for_tokens: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | 0 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | 0 | my $request = $args{request}; | |||||||||||||||||||||||||||
| 296 | 0 | my $session = $args{session}; | |||||||||||||||||||||||||||
| 297 | 0 | my $code = $args{code}; | |||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | 0 | my $openid_conf = $self->openid_configuration; | |||||||||||||||||||||||||||
| 300 | 0 | my $token_endpoint = $openid_conf->{token_endpoint}; | |||||||||||||||||||||||||||
| 301 | 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"}
| |||||||||||||||||||||||||||||
| 302 | 0 | ▶ | $token_endpoint_auth_methods_supported = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | is_array_ref($token_endpoint_auth_methods_supported) ? | ||||||||||||||||||||||||||||
| 304 | $token_endpoint_auth_methods_supported : | ||||||||||||||||||||||||||||
| 305 | [$token_endpoint_auth_methods_supported]; | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 0 | my $auth_sso_oidc_code_verifier = $session->get("auth_sso_oidc_code_verifier"); | |||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 0 | my $params = { | |||||||||||||||||||||||||||
| 310 | grant_type => "authorization_code", | ||||||||||||||||||||||||||||
| 311 | client_id => $self->client_id, | ||||||||||||||||||||||||||||
| 312 | code => $code, | ||||||||||||||||||||||||||||
| 313 | code_verifier => $auth_sso_oidc_code_verifier, | ||||||||||||||||||||||||||||
| 314 | redirect_uri => $self->redirect_uri($request), | ||||||||||||||||||||||||||||
| 315 | }; | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | 0 | my $headers = { | |||||||||||||||||||||||||||
| 318 | "Content-Type" => "application/x-www-form-urlencoded" | ||||||||||||||||||||||||||||
| 319 | }; | ||||||||||||||||||||||||||||
| 320 | 0 | my $client_id = $self->client_id; | |||||||||||||||||||||||||||
| 321 | 0 | my $client_secret = $self->client_secret; | |||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 0 | ▶ | if ( grep { $_ eq "client_secret_basic" } @$token_endpoint_auth_methods_supported ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 0 | $self->log->info("using client_secret_basic"); | |||||||||||||||||||||||||||
| 326 | 0 | $headers->{"Authorization"} = "Basic " . MIME::Base64::encode("$client_id:$client_secret", ""); | |||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | 0 | elsif ( grep { $_ eq "client_secret_post" } @$token_endpoint_auth_methods_supported ) { | |||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | 0 | $self->log->info("using client_secret_post"); | |||||||||||||||||||||||||||
| 332 | 0 | $params->{client_secret} = $client_secret; | |||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | else { | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | 0 | die("token_endpoint $token_endpoint does not support client_secret_basic or client_secret_post"); | |||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 0 | my $res = $self->ua->post( | |||||||||||||||||||||||||||
| 342 | $token_endpoint, | ||||||||||||||||||||||||||||
| 343 | $params, | ||||||||||||||||||||||||||||
| 344 | %$headers | ||||||||||||||||||||||||||||
| 345 | ); | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | 0 | ▶ | die("$token_endpoint returned invalid content type ".$res->content_type) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | unless $res->content_type =~ /json/o; | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | 0 | $self->json->decode($res->content); | |||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 0 | ▶ | sub to_app { | ||||||||||||||||||||||||||
|
Subroutine
to_app: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | 0 | my $self = $_[0]; | |||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=13 SCAR=52.0
| |||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 0 | my $env = $_[0]; | |||||||||||||||||||||||||||
| 360 | 0 | my $log = $self->log(); | |||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 0 | my $request = Plack::Request->new($env); | |||||||||||||||||||||||||||
| 363 | 0 | my $session = Plack::Session->new($env); | |||||||||||||||||||||||||||
| 364 | 0 | my $query_params = $request->query_parameters(); | |||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 0 | ▶ | if( $self->log->is_debug() ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | 0 | $self->log->debugf( "incoming query parameters: %s", [$query_params->flatten] ); | |||||||||||||||||||||||||||
| 369 | 0 | $self->log->debugf( "session: %s", $session->dump() ); | |||||||||||||||||||||||||||
| 370 | 0 | $self->log->debugf( "session_key for auth_sso: %s" . $self->session_key() ); | |||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | 0 | ▶ | if ( $request->method ne "GET" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | 0 | $self->log->errorf("invalid http method %s", $request->method); | |||||||||||||||||||||||||||
| 377 | 0 | return [400, [ "Content-Type" => "text/plain" ], ["invalid http method"]]; | |||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | 0 | my $state = $query_params->get("state"); | |||||||||||||||||||||||||||
| 382 | 0 | my $stored_state = $self->get_csrf_token($session); | |||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | # remove auth_sso from possibly previous successfull authentication | ||||||||||||||||||||||||||||
| 385 | # (allowing for reauthentication) | ||||||||||||||||||||||||||||
| 386 | 0 | $session->remove($self->session_key); | |||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | # redirect to authorization url | ||||||||||||||||||||||||||||
| 389 | 0 | ▶ | if ( !(is_string($stored_state) && is_string($state)) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
is_string($stored_state) and is_string($state)
MC/DC0%0/2
is_string($stored_state) and is_string($state)
is_string($stored_state)
is_string($state)
Truth table0%0/3
A and B
A= is_string($stored_state)B= is_string($state)
| |||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | 0 | $self->cleanup($session); | |||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | 0 | my $authorization_uri = $self->generate_authorization_uri( | |||||||||||||||||||||||||||
| 394 | request => $request, | ||||||||||||||||||||||||||||
| 395 | session => $session | ||||||||||||||||||||||||||||
| 396 | ); | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | 0 | return [302, [Location => $authorization_uri], []]; | |||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | # check csrf | ||||||||||||||||||||||||||||
| 403 | 0 | ▶ | if ( $stored_state ne $state ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | 0 | $self->cleanup($session); | |||||||||||||||||||||||||||
| 406 | 0 | $self->set_auth_sso_error( $session,{ | |||||||||||||||||||||||||||
| 407 | package => __PACKAGE__, | ||||||||||||||||||||||||||||
| 408 | package_id => $self->id, | ||||||||||||||||||||||||||||
| 409 | type => "CSRF_DETECTED", | ||||||||||||||||||||||||||||
| 410 | content => "CSRF_DETECTED" | ||||||||||||||||||||||||||||
| 411 | }); | ||||||||||||||||||||||||||||
| 412 | 0 | return $self->redirect_to_error(); | |||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | # validate authorization returned from idp | ||||||||||||||||||||||||||||
| 417 | 0 | my $error = $query_params->get("error"); | |||||||||||||||||||||||||||
| 418 | 0 | my $error_description = $query_params->get("error_description"); | |||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | 0 | ▶ | if ( is_string($error) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | 0 | $self->cleanup($session); | |||||||||||||||||||||||||||
| 423 | 0 | $self->set_auth_sso_error($session, { | |||||||||||||||||||||||||||
| 424 | package => __PACKAGE__, | ||||||||||||||||||||||||||||
| 425 | package_id => $self->id, | ||||||||||||||||||||||||||||
| 426 | type => $error, | ||||||||||||||||||||||||||||
| 427 | content => $error_description | ||||||||||||||||||||||||||||
| 428 | }); | ||||||||||||||||||||||||||||
| 429 | 0 | return $self->redirect_to_error(); | |||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | 0 | my $code = $query_params->get("code"); | |||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | 0 | ▶ | unless ( is_string($code) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | 0 | $self->cleanup($session); | |||||||||||||||||||||||||||
| 438 | 0 | $self->set_auth_sso_error($session, { | |||||||||||||||||||||||||||
| 439 | package => __PACKAGE__, | ||||||||||||||||||||||||||||
| 440 | package_id => $self->id, | ||||||||||||||||||||||||||||
| 441 | type => "AUTH_SSO_OIDC_AUTHORIZATION_NO_CODE", | ||||||||||||||||||||||||||||
| 442 | content => "oidc authorization endpoint did not return query parameter code" | ||||||||||||||||||||||||||||
| 443 | }); | ||||||||||||||||||||||||||||
| 444 | 0 | return $self->redirect_to_error(); | |||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | 0 | my $tokens = $self->exchange_code_for_tokens( | |||||||||||||||||||||||||||
| 449 | request => $request, | ||||||||||||||||||||||||||||
| 450 | session => $session, | ||||||||||||||||||||||||||||
| 451 | code => $code | ||||||||||||||||||||||||||||
| 452 | ); | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | 0 | ▶ | $self->log->debugf("tokens: %s", $tokens) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 455 | if $self->log->is_debug(); | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | 0 | ▶ | if ( is_string($tokens->{error}) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | 0 | $self->cleanup($session); | |||||||||||||||||||||||||||
| 460 | $self->set_auth_sso_error($session, { | ||||||||||||||||||||||||||||
| 461 | package => __PACKAGE__, | ||||||||||||||||||||||||||||
| 462 | package_id => $self->id, | ||||||||||||||||||||||||||||
| 463 | type => $tokens->{error}, | ||||||||||||||||||||||||||||
| 464 | content => $tokens->{error_description} | ||||||||||||||||||||||||||||
| 465 | 0 | }); | |||||||||||||||||||||||||||
| 466 | 0 | return $self->redirect_to_error(); | |||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | 0 | my $claims = $self->extract_claims_from_id_token($tokens->{id_token}); | |||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||
| 472 | 0 | ▶ | $self->log->debugf("claims: %s", $claims) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 473 | if $self->log->is_debug(); | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | 0 | $self->cleanup($session); | |||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | my $session_data = +{ | ||||||||||||||||||||||||||||
| 478 | extra => {}, | ||||||||||||||||||||||||||||
| 479 | info => $claims, | ||||||||||||||||||||||||||||
| 480 | 0 | uid => $claims->{ $self->uid_key() }, | |||||||||||||||||||||||||||
| 481 | package => __PACKAGE__, | ||||||||||||||||||||||||||||
| 482 | package_id => $self->id, | ||||||||||||||||||||||||||||
| 483 | }; | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | 0 | ▶ | if ($self->store_oidc_response()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 486 | $session_data->{response} = +{ | ||||||||||||||||||||||||||||
| 487 | 0 | content => $self->json->encode($tokens), | |||||||||||||||||||||||||||
| 488 | content_type => "application/json" | ||||||||||||||||||||||||||||
| 489 | }; | ||||||||||||||||||||||||||||
| 490 | } | ||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | 0 | $self->set_auth_sso($session, $session_data); | |||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | 0 | ▶ | $self->log->debugf("auth_sso: %s", $self->get_auth_sso($session)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 495 | if $self->log->is_debug(); | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | 0 | return $self->redirect_to_authorization(); | |||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | 0 | }; | |||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | } | ||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||
| 503 | 1 | 1; | |||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||
| 505 | =pod | ||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||
| 507 | =head1 NAME | ||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | Plack::Auth::SSO::OIDC - implementation of OpenID Connect for Plack::Auth::SSO | ||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | =begin markdown | ||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||
| 513 | # STATUS | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | [](https://travis-ci.org/LibreCat/Plack-Auth-SSO-OIDC) | ||||||||||||||||||||||||||||
| 516 | [](https://coveralls.io/r/LibreCat/Plack-Auth-SSO-OIDC) | ||||||||||||||||||||||||||||
| 517 | [](http://cpants.cpanauthors.org/dist/Plack-Auth-SSO-OIDC) | ||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | =end markdown | ||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||
| 523 | This is an implementation of L<Plack::Auth::SSO> to authenticate against a openid connect server. | ||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | It inherits all configuration options from its parent. | ||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||
| 529 | # in your app.psi (Plack) | ||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | use strict; | ||||||||||||||||||||||||||||
| 532 | use warnings; | ||||||||||||||||||||||||||||
| 533 | use Plack::Builder; | ||||||||||||||||||||||||||||
| 534 | use JSON; | ||||||||||||||||||||||||||||
| 535 | use Plack::Auth::SSO::OIDC; | ||||||||||||||||||||||||||||
| 536 | use Plack::Session::Store::File; | ||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | my $uri_base = "http://localhost:5000"; | ||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | builder { | ||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | # session middleware needed to store "auth_sso" and/or "auth_sso_error" | ||||||||||||||||||||||||||||
| 543 | # in memory session store for testing purposes | ||||||||||||||||||||||||||||
| 544 | enable "Session"; | ||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | # for authentication, redirect your users to this path | ||||||||||||||||||||||||||||
| 547 | mount "/auth/oidc" => Plack::Auth::SSO::OIDC->new( | ||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||
| 549 | # plack application needs to know about the base url of this application | ||||||||||||||||||||||||||||
| 550 | uri_base => $uri_base, | ||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | # after successfull authentication, user is redirected to this path (uri_base is used!) | ||||||||||||||||||||||||||||
| 553 | authorization_path => "/auth/callback", | ||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||
| 555 | # when authentication fails at the identity provider | ||||||||||||||||||||||||||||
| 556 | # user is redirected to this path with session key "auth_sso_error" (hash) | ||||||||||||||||||||||||||||
| 557 | error_path => "/auth/error", | ||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||
| 559 | # openid connect discovery url | ||||||||||||||||||||||||||||
| 560 | openid_uri => "https://example.oidc.org/auth/oidc/.well-known/openid-configuration", | ||||||||||||||||||||||||||||
| 561 | client_id => "my-client-id", | ||||||||||||||||||||||||||||
| 562 | client_secret => "myclient-secret", | ||||||||||||||||||||||||||||
| 563 | uid_key => "email" | ||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||
| 565 | )->to_app(); | ||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||
| 567 | # example psgi app that is called after successfull authentication at /auth/oidc (see above) | ||||||||||||||||||||||||||||
| 568 | # it expects session key "auth_sso" to be present | ||||||||||||||||||||||||||||
| 569 | # here you typically create a user session based on the uid in "auth_sso" | ||||||||||||||||||||||||||||
| 570 | mount "/auth/callback" => sub { | ||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||
| 572 | my $env = shift; | ||||||||||||||||||||||||||||
| 573 | my $session = Plack::Session->new($env); | ||||||||||||||||||||||||||||
| 574 | my $auth_sso= $session->get("auth_sso"); | ||||||||||||||||||||||||||||
| 575 | my $user = MyUsers->get( $auth_sso->{uid} ); | ||||||||||||||||||||||||||||
| 576 | $session->set("user_id", $user->{id}); | ||||||||||||||||||||||||||||
| 577 | [ 200, [ "Content-Type" => "text/plain" ], [ | ||||||||||||||||||||||||||||
| 578 | "logged in! ", $user->{name} | ||||||||||||||||||||||||||||
| 579 | ]]; | ||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||
| 581 | }; | ||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||
| 583 | # example psgi app that is called after unsuccessfull authentication at /auth/oidc (see above) | ||||||||||||||||||||||||||||
| 584 | # it expects session key "auth_sso_error" to be present | ||||||||||||||||||||||||||||
| 585 | mount "/auth/error" => sub { | ||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||
| 587 | my $env = shift; | ||||||||||||||||||||||||||||
| 588 | my $session = Plack::Session->new($env); | ||||||||||||||||||||||||||||
| 589 | my $auth_sso_error = $session->get("auth_sso_error"); | ||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||
| 591 | [ 200, [ "Content-Type" => "text/plain" ], [ | ||||||||||||||||||||||||||||
| 592 | "something happened during single sign on authentication: ", | ||||||||||||||||||||||||||||
| 593 | $auth_sso_error->{content} | ||||||||||||||||||||||||||||
| 594 | ]]; | ||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||
| 596 | }; | ||||||||||||||||||||||||||||
| 597 | }; | ||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||
| 599 | =head1 CONSTRUCTOR ARGUMENTS | ||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||
| 601 | =over 4 | ||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||
| 603 | =item C<< uri_base >> | ||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||
| 605 | See L<Plack::Auth::SSO/uri_base> | ||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | =item C<< id >> | ||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||
| 609 | See L<Plack::Auth::SSO/id> | ||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||
| 611 | =item C<< session_key >> | ||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||
| 613 | See L<Plack::Auth::SSO/session_key> | ||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | =item C<< authorization_path >> | ||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||
| 617 | See L<Plack::Auth::SSO/authorization_path> | ||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||
| 619 | =item C<< error_path >> | ||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||
| 621 | See L<Plack::Auth::SSO/error_path> | ||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||
| 623 | =item C<< openid_uri >> | ||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||
| 625 | base url of the OIDC discovery url. | ||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||
| 627 | typically an url that ends on C<< /.well-known/openid-configuration >> | ||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||
| 629 | =item C<< client_id >> | ||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||
| 631 | client-id as given by the OIDC service | ||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||
| 633 | =item C<< client_secret >> | ||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||
| 635 | client-secret as given by the OIDC service | ||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||
| 637 | =item C<< scope >> | ||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||
| 639 | Scope requested from the OIDC service. | ||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||
| 641 | Space separated string containing all scopes | ||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||
| 643 | Default: C<< "openid profile email" >> | ||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||
| 645 | Please include scope C<< "openid" >> | ||||||||||||||||||||||||||||
| 646 | |||||||||||||||||||||||||||||
| 647 | cf. L<https://openid.net/specs/openid-connect-basic-1_0.html#Scopes> | ||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||
| 649 | =item C<< authorize_params >> | ||||||||||||||||||||||||||||
| 650 | |||||||||||||||||||||||||||||
| 651 | Hash reference of parameters (values must be strings) that are added to | ||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||
| 653 | the authorization url. Empty by default | ||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||
| 655 | e.g. C<< { prompt => "login", "kc_idp_hint" => "orcid" } >> | ||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||
| 657 | Note that some parameters are set internally | ||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||
| 659 | and therefore will have no effect: | ||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||
| 661 | =over 6 | ||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||
| 663 | =item C<< code_challenge >> | ||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||
| 665 | =item C<< code_challenge_method >> | ||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||
| 667 | =item C<< state >> | ||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||
| 669 | =item C<< scope >> | ||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||
| 671 | =item C<< client_id >> | ||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||
| 673 | =item C<< response_type >> | ||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||
| 675 | =item C<< redirect_uri >> | ||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||
| 677 | =back | ||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||
| 679 | =item C<< allowed_authorize_params >> | ||||||||||||||||||||||||||||
| 680 | |||||||||||||||||||||||||||||
| 681 | Array reference of parameter names. | ||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||
| 683 | When constructing the authorization url, | ||||||||||||||||||||||||||||
| 684 | |||||||||||||||||||||||||||||
| 685 | these parameters are copied from the current url query | ||||||||||||||||||||||||||||
| 686 | |||||||||||||||||||||||||||||
| 687 | to the authorization url. This allows to add some | ||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||
| 689 | dynamic configuration, but should be used with caution. | ||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||
| 691 | Note that parameters from C<< authorize_params >> always | ||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||
| 693 | take precedence. | ||||||||||||||||||||||||||||
| 694 | |||||||||||||||||||||||||||||
| 695 | =item C<< uid_key >> | ||||||||||||||||||||||||||||
| 696 | |||||||||||||||||||||||||||||
| 697 | Attribute from claims to be used as uid | ||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||
| 699 | Note that all claims are also stored in C<< $session->get("auth_sso")->{info} >> | ||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||
| 701 | =item C<< store_oidc_response >> | ||||||||||||||||||||||||||||
| 702 | |||||||||||||||||||||||||||||
| 703 | Store C<< content >> and C<< content_type >> of returned OIDC response in session key `auth_sso.response.content` and `auth_sso.response.content_type` | ||||||||||||||||||||||||||||
| 704 | respectively. This can exhaust the cookie length if all session data is stored in the cookie. | ||||||||||||||||||||||||||||
| 705 | |||||||||||||||||||||||||||||
| 706 | Default: C<< 1 >> | ||||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||
| 708 | =back | ||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||
| 710 | =head1 HOW IT WORKS | ||||||||||||||||||||||||||||
| 711 | |||||||||||||||||||||||||||||
| 712 | =over 4 | ||||||||||||||||||||||||||||
| 713 | |||||||||||||||||||||||||||||
| 714 | =item the openid configuration is retrieved from C<< {openid_uri} >> | ||||||||||||||||||||||||||||
| 715 | |||||||||||||||||||||||||||||
| 716 | =over 6 | ||||||||||||||||||||||||||||
| 717 | |||||||||||||||||||||||||||||
| 718 | =item key C<< authorization_endpoint >> must be present in openid configuration | ||||||||||||||||||||||||||||
| 719 | |||||||||||||||||||||||||||||
| 720 | =item key C<< token_endpoint >> must be present in openid configuration | ||||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||
| 722 | =item key C<< jwks_uri >> must be present in openid configuration | ||||||||||||||||||||||||||||
| 723 | |||||||||||||||||||||||||||||
| 724 | =item the user is redirected to the authorization endpoint with extra query parameters | ||||||||||||||||||||||||||||
| 725 | |||||||||||||||||||||||||||||
| 726 | =back | ||||||||||||||||||||||||||||
| 727 | |||||||||||||||||||||||||||||
| 728 | =item after authentication at the authorization endpoint, the user is redirected back to this url with query parameters C<< code >> and C<< state >>. When something happened at the authorization endpoint, query parameters C<< error >> and C<< error_description >> are returned, and no C<< code >>. | ||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||
| 730 | =item C<< code >> is exchanged for a json string, using the token endpoint. This json string is a record that contains attributes like C<< id_token >> and C<< access_token >>. See L<https://openid.net/specs/openid-connect-core-1_0.html#TokenResponse> for more information. | ||||||||||||||||||||||||||||
| 731 | |||||||||||||||||||||||||||||
| 732 | =item key C<< id_token >> in the token json string contains three parts: | ||||||||||||||||||||||||||||
| 733 | |||||||||||||||||||||||||||||
| 734 | =over 6 | ||||||||||||||||||||||||||||
| 735 | |||||||||||||||||||||||||||||
| 736 | =item jwt jose header. Can be decoded with base64 into a json string | ||||||||||||||||||||||||||||
| 737 | |||||||||||||||||||||||||||||
| 738 | =item jwt payload. Can be decoded with base64 into a json string | ||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||
| 740 | =item jwt signature | ||||||||||||||||||||||||||||
| 741 | |||||||||||||||||||||||||||||
| 742 | =back | ||||||||||||||||||||||||||||
| 743 | |||||||||||||||||||||||||||||
| 744 | =item the jwt payload from the C<< id_token >> is decoded into a json string and then to a perl hash. All this data is stored C<< $session->{auth_sso}->{info} >>. One of these attributes will be the uid that will be stored at C<< $session->{auth_sso}->{uid} >>. This is determined by configuration key C<< uid_key >> (see above). e.g. "email" | ||||||||||||||||||||||||||||
| 745 | |||||||||||||||||||||||||||||
| 746 | =back | ||||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||
| 748 | =head1 NOTES | ||||||||||||||||||||||||||||
| 749 | |||||||||||||||||||||||||||||
| 750 | =over 4 | ||||||||||||||||||||||||||||
| 751 | |||||||||||||||||||||||||||||
| 752 | =item Can I reauthenticate when I visit the application? | ||||||||||||||||||||||||||||
| 753 | |||||||||||||||||||||||||||||
| 754 | When this Plack application is for example mounted at | ||||||||||||||||||||||||||||
| 755 | |||||||||||||||||||||||||||||
| 756 | C<< /auth/oidc >>, then you can reauthenticate by visiting | ||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||
| 758 | it again, but it depends on your configuration what actually | ||||||||||||||||||||||||||||
| 759 | |||||||||||||||||||||||||||||
| 760 | happens at the openid connect server. If C<< prompt >> is not | ||||||||||||||||||||||||||||
| 761 | |||||||||||||||||||||||||||||
| 762 | set anywhere (neither in C<< authorize_params >> nor in the | ||||||||||||||||||||||||||||
| 763 | |||||||||||||||||||||||||||||
| 764 | current url if that is allowed), then the external server | ||||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||
| 766 | will just sent you back with the same tokens. | ||||||||||||||||||||||||||||
| 767 | |||||||||||||||||||||||||||||
| 768 | Note that C<< session("auth_sso") >> is removed at the start | ||||||||||||||||||||||||||||
| 769 | |||||||||||||||||||||||||||||
| 770 | of every (re)authentication. | ||||||||||||||||||||||||||||
| 771 | |||||||||||||||||||||||||||||
| 772 | =back | ||||||||||||||||||||||||||||
| 773 | |||||||||||||||||||||||||||||
| 774 | =head1 LOGGING | ||||||||||||||||||||||||||||
| 775 | |||||||||||||||||||||||||||||
| 776 | All subclasses of L<Plack::Auth::SSO> use L<Log::Any> | ||||||||||||||||||||||||||||
| 777 | to log messages to the category that equals the current | ||||||||||||||||||||||||||||
| 778 | package name. | ||||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||
| 780 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 781 | |||||||||||||||||||||||||||||
| 782 | Nicolas Franck, C<< <nicolas.franck at ugent.be> >> | ||||||||||||||||||||||||||||
| 783 | |||||||||||||||||||||||||||||
| 784 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 785 | |||||||||||||||||||||||||||||
| 786 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 787 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 788 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 789 | |||||||||||||||||||||||||||||
| 790 | See L<http://dev.perl.org/licenses/> for more information. | ||||||||||||||||||||||||||||
| 791 | |||||||||||||||||||||||||||||
| 792 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 793 | |||||||||||||||||||||||||||||
| 794 | L<Plack::Auth::SSO> | ||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||
| 796 | =cut | ||||||||||||||||||||||||||||