| 1 | package Browser::OIDC; | ||
| 2 | |||
| 3 | use 5.020; | ||
| 4 | use warnings; | ||
| 5 | use experimental qw/signatures postderef lexical_subs/; | ||
| 6 | |||
| 7 | use Carp; | ||
| 8 | use List::Util 1.33 qw/first none/; | ||
| 9 | use MIME::Base64 qw/encode_base64url/; | ||
| 10 | use Mojo::URL; | ||
| 11 | use Mojo::Util qw/random_bytes/; | ||
| 12 | |||
| 13 | use Browser::OAuth2; | ||
| 14 | use Browser::OIDC::TokenResponse; | ||
| 15 | |||
| 16 | our $VERSION = '0.005'; | ||
| 17 | |||
| 18 | my $ua = Mojo::UserAgent->new; | ||
| 19 | |||
| 20 | my sub validate_url($name, $url) { | ||
| 21 | croak "$name is undefined" unless defined $url; | ||
| 22 | my $uri = Mojo::URL->new($url); | ||
| 23 | croak "Invalid URL for $name" unless ($uri->scheme // '') eq 'https' && length $uri->host; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub new_from_discovery($class, $target_base) { | ||
| 27 | validate_url('target_base', $target_base); | ||
| 28 | my $response = $ua->get("$target_base/.well-known/openid-configuration")->result; | ||
| 29 | croak "Error: ". $response->code if not $response->is_success; | ||
| 30 | my $config = $response->json; | ||
| 31 | croak "Error: non-matching issuer" if $config->{issuer} ne $target_base; | ||
| 32 | return $class->new_from_config($config); | ||
| 33 | } | ||
| 34 | |||
| 35 | sub new_from_config($class, $config) { | ||
| 36 | validate_url($_, $config->{$_}) for qw/issuer authorization_endpoint token_endpoint jwks_uri/; | ||
| 37 | defined $config->{$_} && validate_url($_, $config->{$_}) for qw/userinfo_endpoint/; | ||
| 38 | my $auth_methods_supported = $config->{token_endpoint_auth_methods_supported} // ['client_secret_basic']; | ||
| 39 | my %auth_methods_supported = map { $_ => 1 } $auth_methods_supported->@*; | ||
| 40 | my $auth_method = first { $auth_methods_supported{$_} } qw/client_secret_post client_secret_basic none/; | ||
| 41 | croak("Don't know how to authenticate token") unless $auth_method; | ||
| 42 | my $grant_types_supported = $config->{grant_types_supported} // ['authorization_code']; | ||
| 43 | croak "Only Authorization Code Flow is supported yet" if none { $_ eq 'authorization_code' } $grant_types_supported->@*; | ||
| 44 | my $response_types_supported = $config->{response_types_supported} // ['code']; | ||
| 45 | croak "code is required for authorization code flow" if none { $_ eq 'code' } $response_types_supported->@*; | ||
| 46 | |||
| 47 | return bless { | ||
| 48 | $config->%{qw/issuer authorization_endpoint token_endpoint userinfo_endpoint claims_supported scopes_supported jwks_uri/}, | ||
| 49 | auth_method => $auth_method, | ||
| 50 | response_iss => !!$config->{authorization_response_iss_parameter_supported}, | ||
| 51 | ua => $ua, | ||
| 52 | }, $class; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub get_token($self, %options) { | ||
| 56 | my $oauth2 = Browser::OAuth2->new( | ||
| 57 | delete %options{'client_id', 'client_secret'}, | ||
| 58 | auth_method => $self->{auth_method}, | ||
| 59 | ); | ||
| 60 | |||
| 61 | unshift $options{scope}->@*, 'openid'; | ||
| 62 | $options{nonce} = encode_base64url(random_bytes(32)) if not exists $options{nonce}; | ||
| 63 | $options{response_iss} = $self->{issuer} if $self->{response_iss}; | ||
| 64 | |||
| 65 | my $payload = $oauth2->get_authorization($self->{authorization_endpoint}, %options); | ||
| 66 | my $response = $oauth2->get_token($self->{token_endpoint}, $payload->%*); | ||
| 67 | return Browser::OIDC::TokenResponse->new(parent => $self, value => $response, nonce => $options{nonce}); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub issuer($self) { | ||
| 71 | return $self->{issuer}; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub claims_supported($self) { | ||
| 75 | return ($self->{claims_supported} // [])->@*; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub scopes_supported($self) { | ||
| 79 | return ($self->{scopes_supported} // [])->@*; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub jwks($self) { | ||
| 83 | return $self->{jwks} //= do { | ||
| 84 | my $response = $ua->get($self->{jwks_uri})->result; | ||
| 85 | croak "Could not fetch JSON Web Key Set" if not $response->is_success; | ||
| 86 | $response->json; | ||
| 87 | }; | ||
| 88 | } | ||
| 89 | |||
| 90 | 1; | ||
| 91 | |||
| 92 | __END__ |