Authen-CAS-UserAgent

 view release on metacpan or  search on metacpan

lib/Authen/CAS/UserAgent.pm  view on Meta::CPAN

	}

	return;
};

# default heuristic for finding login parameters
my $defaultLoginParamsHeuristic = sub {
	my ($service, $response, $ua, $h, @params) = @_;

	# find all input controls on the submit form
	my $content = $response->decoded_content;
	while($content =~ /(\<input.*?\>)/igs) {
		my $input = $1;
		my $name = $input =~ /name=\"(.*?)\"/si ? $1 : undef;
		my $value = $input =~ /value=\"(.*?)\"/si ? $1 : undef;

		# we only care about the lt, execution, and _eventId parameters
		if($name eq 'lt' || $name eq 'execution' || $name eq '_eventId') {
			push @params, $name, $value;
		}
	}

lib/Authen/CAS/UserAgent.pm  view on Meta::CPAN


	#attempt using the Location header on a redirect response
	if($response->is_redirect) {
		my $uri = $response->header('Location');
		if($uri =~ /[?&]ticket=([^&]*)$/) {
			return $1;
		}
	}

	#check for a javascript window.location.href redirect
	if($response->decoded_content =~ /window\.location\.href="[^"]*ticket=([^&"]*?)"/sg) {
		return $1;
	}

	return;
};

#default callback to log the user into CAS and return a ticket for the specified service
my $defaultLoginCallback = sub {
	my ($service, $ua, $h) = @_;

lib/Authen/CAS/UserAgent.pm  view on Meta::CPAN


	#create the request uri
	my $ptUri = URI->new_abs('proxy', $h->{'casServer'});
	$ptUri->query_form(
		'pgt'           => $h->{'pgt'},
		'targetService' => $service,
	);

	# fetch proxy ticket and parse response xml
	my $response = $ua->simple_request(HTTP::Request::Common::GET($ptUri));
	my $doc = eval {XML::LibXML->new()->parse_string($response->decoded_content('charset' => 'none'))};
	if($@ || !$doc) {
		$h->{'error'} = ERROR_PROXY_INVALIDRESPONSE;
		push @{$h->{'errors'}}, $h->{'error'};
		return;
	}

	# process the response to extract the proxy ticket or any errors
	my $xpc = XML::LibXML::XPathContext->new();
	$xpc->registerNs('cas', XMLNS_CAS);
	if($xpc->exists('/cas:serviceResponse/cas:proxyFailure', $doc)) {

lib/Authen/CAS/UserAgent.pm  view on Meta::CPAN

		'password' => $h->{'password'},
	]));
	return if($tgtResponse->code != 201);
	my $tgtUri = $tgtResponse->header('Location');

	#retrieve a ticket for the requested service
	my $ticketResponse = $ua->simple_request(HTTP::Request::Common::POST($tgtUri, [
		'service' => $service,
	]));
	return if($ticketResponse->code != 200);
	return $ticketResponse->decoded_content;
};

##Static Methods

#return the default user agent for this class
sub _agent($) {
	return
		$_[0]->SUPER::_agent . ' ' .
		'CAS-UserAgent/' . $VERSION;
}

t/20rest.t  view on Meta::CPAN

$loginUri->query_param('service', $service);
addMockResponses({
	$service  => HTTP::Response->new(302, undef, ['Location' => $loginUri]),
});

# REST TGT api
addMockResponses(sub {
	my ($request) = @_;
	if($request->uri eq $restLoginUri && $request->method eq 'POST') {
		my $params = URI->new('http:');
		$params->query($request->decoded_content);
		if($params->query_param('username') eq $username && $params->query_param('password') eq $password) {
			return HTTP::Response->new(201, undef, ['Location' => $restTgtUri]);
		} else {
			return HTTP::Response->new(400);
		}
	}

	return;
});

# REST ST api
addMockResponses(sub {
	my ($request) = @_;
	if($request->uri eq $restTgtUri && $request->method eq 'POST') {
		my $params = URI->new('http:');
		$params->query($request->decoded_content);
		if($params->query_param('service') eq $service) {
			# generate & return a ticket
			$ticket = 'ST-' . join('', map {chr(rand(26)+65)} (0..19));
			return HTTP::Response->new(200, undef, undef, $ticket);
		} else {
			return HTTP::Response->new(400);
		}
	}

	return;

t/20rest.t  view on Meta::CPAN


# valid username & password
$ua->attach_cas_handler(
	'server' => $casServer,
	'username' => $username,
	'password' => $password,
	'restful' => 1,
);
$response = $ua->get($service);
is($response->code, 200);
is($response->decoded_content, 'success');

# invalid username & password
$ua->attach_cas_handler(
	'server' => $casServer,
	'username' => $username,
	'password' => $password . '.invalid',
	'restful' => 1,
);
$response = $ua->get($service);
is($response->code, 302);



( run in 0.268 second using v1.01-cache-2.11-cpan-26ccb49234f )