CrowdSec-Client

 view release on metacpan or  search on metacpan

lib/CrowdSec/Client.pm  view on Meta::CPAN

package CrowdSec::Client;

use strict;
use Date::Parse;
use HTTP::Request::Common;
use JSON;
use LWP::UserAgent;
use Moo;
use POSIX "strftime";

our $VERSION = '0.04';

# Default alert template
our %DEFAULTS = (
    duration  => '4h',
    labels    => undef,
    origin    => 'CrowdSec::Client',
    reason    => 'Banned by CrowdSec::Client',
    scenario  => 'Banned by CrowdSec::Client',
    simulated => JSON::false,
    type      => 'ban',
);

# Watcher accessors
has machineId => ( is => 'ro' );
has password  => ( is => 'ro' );
has token     => ( is => 'rw' );
has tokenVal  => ( is => 'rw' );
has strictSsl => ( is => 'ro', default => 1, );
has baseUrl   => ( is => 'ro' );

# Bouncer accessors
has apiKey => ( is => 'ro' );

# Common accessors
has userAgent => (
    is      => 'ro',
    default => sub {
        return LWP::UserAgent->new(
            $_[0]->strictSsl
            ? ()
            : (
                ssl_opts => {
                    verify_hostname => 0,
                    SSL_verify_mode => 0
                }
            )
        );
    }
);

has autoLogin => ( is => 'ro' );

has error => ( is => 'rw' );

sub login {
    my ($self) = @_;
    my $error = 0;
    return 1 if $self->tokenIsvalid;
    foreach my $k (qw(machineId password baseUrl)) {
        unless ( $self->{$k} ) {
            $self->error("Missing parameter: $k");
            $error++;
        }
    }



( run in 1.809 second using v1.01-cache-2.11-cpan-e1769b4cff6 )