<commit_before>package FixMyStreet::App::Controller::Auth;
use Moose;
use namespace::autoclean;

BEGIN { extends 'Catalyst::Controller'; }

use Email::Valid;
use Net::Domain::TLD;
use mySociety::AuthToken;
use Digest::SHA1 qw(sha1_hex);

=head1 NAME

FixMyStreet::App::Controller::Auth - Catalyst Controller

=head1 DESCRIPTION

Controller for all the authentication related pages - create account, login,
logout.

=head1 METHODS

=head2 index

Present the user with a login / create account page. 

=cut

sub general : Path : Args(0) {
    my ( $self, $c ) = @_;
    my $req = $c->req;

    # all done unless we have a form posted to us
    return unless $req->method eq 'POST';

    # check that the email is valid - otherwise flag an error
    my $raw_email = lc( $req->param('email') || '' );
    my $email_checker = Email::Valid->new(
        -mxcheck  => 1,
        -tldcheck => 1,
        -fqdn     => 1,
    );

    if ( my $good_email = $email_checker->address($raw_email) ) {
        $c->stash->{email} = $good_email;
    }
    else {
        $c->stash->{email} = $raw_email;
        $c->stash->{email_error} =
          $raw_email ? $email_checker->details : 'missing';
        return;
    }

    # decide which action to take
    $c->detach('login')       if $req->param('login');
    $c->detach('email_login') if $req->param('email_login');

    # hmm - should not get this far. 404 so that user knows there is a problem
    # rather than it silently not working.
    $c->detach('/page_not_found');

}

=head2 login

Allow the user to legin with a username and a password.

=cut

sub login : Private {
    my ( $self, $c ) = @_;

    my $email    = $c->stash->{email}         || '';
    my $password = $c->req->param('password') || '';

    # logout just in case
    $c->logout();

    if ( $c->authenticate( { email => $email, password => $password } ) ) {
        $c->res->redirect( $c->uri_for('/my') );
        return;
    }

    # could not authenticate - show an error
    $c->stash->{login_error} = 1;
}

=head2 email_login

Email the user the details they need to log in. Don't check for an account - if
there isn't one we can create it when they come back with a token (which
contains the email addresss).

=cut

sub email_login : Private {
    my ( $self, $c ) = @_;
    my $email = $c->stash->{email};

    my $token_obj = $c->model('DB::Token')    #
      ->create(
        {
            scope => 'email_login',
            data  => { email => $email }
        }
      );

    # log the user in, send them an email and redirect to the welcome page
    $c->stash->{token} = $token_obj->token;
    $c->send_email( 'login', { to => $email } );
    $c->res->redirect( $c->uri_for('token') );
}

=head2 token

Handle the 'email_login' tokens. Find the account for the email address
(creating if needed), authenticate the user and delete the token.

=cut

sub token : Local {
    my ( $self, $c, $url_token ) = @_;

    # check for a token - if none found then return
    return unless $url_token;

    # retrieve the token or return<commit_msg>Clean up the flow - only mxcheck emails if we need to (ie not for logging in)<commit_after>package FixMyStreet::App::Controller::Auth;
use Moose;
use namespace::autoclean;

BEGIN { extends 'Catalyst::Controller'; }

use Email::Valid;
use Net::Domain::TLD;
use mySociety::AuthToken;
use Digest::SHA1 qw(sha1_hex);

=head1 NAME

FixMyStreet::App::Controller::Auth - Catalyst Controller

=head1 DESCRIPTION

Controller for all the authentication related pages - create account, login,
logout.

=head1 METHODS

=head2 index

Present the user with a login / create account page. 

=cut

sub general : Path : Args(0) {
    my ( $self, $c ) = @_;
    my $req = $c->req;

    # all done unless we have a form posted to us
    return unless $req->method eq 'POST';

    # decide which action to take
    $c->detach('email_login') if $req->param('email_login');
    $c->detach('login');    # default

}

=head2 login

Allow the user to legin with a username and a password.

=cut

sub login : Private {
    my ( $self, $c ) = @_;

    my $email    = $c->req->param('email')    || '';
    my $password = $c->req->param('password') || '';

    # logout just in case
    $c->logout();

    if (   $email
        && $password
        && $c->authenticate( { email => $email, password => $password } ) )
    {
        $c->res->redirect( $c->uri_for('/my') );
        return;
    }

    # could not authenticate - show an error
    $c->stash->{login_error} = 1;
}

=head2 email_login

Email the user the details they need to log in. Don't check for an account - if
there isn't one we can create it when they come back with a token (which
contains the email addresss).

=cut

sub email_login : Private {
    my ( $self, $c ) = @_;

    # check that the email is valid - otherwise flag an error
    my $raw_email = lc( $c->req->param('email') || '' );

    my $email_checker = Email::Valid->new(
        -mxcheck  => 1,
        -tldcheck => 1,
        -fqdn     => 1,
    );

    my $good_email = $email_checker->address($raw_email);
    if ( !$good_email ) {
        $c->stash->{email} = $raw_email;
        $c->stash->{email_error} =
          $raw_email ? $email_checker->details : 'missing';
        return;
    }

    my $token_obj = $c->model('DB::Token')    #
      ->create(
        {
            scope => 'email_login',
            data  => { email => $good_email }
        }
      );

    # log the user in, send them an email and redirect to the welcome page
    $c->stash->{token} = $token_obj->token;
    $c->send_email( 'login', { to => $good_email } );
    $c->res->redirect( $c->uri_for('token') );
}

=head2 token

Handle the 'email_login' tokens. Find the account for the email address
(creating if needed), authenticate the user and delete the token.

=cut

sub token : Local {
    my ( $self, $c, $url_token ) = @_;

    # check for a token - if none found then return
    return unless $url_token;

    # retrieve the token or return
{'fraction_non_alphanumeric': 0.14402400400066678, 'fraction_numerical': 0.00516752792132022, 'mean_word_length': 2.678724708767627, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 94, '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': '1072727', 'n_tokens_mistral': 2037, 'n_tokens_neox': 1964, 'n_words': 626}