<commit_before>package MediaWords::Controller::Api::V2::Auth;

# api controller for handling API key generation

use strict;
use warnings;

use Modern::Perl "2015";
use MediaWords::CommonLibs;

use base 'Catalyst::Controller';

use MediaWords::Controller::Api::V2::MC_Controller_REST;
use MediaWords::DBI::Auth;
use MediaWords::Util::URL;

use Moose;
use namespace::autoclean;

BEGIN { extends 'MediaWords::Controller::Api::V2::MC_Controller_REST' }

__PACKAGE__->config(    #
    action => {         #
        register => { Does => [ qw( ~AdminAuthenticated ~Throttled ~Logged ) ] },
        single   => { Does => [ qw( ~AdminReadAuthenticated ~Throttled ~Logged ) ] },
        login    => { Does => [ qw( ~AdminReadAuthenticated ~Throttled ~Logged ) ] },
        profile  => { Does => [ qw( ~PublicApiKeyAuthenticated ~Throttled ~Logged ) ] },
    }
);

sub register : Local : ActionClass('MC_REST')
{
}

sub register_GET : PathPrefix( '/api' )
{
    my ( $self, $c ) = @_;

    my $db = $c->dbis;

    my $data = $c->req->data;

    my $email = $data->{ email };
    unless ( $email )
    {
        die "'email' is not set.";
    }

    my $password = $data->{ password };
    unless ( $password )
    {
        die "'password' is not set.";
    }

    my $full_name = $data->{ full_name };
    unless ( $full_name )
    {
        die "'full_name' is not set.";
    }

    my $notes = $data->{ notes };
    unless ( defined $notes )
    {
        die "'notes' is undefined (should be at least an empty string).";
    }

    my $subscribe_to_newsletter = $data->{ subscribe_to_newsletter };
    unless ( defined $subscribe_to_newsletter )
    {
        die "'subscribe_to_newsletter' is undefined (should be at least an empty string).";
    }

    my $activation_url = $data->{ activation_url };
    unless ( $activation_url )
    {
        die "'activation_url' is not set.";
    }

    unless ( MediaWords::Util::URL::is_http_url( $activation_url ) )
    {
        die "'activation_url' does not look like a HTTP URL.";
    }

    eval {
        my $new_user = MediaWords::DBI::Auth::User::NewUser->new(
            email                   => $email,
            full_name               => $full_name,
            notes                   => $notes,
            password                => <PASSWORD>,
            password_repeat         => <PASSWORD>,
            role_ids                => MediaWords::DBI::Auth::Roles::default_role_ids( $db ),
            subscribe_to_newsletter => $subscribe_to_newsletter,

            # User has to activate own account via email
            active         => 0,
            activation_url => $activation_url,
        );
        MediaWords::DBI::Auth::Register::add_user( $db, $new_user );
    };
    if ( $@ )
    {
        die "Unable to add user: $@";
    }

    $self->status_ok( $c, entity => { 'success' => 1 } );
}

sub login : Local : ActionClass('MC_REST')
{
}

sub single : Local : ActionClass('MC_REST')
{
}

sub login_GET : PathPrefix( '/api' )
{
    my ( $self, $c ) = @_;

    my $db = $c->dbis;

    my $data     = $c->req->data;
    my $email    = $data->{ username };
    my $password = $data->{ password };

    my $api_key;
    eval {
        $api_key = MediaWords::DBI::Auth::Login::login_with_email_password_get_ip_api_key(
            $db,                        #
            $email,                     #
            $password,                  #
            $c->request_ip_address()    #
        );
    };
    if ( $@ or ( !$api_key ) )
    {
        die "User '$email' was not found or password is incorrect.";
    }

    $self->status_ok( $c, entity => { 'api_key' => $api_key } );
}

