| 1 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | =head1 NAME | ||||||||||
| 5 | |||||||||||
| 6 | OAuth2::Google::Plus::UserInfo | ||||||||||
| 7 | |||||||||||
| 8 | =head1 DESCRIPTION | ||||||||||
| 9 | |||||||||||
| 10 | implements acallback to https://www.googleapis.com/oauth2/v2/userinfo, retrieving | ||||||||||
| 11 | the users google-id, email and verification status. | ||||||||||
| 12 | |||||||||||
| 13 | =head1 SYNOPSYS | ||||||||||
| 14 | |||||||||||
| 15 | use OAuth2::Google::Plus::UserInfo; | ||||||||||
| 16 | |||||||||||
| 17 | my $info = OAuth2::Google::Plus::UserInfo->new( access_token => $access_token ); | ||||||||||
| 18 | |||||||||||
| 19 | if( $info->is_success ) { | ||||||||||
| 20 | my $email = $info->email; | ||||||||||
| 21 | |||||||||||
| 22 | # ... | ||||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | =cut | ||||||||||
| 26 | |||||||||||
| 27 | { | ||||||||||
| 28 | package OAuth2::Google::Plus::UserInfo; | ||||||||||
| 29 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 1 | ▶ | use MooX::late; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | |||||||||||
| 32 | 1 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 1 | ▶ | use JSON qw|decode_json|; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | 1 | ▶ | use Carp::Assert; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 1 | ▶ | use URI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | |||||||||||
| 37 | 1 | has access_token => ( | |||||||||
| 38 | is => 'ro', | ||||||||||
| 39 | isa => 'Str', | ||||||||||
| 40 | required => 1, | ||||||||||
| 41 | ); | ||||||||||
| 42 | |||||||||||
| 43 | 1 | has _user_info => ( | |||||||||
| 44 | is => 'ro', | ||||||||||
| 45 | isa => 'HashRef', | ||||||||||
| 46 | lazy_build => 1, | ||||||||||
| 47 | ); | ||||||||||
| 48 | |||||||||||
| 49 | 1 | has response => ( | |||||||||
| 50 | is => 'ro', | ||||||||||
| 51 | writer => '_set_response', | ||||||||||
| 52 | ); | ||||||||||
| 53 | |||||||||||
| 54 | has _endpoint => ( | ||||||||||
| 55 | is => 'ro', | ||||||||||
| 56 | 0 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 57 | 0 | return 'https://www.googleapis.com/oauth2/v2/userinfo'; | |||||||||
| 58 | }, | ||||||||||
| 59 | 1 | ); | |||||||||
| 60 | |||||||||||
| 61 | 2 | ▶ | sub _build__user_info { | ||||||||
|
Subroutine
_build__user_info: called CC=2 SCAR=6.9
| |||||||||||
| 62 | 2 | my ( $self ) = @_; | |||||||||
| 63 | |||||||||||
| 64 | 2 | my $ua = LWP::UserAgent->new; | |||||||||
| 65 | 2 | $ua->default_header( Authorization => 'OAuth ' . $self->access_token ); | |||||||||
| 66 | |||||||||||
| 67 | 2 | my $response = $ua->get( $self->_endpoint ); | |||||||||
| 68 | 2 | $self->_set_response( $response ); | |||||||||
| 69 | |||||||||||
| 70 | 2 | ▶ | if( $response->is_success ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 1 | my $json = decode_json( $response->content ); | |||||||||
| 72 | 1 | return $json; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 1 | return {}; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 2 | ▶ | sub is_success { | ||||||||
|
Subroutine
is_success: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 79 | 2 | my ( $self ) = @_; | |||||||||
| 80 | 2 | return defined( $self->_user_info->{id} ); | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 2 | ▶ | sub email { | ||||||||
|
Subroutine
email: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 84 | 2 | my ( $self ) = @_; | |||||||||
| 85 | 2 | return $self->_user_info->{email}; | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 2 | ▶ | sub id { | ||||||||
|
Subroutine
id: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 89 | 2 | my ( $self ) = @_; | |||||||||
| 90 | 2 | return $self->_user_info->{id}; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 2 | ▶ | sub verified_email { | ||||||||
|
Subroutine
verified_email: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 94 | 2 | my ( $self ) = @_; | |||||||||
| 95 | 2 | return $self->_user_info->{verified_email}; | |||||||||
| 96 | } | ||||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | 1 | 1; | |||||||||