<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package OAuth::Lite2::Server::GrantHandler::ClientCredentials;

use strict;
use warnings;

use parent 'OAuth::Lite2::Server::GrantHandler';
use OAuth::Lite2::Server::Error;
use OAuth::Lite2::ParamMethod::AuthHeader;
use Carp ();

sub handle_request {
    my ($self, $dh) = @_;

    my $req = $dh-&gt;request;

    my $parser = OAuth::Lite2::ParamMethod::AuthHeader-&gt;new;
    my $header_credentials = $parser-&gt;basic_credentials($req);
    my $client_id = ($header_credentials-&gt;{client_id}) ? $header_credentials-&gt;{client_id} : $req-&gt;param("client_id");
    my $client_secret = ($header_credentials-&gt;{client_secret}) ? $header_credentials-&gt;{client_secret} : $req-&gt;param("client_secret");

    my $user_id = $dh-&gt;get_client_user_id($client_id, $client_secret);
    OAuth::Lite2::Server::Error::InvalidClient-&gt;throw unless defined $user_id;

    my $scope = $req-&gt;param("scope");

    my $auth_info = $dh-&gt;create_or_update_auth_info(
        client_id =&gt; $client_id,
        user_id   =&gt; $user_id,
        scope     =&gt; $scope,
    );
    Carp::croak "OAuth::Lite2::Server::DataHandler::create_or_update_auth_info doesn't return OAuth::Lite2::Model::AuthInfo"
        unless ($auth_info
            &amp;&amp; $auth_info-&gt;isa("OAuth::Lite2::Model::AuthInfo"));

    my $access_token = $dh-&gt;create_or_update_access_token(
        auth_info =&gt; $auth_info,
    );
    Carp::croak "OAuth::Lite2::Server::DataHandler::create_or_update_access_token doesn't return OAuth::Lite2::Model::AccessToken"
        unless ($access_token
            &amp;&amp; $access_token-&gt;isa("OAuth::Lite2::Model::AccessToken"));

    my $res = {
        token_type =&gt; 'Bearer',
        access_token =&gt; $access_token-&gt;token,
    };
    $res-&gt;{expires_in} = int($access_token-&gt;expires_in)
        if $access_token-&gt;expires_in;
    $res-&gt;{refresh_token} = $auth_info-&gt;refresh_token
        if $auth_info-&gt;refresh_token;
    $res-&gt;{scope} = $auth_info-&gt;scope
        if $auth_info-&gt;scope;

    return $res;
}

=head1 NAME

OAuth::Lite2::Server::GrantHandler::ClientCredentials - handler for 'client_credentials' grant_type request

=head1 SYNOPSIS

    my $handler = OAuth::Lite2::Server::GrantHandler::ClientCredentials-&gt;new;
    my $res = $handler-&gt;handle_request( $data_handler );

=head1 DESCRIPTION

handler for 'client_credentials' grant_type request.

=head1 METHODS

=head2 handle_request( $req )

See L&lt;OAuth::Lite2::Server::GrantHandler&gt; document.

=head1 AUTHOR

Lyo Kato, E&lt;lt&gt;lyo.kato@gmail.comE&lt;gt&gt;

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2010 by Lyo Kato

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.8 or,
at your option, any later version of Perl 5 you may have available.

=cut

1;
</pre></body></html>