« blib/lib/Lemonldap/NG/Portal/Plugins/CrowdSecAgent.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Plugins/BruteForceProtection.pm »
| 1 | package Lemonldap::NG::Portal::Auth::Facebook; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use Mouse; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use URI::Escape; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Lemonldap::NG::Common::FormEncode; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Lemonldap::NG::Portal::Main::Constants qw(PE_OK PE_ERROR PE_BADCREDENTIALS); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | our $VERSION = '2.19.0'; | |||||||||
| 11 | |||||||||||
| 12 | 1 | extends 'Lemonldap::NG::Portal::Main::Auth'; | |||||||||
| 13 | |||||||||||
| 14 | # INITIALIZATION | ||||||||||
| 15 | |||||||||||
| 16 | 0 | ▶ | sub init { | ||||||||
|
Subroutine
init: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 17 | 0 | my $self = shift; | |||||||||
| 18 | 0 | eval { require Net::Facebook::Oauth2; }; | |||||||||
| 19 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | 0 | $self->error("Unable to load Net::Facebook::Oauth2: $@"); | |||||||||
| 21 | 0 | return 0; | |||||||||
| 22 | } | ||||||||||
| 23 | 0 | my $ret = 1; | |||||||||
| 24 | 0 | foreach my $arg (qw(facebookAppId facebookAppSecret)) { | |||||||||
| 25 | 0 | ▶ | unless ( $self->conf->{$arg} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 0 | $ret = 0; | |||||||||
| 27 | 0 | $self->error("Parameter $arg is required"); | |||||||||
| 28 | } | ||||||||||
| 29 | } | ||||||||||
| 30 | 0 | return $ret; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | # RUNNING METHODS | ||||||||||
| 34 | |||||||||||
| 35 | 0 | ▶ | sub extractFormInfo { | ||||||||
|
Subroutine
extractFormInfo: not called CC=12 SCAR=50.5
Pod
undocumented
| |||||||||||
| 36 | 0 | my ( $self, $req ) = @_; | |||||||||
| 37 | 0 | my $fb = $self->fb($req); | |||||||||
| 38 | |||||||||||
| 39 | # 1. Check Facebook responses | ||||||||||
| 40 | |||||||||||
| 41 | # 1.1 Good responses | ||||||||||
| 42 | 0 | ▶ | if ( my $code = $req->param('code') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 0 | $self->logger->debug("Get code $code from Facebook"); | |||||||||
| 44 | 0 | my $access_token; | |||||||||
| 45 | 0 | eval { $access_token = $fb->get_access_token( code => $code ); }; | |||||||||
| 46 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 0 | $self->logger->error("Error while getting access token: $@"); | |||||||||
| 48 | 0 | return PE_ERROR; | |||||||||
| 49 | } | ||||||||||
| 50 | 0 | ▶ | if ($access_token) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 0 | $req->{sessionInfo}->{_facebookToken} = $access_token; | |||||||||
| 52 | |||||||||||
| 53 | # Get mandatory fields (see https://developers.facebook.com/tools/explorer) | ||||||||||
| 54 | 0 | my @fields = ( $self->conf->{facebookUserField} ); | |||||||||
| 55 | |||||||||||
| 56 | # Search all wanted fields | ||||||||||
| 57 | push @fields, | ||||||||||
| 58 | 0 | ▶ | map { /^(\w+)$/ ? ($1) : () } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 0 | values %{ $self->conf->{facebookExportedVars} }; | |||||||||
| 60 | |||||||||||
| 61 | 0 | my $data; | |||||||||
| 62 | |||||||||||
| 63 | # When a field is not granted, Facebook returns only an error | ||||||||||
| 64 | # without real explanation. So here we try to reduce query until | ||||||||||
| 65 | # having a valid response | ||||||||||
| 66 | 0 | ▶ | while (@fields) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 0 | $data = $fb->get( | |||||||||
| 68 | 'https://graph.facebook.com/me', | ||||||||||
| 69 | { fields => join( ',', @fields ) } | ||||||||||
| 70 | )->as_hash; | ||||||||||
| 71 | 0 | ▶ | unless ( ref $data ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 0 | $self->logger->error("Unable to get any Facebook field"); | |||||||||
| 73 | 0 | return PE_ERROR; | |||||||||
| 74 | } | ||||||||||
| 75 | 0 | ▶ | if ( $data->{error} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 0 | my $tmp = pop @fields; | |||||||||
| 77 | 0 | $self->logger->warn( | |||||||||
| 78 | "Unable to get some Facebook fields ($data->{error}->{message}). Retrying without $tmp" | ||||||||||
| 79 | ); | ||||||||||
| 80 | } | ||||||||||
| 81 | else { | ||||||||||
| 82 | 0 | last; | |||||||||
| 83 | } | ||||||||||
| 84 | } | ||||||||||
| 85 | 0 | ▶ | unless (@fields) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 0 | $self->logger->error("Unable to get any Facebook field"); | |||||||||
| 87 | 0 | return PE_ERROR; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | # Parse received data | ||||||||||
| 91 | 0 | foreach ( keys %$data ) { | |||||||||
| 92 | 0 | utf8::encode $data->{$_}; | |||||||||
| 93 | 0 | $self->logger->debug( "Facebook data $_: " . $data->{$_} ); | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | # Field to trace user | ||||||||||
| 97 | 0 | ▶ | unless ( $data->{ $self->conf->{facebookUserField} } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 0 | $self->logger->error('Unable to get Facebook id'); | |||||||||
| 99 | 0 | return PE_ERROR; | |||||||||
| 100 | } | ||||||||||
| 101 | 0 | $req->user( $data->{ $self->conf->{facebookUserField} } ); | |||||||||
| 102 | 0 | $req->data->{_facebookData} = $data; | |||||||||
| 103 | 0 | $req->{sessionInfo}->{_facebookData} = $data; | |||||||||
| 104 | |||||||||||
| 105 | # Force redirection to avoid displaying Oauth data | ||||||||||
| 106 | 0 | $req->mustRedirect(1); | |||||||||
| 107 | 0 | return PE_OK; | |||||||||
| 108 | } | ||||||||||
| 109 | 0 | return PE_BADCREDENTIALS; | |||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | # 1.2 Bad responses | ||||||||||
| 113 | 0 | ▶ | if ( my $error_code = $req->param('error_code') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 0 | my $error_message = $req->param('error_message'); | |||||||||
| 115 | 0 | $self->userLogger->error( | |||||||||
| 116 | "Facebook error code $error_code: $error_message"); | ||||||||||
| 117 | 0 | return PE_ERROR; | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | # 2. Else redirect user to Facebook login page: | ||||||||||
| 121 | |||||||||||
| 122 | # Build Facebook redirection | ||||||||||
| 123 | # TODO: use a param to use "publish_stream" or not | ||||||||||
| 124 | 0 | my $check_url = $fb->get_authorization_url( | |||||||||
| 125 | scope => [ 'public_profile', 'email' ], | ||||||||||
| 126 | display => 'page', | ||||||||||
| 127 | ); | ||||||||||
| 128 | 0 | $req->urldc($check_url); | |||||||||
| 129 | 0 | $self->logger->debug( "Redirect user to " . $req->{urldc} ); | |||||||||
| 130 | 0 | $req->continue(1); | |||||||||
| 131 | 0 | $req->steps( [] ); | |||||||||
| 132 | |||||||||||
| 133 | 0 | return PE_OK; | |||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | 0 | ▶ | sub authenticate { | ||||||||
|
Subroutine
authenticate: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 137 | 0 | return PE_OK; | |||||||||
| 138 | } | ||||||||||
| 139 | |||||||||||
| 140 | 0 | ▶ | sub setAuthSessionInfo { | ||||||||
|
Subroutine
setAuthSessionInfo: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 141 | 0 | my ( $self, $req ) = @_; | |||||||||
| 142 | $req->{sessionInfo}->{authenticationLevel} = | ||||||||||
| 143 | 0 | $self->conf->{facebookAuthnLevel}; | |||||||||
| 144 | 0 | return PE_OK; | |||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | 0 | ▶ | sub authFinish { | ||||||||
|
Subroutine
authFinish: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 148 | 0 | return PE_OK; | |||||||||
| 149 | } | ||||||||||
| 150 | |||||||||||
| 151 | 0 | ▶ | sub authLogout { | ||||||||
|
Subroutine
authLogout: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 152 | 0 | return PE_OK; | |||||||||
| 153 | } | ||||||||||
| 154 | |||||||||||
| 155 | 0 | ▶ | sub getDisplayType { | ||||||||
|
Subroutine
getDisplayType: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 156 | 0 | return "logo"; | |||||||||
| 157 | } | ||||||||||
| 158 | |||||||||||
| 159 | 0 | ▶ | sub fb { | ||||||||
|
Subroutine
fb: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 160 | 0 | my ( $self, $req ) = @_; | |||||||||
| 161 | 0 | my $conf = $self->{conf}; | |||||||||
| 162 | 0 | my $fb; | |||||||||
| 163 | 0 | my $sep = '?'; | |||||||||
| 164 | 0 | my $ret = $req->portal; | |||||||||
| 165 | |||||||||||
| 166 | 0 | eval { | |||||||||
| 167 | $fb = Net::Facebook::Oauth2->new( | ||||||||||
| 168 | application_id => $conf->{facebookAppId}, | ||||||||||
| 169 | application_secret => $conf->{facebookAppSecret}, | ||||||||||
| 170 | 0 | callback => $ret, | |||||||||
| 171 | ); | ||||||||||
| 172 | }; | ||||||||||
| 173 | 0 | ▶ | $self->logger->error($@) if ($@); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 174 | |||||||||||
| 175 | 0 | return $fb; | |||||||||
| 176 | } | ||||||||||
| 177 | |||||||||||
| 178 | 1 | 1; | |||||||||