CatalystX-OAuth2

 view release on metacpan or  search on metacpan

lib/Catalyst/Authentication/Credential/OAuth2.pm  view on Meta::CPAN

  push(@data, (state=>$auth_info->{state})) if exists $auth_info->{state};
  push(@data, (client_secret=>$self->client_secret)) if $self->has_client_secret;

  my $req;
  if($self->token_uri_method eq 'GET') {
    $uri->query_form(+{@data});
    $req = GET $uri;
  } elsif($self->token_uri_method eq 'POST') {
    if($self->token_uri_post_content_type eq 'application/json') {
      $req = POST $uri, 'Content_Type' => 'application/json', Content => $j->to_json(+{@data});
    } elsif($self->token_uri_post_content_type eq 'application/x-www-form-urlencoded') {
      $req = POST $uri, 'Content_Type' => 'application/x-www-form-urlencoded', Content => \@data;
    } else {
      die "Unrecognized 'token_uri_post_content_type' of '${\$self->token_uri_post_content_type}'";
    }
  } else {
    die "Unrecognized 'token_uri_method' of '${\$self->token_uri_method}'";
  }

  my $response = $self->ua->request($req);
  if($response->is_success) {
    my $data = $j->jsonToObj( $response->decoded_content ); # Eval wrap
    return $data;
  } else {
    return;
  }
}

1;

__END__

=pod

=head1 NAME

Catalyst::Authentication::Credential::OAuth2 - Authenticate against OAuth2 servers

=head1 VERSION

version 0.001009

=head1 SYNOPSIS

    __PACKAGE__->config(
      'Plugin::Authentication' => {
        default => {
          credential => {
            class     => 'OAuth2',
            grant_uri => 'http://authserver/request',
            token_uri => 'http://authserver/token',
            client_id => 'dead69beef'
          },
          store => { class => 'Null' }
        }
      }
    );

=head1 DESCRIPTION

This module implements authentication via OAuth2 credentials, giving you a
user object which stores tokens for accessing protected resources.

=head1 ATTRIBUTES

=head2 grant_uri

=head2 token_uri

=head2 client_id

Required attributes that you get from your Oauth2 provider

=head2 client_secret

optional secret code from your Oauth2 provider (you need to review the docs from
your provider).

=head2 response_type

The Oauth2 response_type.  Defaults to 'code'.

=head2 scope

Value of 'scope' field submitted to the grant_uri.  Optional.

=head2 audience

Value of 'audience' field submitted to the grant_uri.  Optional.

=head2 token_uri_method

Default is GET; some providers require POST

=head2 token_uri_post_content_type

Default is 'application/x-www-form-urlencoded', some providers support 'application/json'. 

=head2 has_extra_find_user_token_fields

By default we call ->find_user on the store with a hashref that contains key 'token' and the
value of the access_token (which we get from calling the 'token_uri').  The results of calling
the token_uri is usually a JSON named array structure which can contain other fields such as
id_token (typically a JWT).  You can set this to an arrayref of extra fields you want to pass.

=head1 AUTHOR

Eden Cardim <edencardim@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2017 by Suretec Systems Ltd.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut



( run in 0.559 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )