Skip to content

Commit

Permalink
Merge pull request #8 from upwork/v2.2.0
Browse files Browse the repository at this point in the history
feat(grant): add support of Client Credentials Grant
  • Loading branch information
mnovozhylov authored Jul 3, 2023
2 parents 960bd11 + 5e273b0 commit c1e4689
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
perl: [ '5.28', '5.30' ]
perl: [ '5.28', '5.30', '5.34' ]

name: Perl ${{ matrix.python }}
steps:
Expand Down
4 changes: 2 additions & 2 deletions Build.PL
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ my $builder = Module::Build->new(
module_name => 'Net::Upwork::API',
license => 'apache',
dist_author => 'Maksym Novozhylov <mnovozhilov@upwork.com>',
dist_version => '2.1.4',
dist_version => '2.2.0',
dist_abstract => 'Perl bindings for Upwork API (OAuth2)',
build_requires => {
'Test::More' => '0.66',
Expand All @@ -34,7 +34,7 @@ my $builder = Module::Build->new(
requires => {
'perl' => '5.8.8',
'IO::Socket::SSL' => '1.965',
'Net::OAuth2' => '0.63',
'Net::OAuth2' => '0.67',
},
meta_merge => { keywords => [qw( upwork oauth2 )], },
meta_add => {
Expand Down
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2.2.0
* Add support of Client Credentials Grant

## 2.1.4
* Add GraphQL support

Expand Down
6 changes: 5 additions & 1 deletion example/example.pl
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
'client_id' => 'xxxxxxxx',
'client_secret' => 'xxxxxxxx',
'redirect_uri' => 'https://your-call-back-url.here',
# 'grant_type' => 'client_credentials', # used for Client Credentials Grant
# 'access_token' => 'xxxxxxxx',
# 'refresh_token' => 'xxxxxxxx',
# 'refresh_token' => 'xxxxxxxx', # used by Code Authorization Grant
# 'expires_in' => 86399 # TTL. `expires_at` should be enough for basic usage but you may find this option useful for own needs
# 'expires_at' => 1234567890 # timestamp, either get from the Net::OAuth2::AccessToken object or set like time()+actual_expires_in
);

$api = Net::Upwork::API->new($config);
if (!$api->has_access_token()) {
# start Code Authorization Grant
my $authz_url = $api->get_authorization_url();

print "Visit the authorization url and provide oauth_verifier for further authorization\n";
Expand All @@ -38,6 +40,8 @@
$code = <STDIN>;

my $session = $api->get_access_token($code);
# end Code Authorization Grant
#my $session = $api->get_access_token(); # Client Credentials Grant
#print Dumper $session; # Net::OAuth2::AccessToken object
#print Dumper $session->access_token;
#print Dumper $session->refresh_token;
Expand Down
6 changes: 4 additions & 2 deletions lib/Net/Upwork/API.pm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use warnings;
use Net::Upwork::API::Config;
use Net::Upwork::API::Client;

our $VERSION = '2.1.4';
our $VERSION = '2.2.0';

use constant TOKEN_TYPE_BEARER => 'Bearer';

Expand Down Expand Up @@ -116,7 +116,9 @@ sub get_access_token {
my $self = shift;
my ($code) = @_;

chomp($code);
if (defined $code) {
chomp($code);
}

$self->{client}{access_token_session} = $self->{client}{oauth_client}->get_access_token($code);

Expand Down
43 changes: 29 additions & 14 deletions lib/Net/Upwork/API/Client.pm
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,35 @@ sub get_oauth_client {
$lwp = LWP::UserAgent->new();
$lwp->agent(UPWORK_LIBRARY_USER_AGENT);

$self->{oauth_client} = Net::OAuth2::Profile::WebServer->new(
client_id => $self->{config}{client_id},
client_secret => $self->{config}{client_secret},
access_token => $self->{config}{access_token},
refresh_token => $self->{config}{refresh_token},
expires_in => $self->{config}{expires_in},
expires_at => $self->{config}{expires_at},
site => BASE_HOST,
authorize_path => URI_AUTH,
access_token_path => URI_ATOKEN,
refresh_token_path => URI_ATOKEN,
redirect_uri => $self->{config}{redirect_uri},
user_agent => $lwp
);
if ($self->{config}{grant_type} eq "client_credentials") {
$self->{oauth_client} = Net::OAuth2::Profile::WebServer->new(
client_id => $self->{config}{client_id},
client_secret => $self->{config}{client_secret},
grant_type => $self->{config}{grant_type},
access_token => $self->{config}{access_token},
expires_in => $self->{config}{expires_in},
expires_at => $self->{config}{expires_at},
site => BASE_HOST,
access_token_path => URI_ATOKEN,
redirect_uri => $self->{config}{redirect_uri},
user_agent => $lwp
);
} else {
$self->{oauth_client} = Net::OAuth2::Profile::WebServer->new(
client_id => $self->{config}{client_id},
client_secret => $self->{config}{client_secret},
access_token => $self->{config}{access_token},
refresh_token => $self->{config}{refresh_token},
expires_in => $self->{config}{expires_in},
expires_at => $self->{config}{expires_at},
site => BASE_HOST,
authorize_path => URI_AUTH,
access_token_path => URI_ATOKEN,
refresh_token_path => URI_ATOKEN,
redirect_uri => $self->{config}{redirect_uri},
user_agent => $lwp
);
}
}

=item get
Expand Down
1 change: 1 addition & 0 deletions lib/Net/Upwork/API/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ sub new {
my %opts = @_;
$opts{client_id} ||= "";
$opts{client_secret} ||= "";
$opts{grant_type} ||= "code_authorization";
$opts{access_token} ||= "";
$opts{refresh_token} ||= "";
$opts{expires_in} ||= "";
Expand Down

0 comments on commit c1e4689

Please sign in to comment.