GitHub-Authorization
view release on metacpan or search on metacpan
lib/GitHub/Authorization.pm view on Meta::CPAN
#
# This file is part of GitHub-Authorization
#
# This software is Copyright (c) 2012 by Chris Weyl.
#
# This is free software, licensed under:
#
# The GNU Lesser General Public License, Version 2.1, February 1999
#
package GitHub::Authorization;
{
$GitHub::Authorization::VERSION = '0.001';
}
# ABSTRACT: Generate a GitHub OAuth2 non-web authorization token
use strict;
use warnings;
use Carp 'confess';
use autobox::JSON;
use HTTP::Tiny;
use MIME::Base64;
use Params::Validate ':all';
# for SSL and SSL CA verification
use IO::Socket::SSL 1.56;
use Mozilla::CA;
use namespace::clean;
use Sub::Exporter::Progressive -setup => {
exports => [ qw{ is_legal_scope legal_scopes get_gh_token } ],
};
# debugging...
#use Smart::Comments '###';
sub _default_agent {
'GitHub::Authorization/'
. (__PACKAGE__->VERSION || 0)
. q{ }
}
sub _url { 'https://api.github.com' . shift }
sub get_gh_token {
my %_opt = ( type => SCALAR | UNDEF, optional => 1 );
my %args = validate @_ => {
user => { type => SCALAR, regex => qr/^[A-Za-z0-9\.@]+$/ },
password => { type => SCALAR },
scopes => { type => ARRAYREF, default => [ ] },
# optional args
note => { %_opt },
note_url => { %_opt },
client_id => { %_opt, regex => qr/^[a-f0-9]{20}$/ },
client_secret => { %_opt, regex => qr/^[a-f0-9]{40}$/ },
};
my ($user, $password, $scopes) = delete @args{qw{user password scopes}};
$scopes ||= [];
my @illegal =
map { "illegal_scope: $_" }
grep { ! is_legal_scope($_) }
@$scopes;
confess "Bad scopes: @illegal"
if @illegal;
$args{scopes} = $scopes
if @$scopes;
# now, to the real stuff
my $ua = HTTP::Tiny->new(
agent => _default_agent,
verify_SSL => 1,
);
my $url = _url('/authorizations');
my $hash = MIME::Base64::encode_base64("$user:$password", '');
my $headers = { Authorization => 'Basic ' . $hash };
( run in 1.288 second using v1.01-cache-2.11-cpan-39bf76dae61 )