« blib/lib/Lemonldap/NG/Portal/Lib/CrowdSecFilter.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Lib/Notifications/JSON.pm »
| 1 | package Lemonldap::NG::Portal::Plugins::DecryptValue; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Mouse; | ||
| 5 | use Lemonldap::NG::Portal::Main::Constants qw( | ||
| 6 | PE_NOTOKEN | ||
| 7 | PE_TOKENEXPIRED | ||
| 8 | PE_DECRYPTVALUE_SERVICE_NOT_ALLOWED | ||
| 9 | ); | ||
| 10 | |||
| 11 | our $VERSION = '2.22.0'; | ||
| 12 | |||
| 13 | extends qw( | ||
| 14 | Lemonldap::NG::Portal::Main::Plugin | ||
| 15 | Lemonldap::NG::Portal::Lib::_tokenRule | ||
| 16 | ); | ||
| 17 | |||
| 18 | # INITIALIZATION | ||
| 19 | has rule => ( is => 'rw', default => sub { 0 } ); | ||
| 20 | has ott => ( | ||
| 21 | is => 'rw', | ||
| 22 | lazy => 1, | ||
| 23 | default => sub { | ||
| 24 | my $ott = | ||
| 25 | $_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken'); | ||
| 26 | $ott->timeout( $_[0]->{conf}->{formTimeout} ); | ||
| 27 | return $ott; | ||
| 28 | } | ||
| 29 | ); | ||
| 30 | |||
| 31 | sub init { | ||
| 32 | my ($self) = @_; | ||
| 33 | $self->addAuthRoute( decryptvalue => 'run', ['POST'] ) | ||
| 34 | ->addAuthRouteWithRedirect( decryptvalue => 'display', ['GET'] ); | ||
| 35 | |||
| 36 | # Parse activation rule | ||
| 37 | $self->rule( | ||
| 38 | $self->p->buildRule( $self->conf->{decryptValueRule}, 'decryptValue' ) | ||
| 39 | ); | ||
| 40 | return $self->rule ? 1 : 0; | ||
| 41 | } | ||
| 42 | |||
| 43 | # RUNNING METHOD | ||
| 44 | sub display { | ||
| 45 | my ( $self, $req ) = @_; | ||
| 46 | |||
| 47 | # Check access rules | ||
| 48 | unless ( $self->rule->( $req, $req->userData ) ) { | ||
| 49 | $self->userLogger->warn('decryptValue service NOT authorized'); | ||
| 50 | return $self->p->doPE( $req, PE_DECRYPTVALUE_SERVICE_NOT_ALLOWED ); | ||
| 51 | } | ||
| 52 | |||
| 53 | # Display form | ||
| 54 | my $params = { | ||
| 55 | FORM_ACTION => $self->p->relativeUrl( $req, 'decryptvalue' ), | ||
| 56 | MSG => 'decryptCipheredValue', | ||
| 57 | ALERTE => 'alert-warning', | ||
| 58 | TOKEN => ( | ||
| 59 | $self->ottRule->( $req, {} ) | ||
| 60 | ? $self->ott->createToken() | ||
| 61 | : '' | ||
| 62 | ) | ||
| 63 | }; | ||
| 64 | return $self->sendJSONresponse( $req, $params ) if ( $req->wantJSON ); | ||
| 65 | |||
| 66 | # Display form | ||
| 67 | return $self->p->sendHtml( $req, 'decryptvalue', params => $params ); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub run { | ||
| 71 | my ( $self, $req ) = @_; | ||
| 72 | my $msg = my $decryptedValue = ''; | ||
| 73 | |||
| 74 | # Check access rules | ||
| 75 | unless ( $self->rule->( $req, $req->userData ) ) { | ||
| 76 | $self->userLogger->warn('decryptValue service NOT authorized'); | ||
| 77 | return $self->p->doPE( $req, PE_DECRYPTVALUE_SERVICE_NOT_ALLOWED ); | ||
| 78 | } | ||
| 79 | |||
| 80 | # Check token | ||
| 81 | if ( $self->ottRule->( $req, {} ) ) { | ||
| 82 | my $token; | ||
| 83 | if ( $token = $req->param('token') ) { | ||
| 84 | unless ( $self->ott->getToken($token) ) { | ||
| 85 | $self->userLogger->warn( | ||
| 86 | 'DecryptValue called with an expired/bad token'); | ||
| 87 | $msg = PE_TOKENEXPIRED; | ||
| 88 | $token = $self->ott->createToken(); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | else { | ||
| 92 | $self->userLogger->warn('DecryptValue called without token'); | ||
| 93 | $msg = PE_NOTOKEN; | ||
| 94 | $token = $self->ott->createToken(); | ||
| 95 | } | ||
| 96 | |||
| 97 | my $params = { | ||
| 98 | FORM_ACTION => $self->p->relativeUrl( $req, 'decryptvalue' ), | ||
| 99 | MSG => "PE$msg", | ||
| 100 | ALERTE => 'alert-warning', | ||
| 101 | TOKEN => $token, | ||
| 102 | }; | ||
| 103 | return $self->p->sendJSONresponse( $req, $params ) | ||
| 104 | if $req->wantJSON; | ||
| 105 | return $self->p->sendHtml( $req, 'decryptvalue', params => $params ) | ||
| 106 | if $msg; | ||
| 107 | } | ||
| 108 | |||
| 109 | my $cipheredValue = $req->param('cipheredValue') || ''; | ||
| 110 | $self->logger->debug("decryptValue tried with value: $cipheredValue"); | ||
| 111 | |||
| 112 | if ($cipheredValue) { | ||
| 113 | if ( $self->conf->{decryptValueFunctions} | ||
| 114 | and $self->conf->{decryptValueFunctions} =~ | ||
| 115 | qr/^(?:\w+(?:::\w+)*(?:\s+\w+(?:::\w+)*)*)?$/ ) | ||
| 116 | { | ||
| 117 | foreach ( split( /\s+/, $self->{conf}->{decryptValueFunctions} ) ) { | ||
| 118 | $self->userLogger->notice( | ||
| 119 | "Try to decrypt value with function: $_"); | ||
| 120 | /^([\w:{2}]*?)(?:::)?(?:\w+)$/; | ||
| 121 | eval "require $1"; | ||
| 122 | $self->logger->debug("Unable to load decrypt module: $@") | ||
| 123 | if ($@); | ||
| 124 | my $key = $self->conf->{key}; | ||
| 125 | $decryptedValue = eval "$_" . '($cipheredValue, $key)' | ||
| 126 | unless ($@); | ||
| 127 | $self->logger->debug( | ||
| 128 | $@ | ||
| 129 | ? "Unable to eval decrypt function: $@" | ||
| 130 | : "Decrypted value = $decryptedValue" | ||
| 131 | ); | ||
| 132 | last if $decryptedValue; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | else { | ||
| 136 | $self->userLogger->notice("Malformed decrypt functions") | ||
| 137 | if $self->conf->{decryptValueFunctions}; | ||
| 138 | $self->userLogger->notice( | ||
| 139 | "Try to decrypt value with internal LL::NG decrypt function"); | ||
| 140 | $decryptedValue = | ||
| 141 | $self->p->HANDLER->tsv->{cipher}->decrypt($cipheredValue); | ||
| 142 | $self->logger->debug( | ||
| 143 | $@ | ||
| 144 | ? "Unable to decrypt value: $@" | ||
| 145 | : "Decrypted value = $decryptedValue" | ||
| 146 | ); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | # Display form | ||
| 151 | my $params = { | ||
| 152 | FORM_ACTION => $self->p->relativeUrl( $req, 'decryptvalue' ), | ||
| 153 | MSG => 'decryptCipheredValue', | ||
| 154 | DECRYPTED => ( | ||
| 155 | $decryptedValue ? $decryptedValue | ||
| 156 | : 'notAnEncryptedValue' | ||
| 157 | ), | ||
| 158 | DALERTE => ( | ||
| 159 | $decryptedValue ? 'alert-info' | ||
| 160 | : 'alert-danger' | ||
| 161 | ), | ||
| 162 | ALERTE => 'alert-warning', | ||
| 163 | TOKEN => ( | ||
| 164 | $self->ottRule->( $req, {} ) ? $self->ott->createToken() | ||
| 165 | : '' | ||
| 166 | ) | ||
| 167 | }; | ||
| 168 | return $self->p->sendJSONresponse( $req, $params ) if ( $req->wantJSON ); | ||
| 169 | |||
| 170 | # Display form | ||
| 171 | return $self->p->sendHtml( $req, 'decryptvalue', params => $params ); | ||
| 172 | } | ||
| 173 | |||
| 174 | sub displayLink { | ||
| 175 | my ( $self, $req ) = @_; | ||
| 176 | return $self->rule->( $req, $req->userData ); | ||
| 177 | } | ||
| 178 | |||
| 179 | 1; |