CatalystX-OAuth2

 view release on metacpan or  search on metacpan

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

    return $realm->find_user( \%find_user_fields, $ctx );
  }
}

sub _build_callback_uri {
  my ( $self, $ctx ) = @_;
  my $uri = $ctx->request->uri->clone;
  $uri->query(undef);
  return $uri;
}

sub extend_permissions {
  my ( $self, $callback_uri, $auth_info ) = @_;
  my $uri   = URI->new( $self->grant_uri );
  my $query = {
    response_type => $self->response_type,
    client_id     => $self->client_id,
    redirect_uri  => $callback_uri,
  };
  $query->{state} = $auth_info->{state} if exists $auth_info->{state};
  $query->{scope} = $self->scope if $self->has_scope;
  $query->{scope} = $auth_info->{scope} if exists $auth_info->{scope};
  $query->{audience} = $self->audience if $self->has_audience;
  $query->{audience} = $auth_info->{audience} if exists $auth_info->{audience};

  $uri->query_form($query);
  return $uri;
}

my $j = JSON::Any->new;

sub request_access_token {
  my ( $self, $callback_uri, $code, $auth_info ) = @_;
  my $uri   = URI->new( $self->token_uri );
  my @data = (
    client_id    => $self->client_id,
    redirect_uri => "$callback_uri", #stringify for JSON
    code         => $code,
    grant_type   => 'authorization_code');
  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'.



( run in 1.652 second using v1.01-cache-2.11-cpan-39bf76dae61 )