| 1 | package Net::Google::FederatedLogin::Apps::Discoverer; | ||||||||||
| 2 | { | ||||||||||
| 3 | 2 | $Net::Google::FederatedLogin::Apps::Discoverer::VERSION = '0.8.0'; | |||||||||
| 4 | } | ||||||||||
| 5 | # ABSTRACT: Find the OpenID endpoint for apps domain accounts | ||||||||||
| 6 | |||||||||||
| 7 | 2 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 2 | with 'Net::Google::FederatedLogin::Role::Discoverer'; | |||||||||
| 10 | |||||||||||
| 11 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | ▶ | use URI::Escape; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 2 | has app_domain => ( | |||||||||
| 15 | is => 'rw', | ||||||||||
| 16 | isa => 'Str', | ||||||||||
| 17 | required => 1, | ||||||||||
| 18 | ); | ||||||||||
| 19 | |||||||||||
| 20 | 2 | has claimed_id => ( | |||||||||
| 21 | is => 'rw', | ||||||||||
| 22 | isa => 'Str', | ||||||||||
| 23 | ); | ||||||||||
| 24 | |||||||||||
| 25 | |||||||||||
| 26 | 2 | ▶ | sub perform_discovery { | ||||||||
|
Subroutine
perform_discovery: called CC=1 SCAR=3.9
Pod
documented
| |||||||||||
| 27 | 2 | my $self = shift; | |||||||||
| 28 | |||||||||||
| 29 | 2 | require XML::Twig; | |||||||||
| 30 | |||||||||||
| 31 | 0 | my $ua = $self->ua; | |||||||||
| 32 | 0 | my $response = $ua->get($self->_get_discovery_url, | |||||||||
| 33 | Accept => 'application/xrds+xml'); | ||||||||||
| 34 | |||||||||||
| 35 | 0 | my $open_id_endpoint; | |||||||||
| 36 | |||||||||||
| 37 | my $xt = XML::Twig->new( | ||||||||||
| 38 | 0 | ▶ | twig_handlers => {URI => sub {$open_id_endpoint = $_->text}}, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 39 | 0 | ); | |||||||||
| 40 | 0 | $xt->parse($response->decoded_content); | |||||||||
| 41 | |||||||||||
| 42 | 0 | return $open_id_endpoint; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 0 | ▶ | sub _get_discovery_url { | ||||||||
|
Subroutine
_get_discovery_url: not called CC=2 SCAR=17.9
| |||||||||||
| 46 | 0 | my $self = shift; | |||||||||
| 47 | |||||||||||
| 48 | 0 | ▶ | if($self->claimed_id) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 0 | return $self->_get_user_discovery_url; | |||||||||
| 50 | } else { | ||||||||||
| 51 | 0 | return $self->_get_idp_discovery_url; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 0 | ▶ | sub _get_idp_discovery_url { | ||||||||
|
Subroutine
_get_idp_discovery_url: not called CC=4 SCAR=30.0
| |||||||||||
| 57 | 0 | my $self = shift; | |||||||||
| 58 | |||||||||||
| 59 | 0 | my $app_domain = $self->app_domain; | |||||||||
| 60 | |||||||||||
| 61 | #Check google hosted | ||||||||||
| 62 | 0 | my $host_meta_url = 'https://www.google.com/accounts/o8/.well-known/host-meta?hd=' . $app_domain; | |||||||||
| 63 | 0 | my $ua = $self->ua; | |||||||||
| 64 | 0 | my $response = $ua->get($host_meta_url); | |||||||||
| 65 | 0 | ▶ | unless($response->is_success) { #fallback to the domain specific location | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 0 | $host_meta_url = sprintf 'http://%s/.well-known/host-meta', $app_domain; | |||||||||
| 67 | 0 | $response = $ua->get($host_meta_url); | |||||||||
| 68 | } | ||||||||||
| 69 | 0 | ▶ | unless($response->is_success) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 0 | croak 'Unable to find a host-meta page.'; | |||||||||
| 71 | } | ||||||||||
| 72 | 0 | ▶ | if($response->decoded_content =~ m{Link: <(.+)>; \Qrel="describedby http://reltype.google.com/openid/xrd-op"; type="application/xrds+xml"\E}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 0 | return $1; | |||||||||
| 74 | } else { | ||||||||||
| 75 | 0 | croak 'Unable to perform discovery - host-meta page is not as expected.' | |||||||||
| 76 | } | ||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 0 | ▶ | sub _get_user_discovery_url { | ||||||||
|
Subroutine
_get_user_discovery_url: not called CC=1 SCAR=6.9
| |||||||||||
| 80 | 0 | my $self = shift; | |||||||||
| 81 | |||||||||||
| 82 | 0 | my $claimed_id = $self->claimed_id; | |||||||||
| 83 | 0 | my $escaped_id = uri_escape($claimed_id); | |||||||||
| 84 | |||||||||||
| 85 | 0 | my $intermediate_url = $self->_get_idp_discovery_url; | |||||||||
| 86 | |||||||||||
| 87 | 0 | my $ua = $self->ua; | |||||||||
| 88 | 0 | my $response = $ua->get($intermediate_url, | |||||||||
| 89 | Accept => 'application/xrds+xml'); | ||||||||||
| 90 | |||||||||||
| 91 | 0 | my $discovery_url; | |||||||||
| 92 | my $xt = XML::Twig->new( | ||||||||||
| 93 | 0 | ▶ | twig_handlers => { Service => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 94 | 0 | ▶ | if($_->first_child_text('Type') eq 'http://www.iana.org/assignments/relation/describedby') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 0 | $discovery_url = $_->first_child_text('openid:URITemplate'); | |||||||||
| 96 | 0 | $discovery_url =~ s/{%uri}/$escaped_id/; | |||||||||
| 97 | } | ||||||||||
| 98 | }}, | ||||||||||
| 99 | 0 | ); | |||||||||
| 100 | 0 | $xt->parse($response->decoded_content); | |||||||||
| 101 | 0 | return $discovery_url; | |||||||||
| 102 | } | ||||||||||
| 103 | 2 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 104 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||
| 105 | 2 | 1; | |||||||||
| 106 | |||||||||||
| 107 | __END__ | ||||||||||