« blib/lib/Lemonldap/NG/Portal/Lib/SOAPProxy.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Lib/CrowdSec.pm »
| 1 | package Lemonldap::NG::Portal::2F::REST; | ||||||||||
| 2 | |||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | ▶ | use Mouse; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 4 | use Lemonldap::NG::Portal::Main::Constants qw( | |||||||||
| 6 | PE_BADOTP | ||||||||||
| 7 | PE_ERROR | ||||||||||
| 8 | PE_FORMEMPTY | ||||||||||
| 9 | PE_OK | ||||||||||
| 10 | PE_SENDRESPONSE | ||||||||||
| 11 | 4 | ▶ | ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 4 | our $VERSION = '2.0.16'; | |||||||||
| 14 | |||||||||||
| 15 | 4 | extends qw( | |||||||||
| 16 | Lemonldap::NG::Portal::Lib::Code2F | ||||||||||
| 17 | Lemonldap::NG::Portal::Lib::REST | ||||||||||
| 18 | ); | ||||||||||
| 19 | |||||||||||
| 20 | # INITIALIZATION | ||||||||||
| 21 | |||||||||||
| 22 | # Prefix can overriden by sfExtra and is used for routes | ||||||||||
| 23 | 4 | has prefix => ( is => 'rw', default => 'rest' ); | |||||||||
| 24 | |||||||||||
| 25 | # Used to lookup config | ||||||||||
| 26 | 4 | has conf_type => ( is => 'ro', default => 'rest' ); | |||||||||
| 27 | |||||||||||
| 28 | 4 | has legend => ( is => 'rw', default => 'enterRest2fCode' ); | |||||||||
| 29 | 4 | ▶ | has initAttrs => ( is => 'rw', default => sub { {} } ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 4 | ▶ | has vrfyAttrs => ( is => 'rw', default => sub { {} } ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 31 | |||||||||||
| 32 | sub init { | ||||||||||
| 33 | 4 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
init: called CC=8 SCAR=23.4
Pod
undocumented
| |||||||||||
| 34 | |||||||||||
| 35 | 4 | ▶ | if ( $self->code_activation ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 2 | ▶ | unless ( $self->conf->{rest2fInitUrl} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 0 | $self->logger->error( | |||||||||
| 38 | $self->prefix . '2f: missing intialization URL' ); | ||||||||||
| 39 | 0 | return 0; | |||||||||
| 40 | } | ||||||||||
| 41 | } | ||||||||||
| 42 | else { | ||||||||||
| 43 | 2 | ▶ | unless ( $self->conf->{rest2fVerifyUrl} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 0 | $self->logger->error( | |||||||||
| 45 | $self->prefix . '2f: missing verification URL' ); | ||||||||||
| 46 | 0 | return 0; | |||||||||
| 47 | } | ||||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 4 | foreach my $k ( keys %{ $self->conf->{rest2fInitArgs} } ) { | |||||||||
| 51 | 5 | my $attr = $self->conf->{rest2fInitArgs}->{$k}; | |||||||||
| 52 | 5 | $attr =~ s/^$//; | |||||||||
| 53 | 5 | ▶ | unless ( $attr =~ /^\w+$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 0 | $self->logger->error( $self->prefix | |||||||||
| 55 | . "2f: $k key must point to a single attribute or macro" ); | ||||||||||
| 56 | 0 | return 0; | |||||||||
| 57 | } | ||||||||||
| 58 | 5 | $self->initAttrs->{$k} = $attr; | |||||||||
| 59 | } | ||||||||||
| 60 | 4 | foreach my $k ( keys %{ $self->conf->{rest2fVerifyArgs} } ) { | |||||||||
| 61 | 3 | my $attr = $self->conf->{rest2fVerifyArgs}->{$k}; | |||||||||
| 62 | 3 | $attr =~ s/^$//; | |||||||||
| 63 | 3 | ▶ | unless ( $attr =~ /^\w+$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 0 | $self->logger->error( $self->prefix | |||||||||
| 65 | . "2f: $k key must point to a single attribute or macro" ); | ||||||||||
| 66 | 0 | return 0; | |||||||||
| 67 | } | ||||||||||
| 68 | 3 | $self->logger->debug( $self->prefix . "2f: push verify attribute $k" ); | |||||||||
| 69 | 3 | $self->vrfyAttrs->{$k} = $attr; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 4 | return $self->SUPER::init(); | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | sub sendCode { | ||||||||||
| 76 | 0 | ▶ | my ( $self, $req, $sessionInfo, $code ) = @_; | ||||||||
|
Subroutine
sendCode: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||
| 77 | |||||||||||
| 78 | 0 | ▶ | if ( $self->conf->{rest2fInitUrl} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | |||||||||||
| 80 | # Prepare args | ||||||||||
| 81 | my $args = { | ||||||||||
| 82 | user => $sessionInfo->{ $self->conf->{whatToTrace} }, | ||||||||||
| 83 | 0 | ▶ | ( $code ? ( code => $code ) : () ), | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | }; | ||||||||||
| 85 | 0 | foreach my $k ( keys %{ $self->{initAttrs} } ) { | |||||||||
| 86 | 0 | $args->{$k} = $sessionInfo->{ $self->{initAttrs}->{$k} }; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | # Launch REST request | ||||||||||
| 90 | 0 | $self->logger->debug( $self->prefix . '2f: call init URL' ); | |||||||||
| 91 | my $res = | ||||||||||
| 92 | 0 | eval { $self->restCall( $self->conf->{rest2fInitUrl}, $args ); }; | |||||||||
| 93 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 0 | $self->logger->error( $self->prefix . "2f: error ($@)" ); | |||||||||
| 95 | 0 | return PE_ERROR; | |||||||||
| 96 | } | ||||||||||
| 97 | 0 | ▶ | unless ( $res->{result} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 0 | $self->logger->error( $self->prefix . '2f: initialization failed' ); | |||||||||
| 99 | 0 | return PE_ERROR; | |||||||||
| 100 | } | ||||||||||
| 101 | } | ||||||||||
| 102 | else { | ||||||||||
| 103 | 0 | $self->logger->debug( | |||||||||
| 104 | $self->prefix . '2f: no init URL, skipping initialization' ); | ||||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 0 | return 1; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | sub verify_external { | ||||||||||
| 111 | 0 | ▶ | my ( $self, $req, $session, $code ) = @_; | ||||||||
|
Subroutine
verify_external: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||
| 112 | |||||||||||
| 113 | # Prepare args | ||||||||||
| 114 | my $args = { | ||||||||||
| 115 | user => $session->{ $self->conf->{whatToTrace} }, | ||||||||||
| 116 | 0 | code => $code | |||||||||
| 117 | }; | ||||||||||
| 118 | |||||||||||
| 119 | 0 | foreach my $k ( keys %{ $self->{vrfyAttrs} } ) { | |||||||||
| 120 | |||||||||||
| 121 | # in older versions, code was not automatically defined | ||||||||||
| 122 | # if admins defined it explicitly, do not treat it as a session | ||||||||||
| 123 | # attribute | ||||||||||
| 124 | $args->{$k} = ( | ||||||||||
| 125 | $k eq 'code' | ||||||||||
| 126 | ? $code | ||||||||||
| 127 | 0 | ▶ | : $session->{ $self->{vrfyAttrs}->{$k} } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | ); | ||||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | # Launch REST request | ||||||||||
| 132 | 0 | $self->logger->debug( $self->prefix . '2f: call verify URL' ); | |||||||||
| 133 | my $res = | ||||||||||
| 134 | 0 | eval { $self->restCall( $self->conf->{rest2fVerifyUrl}, $args ); }; | |||||||||
| 135 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | 0 | $self->logger->error( $self->prefix . "2f: error ($@)" ); | |||||||||
| 137 | 0 | return PE_ERROR; | |||||||||
| 138 | } | ||||||||||
| 139 | |||||||||||
| 140 | # Result | ||||||||||
| 141 | 0 | ▶ | unless ( $res->{result} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 142 | $self->userLogger->warn( $self->prefix | ||||||||||
| 143 | . '2f: failed for ' | ||||||||||
| 144 | 0 | . $session->{ $self->conf->{whatToTrace} } ); | |||||||||
| 145 | 0 | return PE_BADOTP; | |||||||||
| 146 | } | ||||||||||
| 147 | |||||||||||
| 148 | $self->logger->debug( | ||||||||||
| 149 | 0 | $self->prefix . '2f: credentials accepted by server' ); | |||||||||
| 150 | 0 | return PE_OK; | |||||||||
| 151 | } | ||||||||||
| 152 | |||||||||||
| 153 | 4 | 1; | |||||||||