CatalystX-OAuth2

 view release on metacpan or  search on metacpan

lib/Catalyst/ActionRole/OAuth2/AuthToken/ViaAuthGrant.pm  view on Meta::CPAN

package Catalyst::ActionRole::OAuth2::AuthToken::ViaAuthGrant;
use Moose::Role;
use Try::Tiny;
use CatalystX::OAuth2::Request::AuthToken;

# ABSTRACT: Authorization token provider endpoint for OAuth2 authentication flows


with 'CatalystX::OAuth2::ActionRole::Token';

sub build_oauth2_request {
  my ( $self, $controller, $c ) = @_;

  my $store = $controller->store;
  my $req;

lib/Catalyst/ActionRole/OAuth2/AuthToken/ViaRefreshToken.pm  view on Meta::CPAN

package Catalyst::ActionRole::OAuth2::AuthToken::ViaRefreshToken;
use Moose::Role;
use Try::Tiny;
use CatalystX::OAuth2::Request::RefreshToken;

# ABSTRACT: Authorization token refresh provider endpoint for OAuth2 authentication flows


with 'CatalystX::OAuth2::ActionRole::Token';

sub build_oauth2_request {
  my ( $self, $controller, $c ) = @_;

  my $store = $controller->store;
  my $req;

lib/Catalyst/ActionRole/OAuth2/GrantAuth.pm  view on Meta::CPAN

package Catalyst::ActionRole::OAuth2::GrantAuth;
use Moose::Role;
use Try::Tiny;
use CatalystX::OAuth2::Request::GrantAuth;

# ABSTRACT: Authorization grant endpoint for OAuth2 authentication flows


with 'CatalystX::OAuth2::ActionRole::Grant';

sub build_oauth2_request {
  my ( $self, $controller, $c ) = @_;

  my $store = $controller->store;
  my $req;
  try {

lib/Catalyst/ActionRole/OAuth2/ProtectedResource.pm  view on Meta::CPAN

package Catalyst::ActionRole::OAuth2::ProtectedResource;
use Moose::Role;
use CatalystX::OAuth2::Request::ProtectedResource;

# ABSTRACT: Resource endpoint for OAuth2 authentication flows


with 'CatalystX::OAuth2::ActionRole::RequestInjector';

sub build_oauth2_request {
  my ( $self, $controller, $c ) = @_;

  my $auth = $c->req->header('Authorization')
    or $c->res->status(401), $c->detach;
  my ( $type, $token ) = split ' ', $auth;

lib/Catalyst/ActionRole/OAuth2/RequestAuth.pm  view on Meta::CPAN

package Catalyst::ActionRole::OAuth2::RequestAuth;
use Moose::Role;
use Try::Tiny;
use URI;
use CatalystX::OAuth2::Request::RequestAuth;

# ABSTRACT: Authorization grant endpoint for OAuth2 authentication flows


with 'CatalystX::OAuth2::ActionRole::Grant';

has enable_client_secret => ( isa => 'Bool', is => 'ro', default => 0 );

sub build_oauth2_request {
  my ( $self, $controller, $c ) = @_;

  my $store = $controller->store;

lib/CatalystX/OAuth2.pm  view on Meta::CPAN

package CatalystX::OAuth2;
use Moose::Role;

# ABSTRACT: OAuth2 services for Catalyst


requires '_build_query_parameters';

# spec isn't clear re missing endpoint uris
has redirect_uri  => ( is => 'ro', required => 0 );

has store => (
  is        => 'rw',
  does      => 'CatalystX::OAuth2::Store',
  init_arg  => undef,
  predicate => 'has_store'
);

has query_parameters => ( is => 'rw', init_arg => undef, lazy_build => 1 );

lib/CatalystX/OAuth2/Request/RequestAuth.pm  view on Meta::CPAN

    or return {
    error             => 'unauthorized_client',
    error_description => 'the client identified by '
      . $self->client_id
      . ' is not authorized to access this resource'
    }
    if $self->enable_client_secret;

  $q{client_id} = $self->client_id;

  $client->endpoint eq $self->redirect_uri
    or return {
    error => 'invalid_request',
    error_description =>
      'redirection_uri does not match the registered client endpoint'
    };

  $q{redirect_uri} = $self->redirect_uri;

  my $code = $store->create_client_code( $self->client_id );
  $q{code} = $code->as_string;

  return \%q;
}

lib/CatalystX/OAuth2/Schema/Result/Client.pm  view on Meta::CPAN

package CatalystX::OAuth2::Schema::Result::Client;
use parent 'DBIx::Class';

# ABSTRACT: A table for registering clients

__PACKAGE__->load_components(qw(Core));
__PACKAGE__->table('client');
__PACKAGE__->add_columns(
  id            => { data_type => 'int',  is_auto_increment => 1 },
  endpoint      => { data_type => 'text', is_nullable       => 0 },
  client_secret => { data_type => 'text', is_nullable       => 1 }
);
__PACKAGE__->set_primary_key('id');
__PACKAGE__->has_many( codes => 'CatalystX::OAuth2::Schema::Result::Code' =>
    { 'foreign.client_id' => 'self.id' } );

sub find_refresh {
  shift->codes->search( { is_active => 1 } )
    ->related_resultset('refresh_tokens')->find(@_);
}

lib/CatalystX/OAuth2/Store.pm  view on Meta::CPAN

package CatalystX::OAuth2::Store;
use Moose::Role;

# ABSTRACT: The API for oauth2 stores

requires qw(
  find_client
  client_endpoint
  create_client_code
  client_code_is_active
  activate_client_code
  deactivate_client_code
  create_access_token
  find_client_code
  verify_client_secret
  verify_client_token
);

lib/CatalystX/OAuth2/Store/DBIC.pm  view on Meta::CPAN

has client_model => (
  isa      => 'Str',
  is       => 'ro',
  required => 1
);
has _client_model => (
  isa        => 'DBIx::Class::ResultSet',
  is         => 'ro',
  lazy_build => 1
);
has endpoint_field => ( isa => 'Str', is => 'ro', default => 'endpoint' );
has refresh_relation =>
  ( isa => 'Str', is => 'ro', default => 'refresh_tokens' );
has token_relation => ( isa => 'Str', is => 'ro', default => 'tokens' );
has code_relation  => ( isa => 'Str', is => 'ro', default => 'codes' );
has code_activation_field =>
  ( isa => 'Str', is => 'ro', default => 'is_active' );

sub _build__client_model {
  my ($self) = @_;
  return $self->app->model( $self->client_model );
}

sub find_client {
  my ( $self, $id ) = @_;
  $self->_client_model->find($id);
}

sub client_endpoint {
  my ( $self, $id ) = @_;
  my $client = $self->find_client($id)
    or return;
  return $client->get_column( $self->endpoint_field );
}

sub _code_rs {
  my ( $self, $id ) = @_;
  return $self->_client_model->related_resultset( $self->code_relation )
    unless defined($id);
  my $client = $self->find_client($id)
    or return;
  return $client->related_resultset( $self->code_relation );
}

t/lib/AuthServer/Model/DB.pm  view on Meta::CPAN

package AuthServer::Model::DB;
use Moose;

BEGIN { extends 'Catalyst::Model::DBIC::Schema' }

has user_endpoint =>
  ( isa => 'Str', is => 'ro', default => sub {'http://localhost/auth'} );

__PACKAGE__->config(
  schema_class => 'CatalystX::OAuth2::Schema',
  connect_info => [ 'dbi:SQLite:dbname=:memory:', '', '' ]
);

around COMPONENT => sub {
  my $orig  = shift;
  my $class = shift;
  my $self  = $class->$orig(@_);
  $self->schema->deploy;
  $self->schema->resultset('Client')
    ->create(
    { endpoint => $self->user_endpoint, client_secret => 'foosecret' } );
  return $self;
};

1;

t/unit/300-actionrole-grant-auth.t  view on Meta::CPAN

use strictures 1;
use Test::More;

use HTTP::Request::Common;
use lib 't/lib';
use Catalyst::Test 'AuthServer';


my $code =
  AuthServer->model('DB::Code')
  ->create( { client => { endpoint => '/client/foo' } } );

# try grant with invalid code and no approval param
# should display form
{
  my $uri = URI->new('/grant');
  $uri->query_form(
    { response_type => 'code',
      client_id     => 1,
      state         => 'bar',
      code          => 999999,

t/unit/400-actionrole-auth-token-via-auth-grant.t  view on Meta::CPAN

use Test::More;
use JSON::Any;
use HTTP::Request::Common;
use lib 't/lib';
use Catalyst::Test 'AuthServer';

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


my $code = AuthServer->model('DB::Code')
  ->create( { client => { endpoint => '/client/foo' } } );

{
  my $uri = URI->new('/token');
  $uri->query_form(
    { grant_type   => 'authorization_code',
      redirect_uri => '/client/foo',
      code         => $code->as_string
    }
  );
  my ($res2, $c) = ctx_request($uri);

t/unit/500-actionrole-auth-token-via-refresh-token.t  view on Meta::CPAN

use Test::More;
use JSON::Any;
use HTTP::Request::Common;
use lib 't/lib';
use Catalyst::Test 'AuthServer';

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


my $code = AuthServer->model('DB::Code')->create(
  { client    => { endpoint => '/client/foo' },
    is_active => 1
  }
);

my $refresh;

{
  my $uri = URI->new('/withrefresh/token');
  $uri->query_form(
    { grant_type   => 'authorization_code',

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.644 second using v1.00-cache-2.02-grep-82fe00e-cpan-4673cadbf75 )