GitHub-Apps-Auth
view release on metacpan or search on metacpan
# NAME
GitHub::Apps::Auth - The fetcher that get a token for GitHub Apps
# SYNOPSIS
use GitHub::Apps::Auth;
my $auth = GitHub::Apps::Auth->new(
private_key => "<filename>", # when read private key from file
private_key => \$pk, # when read private key from variable
app_id => <app_id>,
login => <organization or user>
);
# This method returns the cached token inside an object.
# However, refresh expired token automatically.
my $token = $auth->issued_token;
# If you want to use with Pithub
use Pithub;
# GitHub::Apps::Auth object behaves like a string.
GitHub::Apps::Auth is the fetcher for getting a GitHub token of GitHub Apps.
This module provides a way to get a token that need to be updated regularly for GitHub API.
# CONSTRUCTOR
## new
my $auth = GitHub::Apps::Auth->new(
private_key => "<filename>",
app_id => <app_id>,
installation_id => <installation_id>
);
Constructs an instance of `GitHub::Apps::Auth` from credentials.
### parameters
#### private\_key
lib/GitHub/Apps/Auth.pm view on Meta::CPAN
package GitHub::Apps::Auth;
use 5.008001;
use strict;
use warnings;
our $VERSION = "0.04";
use Class::Accessor::Lite (
rw => [qw/token expires installation_id/],
ro => [qw/_furl private_key app_id/],
);
use Carp;
use Crypt::PK::RSA;
use Crypt::JWT qw/encode_jwt/;
use Furl;
use JSON qw/decode_json/;
use Time::Moment;
sub _lazy(&) {
lib/GitHub/Apps/Auth.pm view on Meta::CPAN
"\"\"" => sub { shift->issued_token },
"." => sub {
my ($self, $other, $reverse) = @_;
return $reverse ?
_lazy { "$other" . "$self" } :
_lazy { "$self" . "$other" };
};
sub new {
my ($class, %args) = @_;
if (!exists $args{private_key} || !$args{private_key}) {
croak "private_key is required.";
}
if (!exists $args{app_id} || !$args{app_id}) {
croak "app_id is required.";
}
if (!$args{installation_id} && !$args{login}) {
croak "must be set installation_id or login.";
}
my $pk = Crypt::PK::RSA->new($args{private_key});
my $klass = {
private_key => $pk,
installation_id => $args{installation_id},
app_id => $args{app_id},
expires => 0,
_furl => Furl->new,
};
my $self = bless $klass, $class;
if (!$self->installation_id) {
my $installations = $self->installations;
if (!exists $installations->{$args{login}}) {
lib/GitHub/Apps/Auth.pm view on Meta::CPAN
sub _generate_jwt {
my $self = shift;
my $jwt = encode_jwt(
payload => {
iat => time(),
exp => time() + 60,
iss => $self->app_id,
},
alg => "RS256",
key => $self->private_key,
);
return $jwt;
}
sub _generate_request_header {
my $self = shift;
my $jwt = $self->_generate_jwt();
return [
lib/GitHub/Apps/Auth.pm view on Meta::CPAN
=encoding utf-8
=head1 NAME
GitHub::Apps::Auth - The fetcher that get a token for GitHub Apps
=head1 SYNOPSIS
use GitHub::Apps::Auth;
my $auth = GitHub::Apps::Auth->new(
private_key => "<filename>", # when read private key from file
private_key => \$pk, # when read private key from variable
app_id => <app_id>,
login => <organization or user>
);
# This method returns the cached token inside an object.
# However, refresh expired token automatically.
my $token = $auth->issued_token;
# If you want to use with Pithub
use Pithub;
# GitHub::Apps::Auth object behaves like a string.
lib/GitHub/Apps/Auth.pm view on Meta::CPAN
GitHub::Apps::Auth is the fetcher for getting a GitHub token of GitHub Apps.
This module provides a way to get a token that need to be updated regularly for GitHub API.
=head1 CONSTRUCTOR
=head2 new
my $auth = GitHub::Apps::Auth->new(
private_key => "<filename>",
app_id => <app_id>,
installation_id => <installation_id>
);
Constructs an instance of C<GitHub::Apps::Auth> from credentials.
=head3 parameters
=head4 private_key
B<Required: true>
This parameter is a private key of the GitHub Apps.
This must be a filename or string in the pem format. You can get a private key from Settings page of GitHub Apps. See L<Generating a private key|https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/#generating-a-priv...
=head4 app_id
B<Required: true>
t/01_issued_token.t view on Meta::CPAN
use GitHub::Apps::Auth;
use Crypt::PK::RSA;
use JSON qw/encode_json/;
use Time::Moment;
use Furl::Response;
my $pk = Crypt::PK::RSA->new->generate_key->export_key_pem("private");
my $auth = GitHub::Apps::Auth->new(
private_key => \$pk,
app_id => 42,
installation_id => 4242,
);
my $expected_base_token = 1234567890;
my $g = mock_guard "GitHub::Apps::Auth" => {
_post_to_access_token => sub {
my $body = encode_json({
token => $expected_base_token++ . "",
expires_at => Time::Moment->from_epoch(time())->plus_minutes(1)->strftime("%Y-%m-%dT%H:%M:%S%Z"),
t/02_overload.t view on Meta::CPAN
use GitHub::Apps::Auth;
use Crypt::PK::RSA;
use JSON qw/encode_json/;
use Time::Moment;
use Furl::Response;
my $pk = Crypt::PK::RSA->new->generate_key->export_key_pem("private");
my $auth = GitHub::Apps::Auth->new(
private_key => \$pk,
app_id => 42,
installation_id => 4242,
);
my $expected_base_token = 1234567890;
my $g = mock_guard "GitHub::Apps::Auth" => {
_post_to_access_token => sub {
my $body = encode_json({
token => $expected_base_token++ . "",
expires_at => Time::Moment->from_epoch(time())->plus_minutes(1)->strftime("%Y-%m-%dT%H:%M:%S%Z"),
t/03_join.t view on Meta::CPAN
use GitHub::Apps::Auth;
use Crypt::PK::RSA;
use JSON qw/encode_json/;
use Time::Moment;
use Furl::Response;
my $pk = Crypt::PK::RSA->new->generate_key->export_key_pem("private");
my $auth = GitHub::Apps::Auth->new(
private_key => \$pk,
app_id => 42,
installation_id => 4242,
);
my $expected_base_token = 1234567890;
my $g = mock_guard "GitHub::Apps::Auth" => {
_post_to_access_token => sub {
my $body = encode_json({
token => $expected_base_token++ . "",
expires_at => Time::Moment->from_epoch(time())->plus_minutes(1)->strftime("%Y-%m-%dT%H:%M:%S%Z"),
t/04_installations.t view on Meta::CPAN
my $g = mock_guard "GitHub::Apps::Auth" => {
installations => sub {
return {
foobar => 4242,
},
},
};
my $auth = GitHub::Apps::Auth->new(
private_key => \$pk,
app_id => 42,
login => "foobar",
);
is $auth->installation_id, 4242;
done_testing;
( run in 1.138 second using v1.01-cache-2.11-cpan-a5abf4f5562 )