| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 1 |  |  | 1 |  | 1227 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 39 |  | 
| 2 | 1 |  |  | 1 |  | 6 | use warnings; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 34 |  | 
| 3 | 1 |  |  | 1 |  | 6 | use utf8; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 7 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | package Amon2::Auth::Site::Github; | 
| 6 | 1 |  |  | 1 |  | 36 | use Mouse; | 
|  | 1 |  |  |  |  | 10 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 1 |  |  | 1 |  | 392 | use Amon2::Auth; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 23 |  | 
| 9 | 1 |  |  | 1 |  | 5 | use LWP::UserAgent; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 29 |  | 
| 10 | 1 |  |  | 1 |  | 8 | use JSON; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 9 |  | 
| 11 | 1 |  |  | 1 |  | 509 | use Amon2::Auth::Util qw(parse_content); | 
|  | 1 |  |  |  |  | 26 |  | 
|  | 1 |  |  |  |  | 2316 |  | 
| 12 |  |  |  |  |  |  | our $VERSION = '0.05'; | 
| 13 |  |  |  |  |  |  |  | 
| 14 | 0 |  |  | 0 | 0 |  | sub moniker { 'github' } | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | has client_id => ( | 
| 17 |  |  |  |  |  |  | is => 'ro', | 
| 18 |  |  |  |  |  |  | isa => 'Str', | 
| 19 |  |  |  |  |  |  | required => 1, | 
| 20 |  |  |  |  |  |  | ); | 
| 21 |  |  |  |  |  |  | has client_secret => ( | 
| 22 |  |  |  |  |  |  | is => 'ro', | 
| 23 |  |  |  |  |  |  | isa => 'Str', | 
| 24 |  |  |  |  |  |  | required => 1, | 
| 25 |  |  |  |  |  |  | ); | 
| 26 |  |  |  |  |  |  | has scope => ( | 
| 27 |  |  |  |  |  |  | is => 'ro', | 
| 28 |  |  |  |  |  |  | isa => 'Str', | 
| 29 |  |  |  |  |  |  | ); | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | has user_info => ( | 
| 32 |  |  |  |  |  |  | is => 'rw', | 
| 33 |  |  |  |  |  |  | isa => 'Bool', | 
| 34 |  |  |  |  |  |  | default => 1, | 
| 35 |  |  |  |  |  |  | ); | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | has ua => ( | 
| 38 |  |  |  |  |  |  | is => 'ro', | 
| 39 |  |  |  |  |  |  | isa => 'LWP::UserAgent', | 
| 40 |  |  |  |  |  |  | lazy => 1, | 
| 41 |  |  |  |  |  |  | default => sub { | 
| 42 |  |  |  |  |  |  | my $ua = LWP::UserAgent->new(agent => "Amon2::Auth/$Amon2::Auth::VERSION"); | 
| 43 |  |  |  |  |  |  | }, | 
| 44 |  |  |  |  |  |  | ); | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | has authorize_url => ( | 
| 47 |  |  |  |  |  |  | is => 'ro', | 
| 48 |  |  |  |  |  |  | isa => 'Str', | 
| 49 |  |  |  |  |  |  | default => 'https://github.com/login/oauth/authorize', | 
| 50 |  |  |  |  |  |  | ); | 
| 51 |  |  |  |  |  |  | has access_token_url => ( | 
| 52 |  |  |  |  |  |  | is => 'ro', | 
| 53 |  |  |  |  |  |  | isa => 'Str', | 
| 54 |  |  |  |  |  |  | default => 'https://github.com/login/oauth/access_token', | 
| 55 |  |  |  |  |  |  | ); | 
| 56 |  |  |  |  |  |  | has redirect_url => ( | 
| 57 |  |  |  |  |  |  | is => 'ro', | 
| 58 |  |  |  |  |  |  | isa => 'Str', | 
| 59 |  |  |  |  |  |  | ); | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | sub auth_uri { | 
| 62 | 0 |  |  | 0 | 0 |  | my ($self, $c, $callback_uri) = @_; | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 0 |  |  |  |  |  | my $redirect_uri = URI->new($self->authorize_url); | 
| 65 | 0 |  |  |  |  |  | my %params; | 
| 66 | 0 | 0 |  |  |  |  | if (defined $callback_uri) { | 
|  |  | 0 |  |  |  |  |  | 
| 67 | 0 |  |  |  |  |  | $params{redirect_uri} = $callback_uri; | 
| 68 |  |  |  |  |  |  | } elsif (defined $self->redirect_url) { | 
| 69 | 0 |  |  |  |  |  | $params{redirect_uri} = $self->redirect_url; | 
| 70 |  |  |  |  |  |  | } | 
| 71 | 0 |  |  |  |  |  | for (qw(client_id scope)) { | 
| 72 | 0 | 0 |  |  |  |  | next unless defined $self->$_; | 
| 73 | 0 |  |  |  |  |  | $params{$_} = $self->$_; | 
| 74 |  |  |  |  |  |  | } | 
| 75 | 0 |  |  |  |  |  | $redirect_uri->query_form(%params); | 
| 76 | 0 |  |  |  |  |  | return $redirect_uri->as_string; | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | sub callback { | 
| 80 | 0 |  |  | 0 | 0 |  | my ($self, $c, $callback) = @_; | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 0 | 0 |  |  |  |  | my $code = $c->req->param('code') or die "Cannot get a 'code' parameter"; | 
| 83 | 0 |  |  |  |  |  | my %params = (code => $code); | 
| 84 | 0 |  |  |  |  |  | $params{client_id} = $self->client_id; | 
| 85 | 0 |  |  |  |  |  | $params{client_secret} = $self->client_secret; | 
| 86 | 0 | 0 |  |  |  |  | $params{redirect_url} = $self->redirect_url if defined $self->redirect_url; | 
| 87 | 0 |  |  |  |  |  | my $res = $self->ua->post($self->access_token_url, \%params); | 
| 88 | 0 | 0 |  |  |  |  | $res->is_success or die "Cannot authenticate"; | 
| 89 | 0 |  |  |  |  |  | my $dat = parse_content($res->decoded_content); | 
| 90 | 0 | 0 |  |  |  |  | if (my $err = $dat->{error}) { | 
| 91 | 0 |  |  |  |  |  | return $callback->{on_error}->($err); | 
| 92 |  |  |  |  |  |  | } | 
| 93 | 0 | 0 |  |  |  |  | my $access_token = $dat->{access_token} or die "Cannot get a access_token"; | 
| 94 | 0 |  |  |  |  |  | my @args = ($access_token); | 
| 95 | 0 | 0 |  |  |  |  | if ($self->user_info) { | 
| 96 | 0 |  |  |  |  |  | my $res = $self->ua->get("https://api.github.com/user?oauth_token=${access_token}"); | 
| 97 | 0 | 0 |  |  |  |  | $res->is_success or return $callback->{on_error}->($res->status_line); | 
| 98 | 0 |  |  |  |  |  | my $dat = decode_json($res->decoded_content); | 
| 99 | 0 |  |  |  |  |  | push @args, $dat; | 
| 100 |  |  |  |  |  |  | } | 
| 101 | 0 |  |  |  |  |  | return $callback->{on_finished}->( @args ); | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | 1; | 
| 105 |  |  |  |  |  |  | __END__ |