Gungho

 view release on metacpan or  search on metacpan

docs/ja/Gungho/Component/Authentication/Basic.pod  view on Meta::CPAN


=head1 NAME

Gungho::Component::Authentication::Basic.ja - GunghoでBasic認証を行う

=head1 SYNOPSIS

  ---
  components:
    - Authentication::Basic
  credentials:
    basic:
      -
        - http://example.com
        - "Admin Only"
        - username
        - password
      -
        - http://example2.com
        - "Admin Only"
        - username2

docs/ja/Gungho/Component/Authentication/Basic.pod  view on Meta::CPAN


=head2 setup($c)

コンポーネントを初期化します。

=head2 authenticate($is_proxy, $realm, $request, $response)

Basic認証を行います。認証が必要な最初のリクエストが401で失敗した場合は
認証ヘッダーを付与し再度リクエストを行います。

=head2 set_basic_credentials($uri, $realm, $uid, $pass)

指定のURLとRealmに対して認証情報を設定します。

=head2 get_basic_credentials($uri, $realm)

指定のURLとRealmに対して認証情報を返します

=cut

lib/Gungho/Component/Authentication/Basic.pm  view on Meta::CPAN

use MIME::Base64 ();
use URI;

__PACKAGE__->mk_classdata($_) for qw(basic_authentication);

sub setup
{
    my $self = shift;
    $self->basic_authentication({});

    my $list = $self->config->{credentials}{basic};
    if ($list) {
        foreach my $conf (@$list) {
            $self->set_basic_credentials(@$conf);
        }
    } 

    $self->next::method(@_);
}

sub authenticate
{
    my ($self, $proxy, $auth_param, $req, $res) = @_;

    my ($user, $pass) = $self->get_basic_credentials($req->url, $auth_param->{realm});

    return 0 unless defined $user and defined $pass;

    my $auth_header = $proxy ? "Proxy-Authorization" : "Authorization";
    my $auth_value = "Basic " . MIME::Base64::encode("$user:$pass", "");

    # Check if this is a repeated fialure to auth
    my $r = $res;
    while ($r) {
        my $auth = $r->request->header($auth_header);

lib/Gungho/Component/Authentication/Basic.pm  view on Meta::CPAN

        $r = $r->previous;
    }

    my $referral = $req->clone;
    $referral->header($auth_header => $auth_value);

    $self->send_request($referral);
    return 1;
}

sub set_basic_credentials
{
    my $self = shift;
    my ($uri, $realm, $uid, $pass) = @_;

    if (! eval { $uri->isa('URI') }) {
        $uri = URI->new($uri);
    }

    $self->basic_authentication()->{lc ($uri->host_port)}{$realm} = [$uid, $pass];
}

sub get_basic_credentials
{
    my $self = shift;
    my ($uri, $realm) = @_;

    if (! eval { $uri->isa('URI') }) {
        $uri = URI->new($uri);
    }

    if (exists $self->basic_authentication()->{lc($uri->host_port)}{$realm}) {
        return @{$self->basic_authentication()->{lc($uri->host_port)}{$realm}};

lib/Gungho/Component/Authentication/Basic.pm  view on Meta::CPAN


=head1 NAME

Gungho::Component::Authentication::Basic - Add Basic Auth To Gungho

=head1 SYNOPSIS

  ---
  components:
    - Authentication::Basic
  credentials:
    basic:
      -
        - http://example.com
        - "Admin Only"
        - username
        - password
      -
        - http://example2.com
        - "Admin Only"
        - username2

lib/Gungho/Component/Authentication/Basic.pm  view on Meta::CPAN

=head1 METHODS

=head2 setup($c)

Sets up the component

=head2 authenticate($is_proxy, $realm, $request, $response)

Does the WWW Authentication and redispatches the request

=head2 set_basic_credentials($uri, $realm, $uid, $pass)

Sets the credentials for a uri + realm.

=head2 get_basic_credentials($uri, $realm)

Get the credentials for a uri + realm.

=head1 CAVEATS

This component merely stores data in Gungho that can be used for authentication.
The Engine type that Gungho is currently using must respect the information.

=cut

t/03_live/twitter.t  view on Meta::CPAN

    if (! $username || ! $password) {
        plan(skip_all => "Enable GUNGHO_TWITTER_USERNAME and GUNGHO_TWITTER_PASSWORD to run these tests");
    } else {
        plan(tests => 5);
        use_ok("Gungho::Inline");
    }
}

Gungho::Inline->run(
    {
        credentials => {
            basic =>
              [ [ 'http://twitter.com', 'Twitter API', $username, $password ] ]
        },
        components => ['Authentication::Basic'],
    },
    {
        provider => \&provider,
        handler  => \&handler,
    }
);



( run in 0.423 second using v1.01-cache-2.11-cpan-fd5d4e115d8 )