Amon2-Auth-Site-Google

 view release on metacpan or  search on metacpan

lib/Amon2/Auth/Site/Google.pm  view on Meta::CPAN

has scope => (
    is      => 'ro',
    isa     => 'ArrayRef',
    default => sub { [qw(https://www.googleapis.com/auth/userinfo.profile)] },
);

has user_info => (
    is => 'rw',
    isa => 'Bool',
    default => 1,
);

has authorize_url => (
    is      => 'ro',
    isa     => 'Str',
    default => 'https://accounts.google.com/o/oauth2/auth',
);

has token_url => (
    is      => 'ro',
    isa     => 'Str',
    default => 'https://accounts.google.com/o/oauth2/token',
);

has token_info_url => (
    is      => 'ro',
    isa     => 'Str',
    default => 'https://www.googleapis.com/oauth2/v1/tokeninfo',
);

has user_info_url => (
    is      => 'ro',
    isa     => 'Str',
    default => 'https://www.googleapis.com/oauth2/v1/userinfo',
);

has ua => (
    is => 'ro',
    isa => 'LWP::UserAgent',
    lazy => 1,
    default => sub {
        LWP::UserAgent->new(agent => "Amon2::Auth::Site::Google/$VERSION");
    },
);

no Mouse;
__PACKAGE__->meta->make_immutable;

sub moniker { 'google' }

sub auth_uri {
    my ($self, $c, $callback_uri) = @_;

    my $uri = URI->new($self->authorize_url);
    $uri->query_form(+{
        client_id       => $self->client_id,
        scope           => $self->scope,
        redirect_uri    => $callback_uri,
        response_type   => 'code',
        access_type     => 'offline',
        approval_prompt => 'force',
    });
    return $uri->as_string;
}

sub callback {
    my ($self, $c, $callback) = @_;

    my $res = $self->ua->post($self->token_url, +{
        client_id     => $self->client_id,
        client_secret => $self->client_secret,
        code          => $c->req->param('code'),
        redirect_uri  => $self->redirect_url,
        grant_type    => 'authorization_code',
    });
    $res->is_success or do {
        warn $res->decoded_content;
        return $callback->{on_error}->($res->decoded_content);
    };
    my $token = decode_json $res->content;

    my $uri = URI->new($self->token_info_url);
    $uri->query_form(+{ access_token => $token->{access_token} });
    $res = $self->ua->get($uri->as_string);
    my $token_info = decode_json $res->content;
    $res->is_success or do {
        warn $res->decoded_content;
        return $callback->{on_error}->($res->decoded_content);
    };
    $token_info->{audience} eq $self->client_id or do {
        warn 'invalid token';
        return $callback->{on_error}->('invalid token');
    };
    my @args = ($token->{access_token}, $token->{refresh_token});

    if ($self->user_info) {
        my $uri = URI->new($self->user_info_url);
        $uri->query_form(+{ access_token => $token->{access_token} });
        my $res = $self->ua->get($uri->as_string);
        $res->is_success or do {
            warn $res->decoded_content;
            return $callback->{on_error}->($res->decoded_content);
        };
        my $user = decode_json $res->content;
        push @args, $user;
    }

    $callback->{on_finished}->(@args);
}

1;
__END__

=encoding utf-8

=head1 NAME

Amon2::Auth::Site::Google - Google auth integration for Amon2

=head1 SYNOPSIS



( run in 1.878 second using v1.01-cache-2.11-cpan-6aa56a78535 )