sub single_GET : PathPrefix( '/api' )
{
    my ( $self, $c ) = @_;

    my $db = $c->dbis;

    my $email    = $c->req->params->{ username };
    my $password = $c->req->params->{ password };

    my $api_key;
    eval {
        $api_key = MediaWords::DBI::Auth::Login::login_with_email_password_get_ip_api_key(
            $db,                        #
            $email,                     #
            $password,                  #
            $c->request_ip_address()    #
        );
    };
    if ( $@ or ( !$api_key ) )
    {
        $self->status_ok( $c, entity => [ { 'result' => 'not found' } ] );
        return;
    }
<commit_msg>Make auth/login API call return "success": 1<commit_after>package MediaWords::Controller::Api::V2::Auth;

# api controller for handling API key generation

use strict;
use warnings;

use Modern::Perl "2015";
use MediaWords::CommonLibs;

use base 'Catalyst::Controller';

use MediaWords::Controller::Api::V2::MC_Controller_REST;
use MediaWords::DBI::Auth;
use MediaWords::Util::URL;

use Moose;
use namespace::autoclean;

BEGIN { extends 'MediaWords::Controller::Api::V2::MC_Controller_REST' }

__PACKAGE__->config(    #
    action => {         #
        register => { Does => [ qw( ~AdminAuthenticated ~Throttled ~Logged ) ] },
        single   => { Does => [ qw( ~AdminReadAuthenticated ~Throttled ~Logged ) ] },
        login    => { Does => [ qw( ~AdminReadAuthenticated ~Throttled ~Logged ) ] },
        profile  => { Does => [ qw( ~PublicApiKeyAuthenticated ~Throttled ~Logged ) ] },
    }
);

sub register : Local : ActionClass('MC_REST')
{
}

sub register_GET : PathPrefix( '/api' )
{
    my ( $self, $c ) = @_;

    my $db = $c->dbis;

    my $data = $c->req->data;

    my $email = $data->{ email };
    unless ( $email )
    {
        die "'email' is not set.";
    }

    my $password = $data->{ password };
    unless ( $password )
    {
        die "'password' is not set.";
    }

    my $full_name = $data->{ full_name };
    unless ( $full_name )
    {
        die "'full_name' is not set.";
    }

    my $notes = $data->{ notes };
    unless ( defined $notes )
    {
        die "'notes' is undefined (should be at least an empty string).";
    }

    my $subscribe_to_newsletter = $data->{ subscribe_to_newsletter };
    unless ( defined $subscribe_to_newsletter )
    {
        die "'subscribe_to_newsletter' is undefined (should be at least an empty string).";
    }

    my $activation_url = $data->{ activation_url };
    unless ( $activation_url )
    {
        die "'activation_url' is not set.";
    }

    unless ( MediaWords::Util::URL::is_http_url( $activation_url ) )
    {
        die "'activation_url' does not look like a HTTP URL.";
    }

    eval {
        my $new_user = MediaWords::DBI::Auth::User::NewUser->new(
            email                   => $email,
            full_name               => $full_name,
            notes                   => $notes,
            password                => <PASSWORD>,
            password_repeat         => <PASSWORD>,
            role_ids                => MediaWords::DBI::Auth::Roles::default_role_ids( $db ),
            subscribe_to_newsletter => $subscribe_to_newsletter,

            # User has to activate own account via email
            active         => 0,
            activation_url => $activation_url,
        );
        MediaWords::DBI::Auth::Register::add_user( $db, $new_user );
    };
    if ( $@ )
    {
        die "Unable to add user: $@";
    }

    $self->status_ok( $c, entity => { 'success' => 1 } );
}

sub login : Local : ActionClass('MC_REST')
{
}

sub single : Local : ActionClass('MC_REST')
{
}

sub login_GET : PathPrefix( '/api' )
{
    my ( $self, $c ) = @_;

    my $db = $c->dbis;

    my $data     = $c->req->data;
    my $email    = $data->{ username };
    my $password = $data->{ password };

    my $api_key;
    eval {
        $api_key = MediaWords::DBI::Auth::Login::login_with_email_password_get_ip_api_key(
            $db,                        #
            $email,                     #
            $password,                  #
            $c->request_ip_address()    #
        );
    };
    if ( $@ or ( !$api_key ) )
    {
        die "User '$email' was not found or password is incorrect.";
    }

    $self->status_ok( $c, entity => { 'success' => 1, 'api_key' => $api_key } );
}

sub single_GET : PathPrefix( '/api' )
{
    my ( $self, $c ) = @_;

    my $db = $c->dbis;

    my $email    = $c->req->params->{ username };
    my $password = $c->req->params->{ password };

    my $api_key;
    eval {
        $api_key = MediaWords::DBI::Auth::Login::login_with_email_password_get_ip_api_key(
            $db,                        #
            $email,                     #
            $password,                  #
            $c->request_ip_address()    #
        );
    };
    if ( $@ or ( !$api_key ) )
    {
        $self->status_ok( $c, entity => [ { 'result' => 'not found' } ] );
        return;
    }

{'fraction_non_alphanumeric': 0.15168344324887192, 'fraction_numerical': 0.0023140113386555593, 'mean_word_length': 1.9592605272167065, 'pattern_counts': {'":': 1, '<': 7, '<?xml version=': 0, '>': 112, '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': '5382563', 'n_tokens_mistral': 2871, 'n_tokens_neox': 2748, 'n_words': 622}