| 1 |
Protect your application |
| 2 |
======================== |
| 3 |
|
| 4 |
Presentation |
| 5 |
------------ |
| 6 |
|
| 7 |
Your application can know the connected user using: |
| 8 |
|
| 9 |
- REMOTE_USER environment variable (with local Handler or SetEnvIf |
| 10 |
trick) |
| 11 |
- HTTP header (in all cases) |
| 12 |
|
| 13 |
To get more information on user (name, mail, etc.), you have to read |
| 14 |
:ref:`HTTP headers<headers>`. |
| 15 |
|
| 16 |
|
| 17 |
.. tip:: |
| 18 |
|
| 19 |
If your application is based on `Perl CGI package |
| 20 |
<http://search.cpan.org/perldoc?CGI>`__, you can simply replace CGI by |
| 21 |
:ref:`Lemonldap::NG::Handler::CGI<selfmadeapplication-perl-auto-protected-cgi>` |
| 22 |
|
| 23 |
Code snippet |
| 24 |
------------ |
| 25 |
|
| 26 |
Examples with a :ref:`configured header<headers>` named |
| 27 |
'Auth-User': |
| 28 |
|
| 29 |
Perl |
| 30 |
~~~~ |
| 31 |
|
| 32 |
.. code-block:: perl |
| 33 |
|
| 34 |
print "Connected user: " . $env->{HTTP_AUTH_USER}; |
| 35 |
|
| 36 |
PHP |
| 37 |
~~~ |
| 38 |
|
| 39 |
.. code-block:: php |
| 40 |
|
| 41 |
print "Connected user: ".$_SERVER["HTTP_AUTH_USER"]; |
| 42 |
|
| 43 |
.. _selfmadeapplication-perl-auto-protected-cgi: |
| 44 |
|
| 45 |
Perl auto-protected CGI |
| 46 |
----------------------- |
| 47 |
|
| 48 |
LL::NG now uses FastCGI instead of CGI, but you still can write your own |
| 49 |
protected CGI. |
| 50 |
|
| 51 |
First create a PSGI module based on Lemonldap::NG::Handler: |
| 52 |
|
| 53 |
.. code-block:: perl |
| 54 |
|
| 55 |
package My::PSGI; |
| 56 |
|
| 57 |
use base "Lemonldap::NG::Handler::PSGI"; # or Lemonldap::NG::Handler::PSGI::OAuth2, etc… |
| 58 |
|
| 59 |
sub init { |
| 60 |
my ($self,$args) = @_; |
| 61 |
$self->protection('manager'); |
| 62 |
$self->SUPER::init($args) or return 0; |
| 63 |
$self->staticPrefix("/static"); |
| 64 |
$self->templateDir("/usr/share/lemonldap-ng/portal/templates"); |
| 65 |
# See Lemonldap::NG::Common::PSGI for more |
| 66 |
#... |
| 67 |
# Return a boolean. If false, then error message has to be stored in |
| 68 |
# $self->error |
| 69 |
return 1; |
| 70 |
} |
| 71 |
|
| 72 |
sub handler { |
| 73 |
my ( $self, $req ) = @_; |
| 74 |
|
| 75 |
# Will be called only if authorisated |
| 76 |
my $userId = $self->userId($req); |
| 77 |
#... |
| 78 |
|
| 79 |
# Return JSON |
| 80 |
# $self->sendJSONresponse(...); |
| 81 |
|
| 82 |
# or Return HTML |
| 83 |
$self->sendHtml($req, "myskin/mytemplate", ( params => { 'userId' => $userId }) ); |
| 84 |
} |
| 85 |
|
| 86 |
They create a FCGI script like this: |
| 87 |
|
| 88 |
.. code-block:: perl |
| 89 |
|
| 90 |
#!/usr/bin/env perl |
| 91 |
|
| 92 |
use My::PSGI; |
| 93 |
use Plack::Handler::FCGI; |
| 94 |
|
| 95 |
Plack::Handler::FCGI->new->run( My::PSGI->run() ); |
| 96 |
|
| 97 |
See our LLNG Nginx/Apache configurations to see how to launch it or read |
| 98 |
`PSGI/Plack documentation <https://plackperl.org/>`__. |
| 99 |
|
| 100 |
The protection parameter must be set when calling the init() method: |
| 101 |
|
| 102 |
- ``none``: no protection |
| 103 |
- ``authenticate``: check authentication but do not manage |
| 104 |
authorization |
| 105 |
- ``manager``: rely on virtual host configuration in Manager |
| 106 |
- ``rule: xxx``: apply a specific rule |