sub
call {
my
(
$self
,
$env
) =
@_
;
my
$is_legacy
= 0;
my
$error_res
=
try
{
my
$req
= Plack::Request->new(
$env
);
my
$parser
= OAuth::Lite2::ParamMethods->get_param_parser(
$req
)
or OAuth::Lite2::Server::Error::InvalidRequest->throw;
$is_legacy
=
$parser
->is_legacy(
$req
);
my
(
$token
,
$params
) =
$parser
->parse(
$req
);
OAuth::Lite2::Server::Error::InvalidRequest->throw
unless
$token
;
my
$dh
=
$self
->{data_handler}->new;
my
$access_token
=
$dh
->get_access_token(
$token
);
OAuth::Lite2::Server::Error::InvalidToken->throw
unless
$access_token
;
Carp::croak
"OIDC::Lite::Server::DataHandler::get_access_token doesn't return OAuth::Lite2::Model::AccessToken"
unless
$access_token
->isa(
"OAuth::Lite2::Model::AccessToken"
);
unless
(
$access_token
->created_on +
$access_token
->expires_in >
time
())
{
if
(
$is_legacy
){
OAuth::Lite2::Server::Error::ExpiredTokenLegacy->throw;
}
else
{
OAuth::Lite2::Server::Error::ExpiredToken->throw;
}
}
my
$auth_info
=
$dh
->get_auth_info_by_id(
$access_token
->auth_id);
OAuth::Lite2::Server::Error::InvalidToken->throw
unless
$auth_info
;
Carp::croak
"OIDC::Lite::Server::DataHandler::get_auth_info_by_id doesn't return OIDC::Lite::Model::AuthInfo"
unless
$auth_info
->isa(
"OIDC::Lite::Model::AuthInfo"
);
$dh
->validate_client_by_id(
$auth_info
->client_id)
or OAuth::Lite2::Server::Error::InvalidToken->throw;
$dh
->validate_user_by_id(
$auth_info
->user_id)
or OAuth::Lite2::Server::Error::InvalidToken->throw;
$env
->{REMOTE_USER} =
$auth_info
->user_id;
$env
->{X_OAUTH_CLIENT} =
$auth_info
->client_id;
$env
->{X_OAUTH_SCOPE} =
$auth_info
->scope
if
$auth_info
->scope;
$env
->{X_OIDC_USERINFO_CLAIMS} =
$auth_info
->userinfo_claims
if
exists
$auth_info
->userinfo_claims->[0];
$env
->{X_OAUTH_IS_LEGACY} = (
$is_legacy
);
return
;
}
catch
{
if
(
$_
->isa(
"OAuth::Lite2::Server::Error"
)) {
my
@params
;
push
(
@params
,
sprintf
(
q{realm="%s"}
,
$self
->{realm}))
if
$self
->{realm};
push
(
@params
,
sprintf
(
q{error="%s"}
,
$_
->type));
push
(
@params
,
sprintf
(
q{error_description="%s"}
,
$_
->description))
if
$_
->description;
push
(
@params
,
sprintf
(
q{error_uri="%s"}
,
$self
->{error_uri}))
if
$self
->{error_uri};
if
(
$is_legacy
){
return
[
$_
->code, [
"WWW-Authenticate"
=>
"OAuth "
.
join
(
', '
,
@params
) ], [ ] ];
}
else
{
return
[
$_
->code, [
"WWW-Authenticate"
=>
"Bearer "
.
join
(
', '
,
@params
) ], [ ] ];
}
}
else
{
die
$_
;
}
};
$error_res
=
$self
->app->(
$env
)
unless
$error_res
;
return
$error_res
;
}
1;