| 1 | package Browser::OIDC::TokenResponse; | ||
| 2 | |||
| 3 | use 5.020; | ||
| 4 | use warnings; | ||
| 5 | use experimental qw/signatures postderef lexical_subs/; | ||
| 6 | |||
| 7 | use Carp; | ||
| 8 | use Mojo::Util 'term_escape'; | ||
| 9 | |||
| 10 | our $VERSION = '0.005'; | ||
| 11 | |||
| 12 | sub new($class, %options) { | ||
| 13 | return bless { | ||
| 14 | parent => $options{parent}, | ||
| 15 | value => $options{value}, | ||
| 16 | nonce => $options{nonce}, | ||
| 17 | }, $class; | ||
| 18 | } | ||
| 19 | |||
| 20 | for my $name (qw/id_token access_token expires_in token_type/) { | ||
| 21 | my $sub = sub($self) { return $self->{value}{$name} }; | ||
| 22 | no strict 'refs'; | ||
| 23 | *$name = $sub; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub nonce($self) { | ||
| 27 | return $self->{nonce}; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub userinfo($self) { | ||
| 31 | my $parent = $self->{parent}; | ||
| 32 | my $url = $parent->{userinfo_endpoint} // croak 'No userinfo URL'; | ||
| 33 | my $authorization = join ' ', $self->{value}{token_type}, $self->{value}{access_token}; | ||
| 34 | my $response = $parent->{ua}->get($parent->{userinfo_endpoint}, { Authorization => $authorization })->result; | ||
| 35 | croak "Could not fetch userinfo " . term_escape($response->message) if not $response->is_success; | ||
| 36 | return $response->json; | ||
| 37 | } | ||
| 38 | |||
| 39 | 1; | ||
| 40 | |||
| 41 | =head1 SYNOPSIS | ||
| 42 | |||
| 43 | my $oidc = Browser::OIDC->new($url); | ||
| 44 | my $token_response = $oidc->get_token(%args); | ||
| 45 | my $id_token = $token_response->id_token; | ||
| 46 | |||
| 47 | =head1 DESCRIPTION | ||
| 48 | |||
| 49 | This represents a token as returned by C<Browser::OIDC->get_token>. It should always contain an ID token, and usually an access token as well. | ||
| 50 | |||
| 51 | =head1 METHODS | ||
| 52 | |||
| 53 | =head2 id_token | ||
| 54 | |||
| 55 | This returns the raw ID token. This should be JWT formatted and is not validated. | ||
| 56 | |||
| 57 | =head2 access_token | ||
| 58 | |||
| 59 | This returns the raw access token. | ||
| 60 | |||
| 61 | =head2 expires_in | ||
| 62 | |||
| 63 | The amount of time from fetching until the access token expires, in seconds. | ||
| 64 | |||
| 65 | =head2 token_type | ||
| 66 | |||
| 67 | The type of the token. This will usually be C<'bearer'>. | ||
| 68 | |||
| 69 | =head2 nonce | ||
| 70 | |||
| 71 | The nonce used for the token request. | ||
| 72 | |||
| 73 | =head2 userinfo | ||
| 74 | |||
| 75 | This fetches the userinfo associated with the access token. |