<commit_before>package Netdisco::Web::AuthN;

use Dancer ':syntax';
use Dancer::Plugin::DBIC;

use Digest::MD5 ();

hook 'before' => sub {
    if (! session('user') && request->path !~ m{/login$}) {
        if (setting('environment') eq 'development' and setting('no_auth')) {
            session(user => 'developer');
        }
        else {
            var(requested_path => request->path);
            request->path_info('/');
        }
    }
};

post '/login' => sub {
    if (param('username') and param('password')) {
        my $user = schema('netdisco')->resultset('User')->find(param('username'));
        if ($user) {
            my $sum = Digest::MD5::md5_hex(param('password'));
            if ($sum and $sum eq $user->password) {
                session(user => $user->username);
                # redirect param('path') || '/'; FIXME requested_path?
                redirect uri_for('/');
                return;
            }
        }
    }
    redirect '/?failed=1';
};

get '/logout' => sub {
    session->destroy;
    redirect uri_for('/', {logout => 1});
};

true;<commit_msg>some debugging for dotcloud<commit_after>package Netdisco::Web::AuthN;

use Dancer ':syntax';
use Dancer::Plugin::DBIC;

use Digest::MD5 ();

hook 'before' => sub {
    if (! session('user') && request->path !~ m{/login$}) {
        if (setting('environment') eq 'development' and setting('no_auth')) {
            session(user => 'developer');
        }
        else {
            var(requested_path => request->path);
            request->path_info('/');
        }
    }
    # use Data::Dumper;
    # Dancer::Logger:core( Dumper request );
};

post '/login' => sub {
    if (param('username') and param('password')) {
        my $user = schema('netdisco')->resultset('User')->find(param('username'));
        if ($user) {
            my $sum = Digest::MD5::md5_hex(param('password'));
            if ($sum and $sum eq $user->password) {
                session(user => $user->username);
                # session(host => request->header('x-forwarded-for'));
                # redirect param('path') || '/'; FIXME requested_path?
                redirect uri_for('/');
                return;
            }
        }
    }
    redirect '/?failed=1';
};

get '/logout' => sub {
    session->destroy;
    redirect uri_for('/', {logout => 1});
};

true;
{'fraction_non_alphanumeric': 0.1895229982964225, 'fraction_numerical': 0.004258943781942078, 'mean_word_length': 2.213406292749658, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4020767', 'n_tokens_mistral': 716, 'n_tokens_neox': 696, 'n_words': 147}