| 1 | package Browser::OAuth2; | ||
| 2 | |||
| 3 | use 5.020; | ||
| 4 | use warnings; | ||
| 5 | use experimental qw/signatures postderef lexical_subs/; | ||
| 6 | |||
| 7 | use Browser::Open 'open_browser'; | ||
| 8 | use Carp; | ||
| 9 | use Digest::SHA 'sha256'; | ||
| 10 | use MIME::Base64 qw/encode_base64 encode_base64url/; | ||
| 11 | use Mojo::Server::Daemon; | ||
| 12 | use Mojo::URL; | ||
| 13 | use Mojo::UserAgent; | ||
| 14 | use Mojo::Util qw/encode random_bytes url_escape xml_escape/; | ||
| 15 | |||
| 16 | my $ua = Mojo::UserAgent->new; | ||
| 17 | |||
| 18 | sub new($class, %options) { | ||
| 19 | my $client_id = $options{client_id} // croak 'No client id given'; | ||
| 20 | return bless { | ||
| 21 | %options{'client_id', 'client_secret'}, | ||
| 22 | auth_method => $options{auth_method} // 'client_secret_basic', | ||
| 23 | }, $class; | ||
| 24 | } | ||
| 25 | |||
| 26 | my sub form_escape($input) { | ||
| 27 | return url_escape(encode('UTF-8', $input)) =~ s/%20/+/gr; | ||
| 28 | } | ||
| 29 | |||
| 30 | my sub term_escape($input) { | ||
| 31 | return $input =~ s/[[:cntrl:]]//gr; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub get_authorization($self, $authorization_endpoint, %options) { | ||
| 35 | my $state = encode_base64url(random_bytes(16)); | ||
| 36 | |||
| 37 | my $ioloop = Mojo::IOLoop->new; | ||
| 38 | my $daemon = Mojo::Server::Daemon->new(ioloop => $ioloop, listen => ['http://localhost:0'], silent => 1); | ||
| 39 | my ($code, $exception); | ||
| 40 | $daemon->on(request => sub ($daemon, $tx) { | ||
| 41 | my $request = $tx->req; | ||
| 42 | my $parameters = $request->method eq 'POST' ? Mojo::Parameters->new($request->body) : $request->url->query; | ||
| 43 | |||
| 44 | if ($request->url->path eq '/auth/callback') { | ||
| 45 | my $received_state = $parameters->param('state'); | ||
| 46 | if ($received_state ne $state) { | ||
| 47 | $tx->res->code(400); | ||
| 48 | $tx->res->body('Invalid state'); | ||
| 49 | $tx->resume; | ||
| 50 | } elsif (defined $options{response_iss} and $parameters->param('iss') ne $options{response_iss}) { | ||
| 51 | $tx->res->code(400); | ||
| 52 | $tx->res->body('Incorrect issuer'); | ||
| 53 | $tx->resume; | ||
| 54 | } elsif (my $error = $parameters->param('error')) { | ||
| 55 | my $message = $parameters->param('error_description') // $error; | ||
| 56 | $tx->res->code(400); | ||
| 57 | $tx->res->body(xml_escape($message)); | ||
| 58 | $exception = term_escape($message); | ||
| 59 | $tx->on(finish => sub { $ioloop->stop }); | ||
| 60 | $tx->resume; | ||
| 61 | } elsif (defined($code = $parameters->param('code'))) { | ||
| 62 | $tx->res->code(200); | ||
| 63 | $tx->res->headers->content_type($options{message_type}) if $options{message_type}; | ||
| 64 | $tx->res->body($options{message} // 'Authorization token received, you can close this now'); | ||
| 65 | $tx->on(finish => sub { $ioloop->stop }); | ||
| 66 | $tx->resume; | ||
| 67 | } else { | ||
| 68 | $tx->res->code(400); | ||
| 69 | $tx->res->body('Missing code'); | ||
| 70 | $tx->resume; | ||
| 71 | return; | ||
| 72 | } | ||
| 73 | } else { | ||
| 74 | $tx->res->code(400); | ||
| 75 | $tx->resume; | ||
| 76 | } | ||
| 77 | }); | ||
| 78 | $daemon->start; | ||
| 79 | |||
| 80 | my ($port) = $daemon->ports->@*; | ||
| 81 | my $redirect_uri = "http://localhost:$port/auth/callback"; | ||
| 82 | my $full_target = Mojo::URL->new($authorization_endpoint); | ||
| 83 | $full_target->query->append(response_type => 'code'); | ||
| 84 | $full_target->query->append(redirect_uri => $redirect_uri); | ||
| 85 | $full_target->query->append(client_id => $self->{client_id}); | ||
| 86 | my $code_verifier = encode_base64url(random_bytes(32)); | ||
| 87 | $full_target->query->append(code_challenge => encode_base64url(sha256($code_verifier))); | ||
| 88 | $full_target->query->append(code_challenge_method => 'S256'); | ||
| 89 | $full_target->query->append(state => $state); | ||
| 90 | for my $name (qw/response_mode nonce display max_age/) { | ||
| 91 | $full_target->query->append($name => $options{$name}) if defined $options{$name}; | ||
| 92 | } | ||
| 93 | for my $name (qw/scope prompt/) { | ||
| 94 | $full_target->query->append($name => join ' ', $options{$name}->@*) if defined $options{$name}; | ||
| 95 | } | ||
| 96 | |||
| 97 | my $timeout = $options{timeout} // 60; | ||
| 98 | my $timer = $ioloop->timer($timeout, sub { | ||
| 99 | $exception = 'Timeout'; | ||
| 100 | $ioloop->stop; | ||
| 101 | }); | ||
| 102 | |||
| 103 | my $ok = open_browser($full_target); | ||
| 104 | say "Could not open browser: please open $full_target manually" if not defined $ok or $ok != 0; | ||
| 105 | |||
| 106 | $ioloop->start; | ||
| 107 | $ioloop->remove($timer); | ||
| 108 | croak $exception if $exception; | ||
| 109 | croak 'No authorization code received' unless defined $code; | ||
| 110 | |||
| 111 | return { | ||
| 112 | grant_type => 'authorization_code', | ||
| 113 | code => $code, | ||
| 114 | code_verifier => $code_verifier, | ||
| 115 | redirect_uri => $redirect_uri, | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | sub get_token($self, $token_endpoint, %payload) { | ||
| 120 | my %headers; | ||
| 121 | if ($self->{auth_method} eq 'client_secret_post') { | ||
| 122 | $payload{client_id} = $self->{client_id}; | ||
| 123 | $payload{client_secret} = $self->{client_secret}; | ||
| 124 | } elsif ($self->{auth_method} eq 'client_secret_basic') { | ||
| 125 | my $auth = join ':', form_escape($self->{client_id}), form_escape($self->{client_secret}); | ||
| 126 | $headers{Authorization} = 'Basic ' . encode_base64($auth, ''); | ||
| 127 | } else { | ||
| 128 | $payload{client_id} = $self->{client_id}; | ||
| 129 | } | ||
| 130 | my $response = $ua->post($token_endpoint, \%headers, form => \%payload)->result; | ||
| 131 | if ($response->is_success) { | ||
| 132 | return $response->json; | ||
| 133 | } else { | ||
| 134 | croak sprintf "Could not get token (%s): %s", $response->code , term_escape($response->body); | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | 1; | ||
| 139 | |||
| 140 | __END__ |