<commit_before>package Bio::KBase::AuthTokenLite;

use strict;
use warnings;
use JSON;
use Bio::KBase::Auth;
use LWP::UserAgent;
use POSIX;

# We use Object::Tiny::RW to generate getters/setters for the attributes
# and save ourselves some tedium
use Object::Tiny::RW qw {
    error_message
    user_id
    password
    client_secret
    sshagent_keys
    sshagent_keyname
};

# This is the name of the environment variable that contains a
# pregenerated token
our $TokenEnv = "KB_AUTH_TOKEN";

# Your typical constructor - takes a hash that specifies the initial values to
# plug into the object.
# A special attribute is "ignore_kbase_config", if that it set then we will not bother
# trying to read the ~/.kbase_config file
sub new {
    my $class = shift;

    # Don't bother with calling the Object::Tiny::RW constructor,
    # since it doesn't do anything except return a blessed empty hash
    my $self = $class->SUPER::new(
        'token' => undef,
        'error_message' => undef,
        @_
    );
    
    my $conf_path = "$ENV{HOME}/.kbase_config";

    if (defined($ENV{ KB_CLIENT_CONFIG })) {
	if ( -r $ENV{ KB_CLIENT_CONFIG }) {
	    $conf_path = $ENV{ KB_CLIENT_CONFIG };
	} else {
	    die "\$ENV{KB_CLIENT_CONFIG} points to an unreadable file: ".$ENV{ KB_CLIENT_CONFIG };
	}
    }

    eval {
	my $conf = Config::Simple->new( filename => $conf_path);
	my %c = $conf->vars();
	
	# make ignore_kbase_config an alias for ignore_authrc if it isn't specified
	if ( !exists( $self->{'ignore_kbase_config'}) &&
	     exists( $self->{'ignore_authrc'})) {
	    $self->{'ignore_kbase_config'} = $self->{'ignore_authrc'};
	}

	# If we were given a token, try set that using the formal setter
	# elsif we have appropriate login credentials, try to get a
	# token
	if ($self->{'token'}) {
	    $self->token( $self->{'token'});
	} elsif ( defined( $ENV{$TokenEnv})) {
	    $self->token($ENV{$TokenEnv});
	} elsif (! $self->{'ignore_kbase_config'}) {
	    
	    # If we get a token, use that immediately and ignore the rest,
	    # otherwise set the other attributes and fetch the token
	    if (exists( $c{ 'authentication.token'})) {
		$self->token( $c{'authentication.token'});
	    }
	}
    };
    if ($@) {
	$self->error_message("Failed to acquire token: $@");
    }
    return($self);
}

# getter/setter for token, if we are given a token, parse it out
# and set the appropriate attributes <commit_msg>Lite token fixes.<commit_after>package Bio::KBase::AuthTokenLite;

use strict;
use warnings;
use JSON;
use Bio::KBase::Auth;
use LWP::UserAgent;
use POSIX;

# We use Object::Tiny::RW to generate getters/setters for the attributes
# and save ourselves some tedium
use Object::Tiny::RW qw {
    error_message
    user_id
    password
    client_secret
    sshagent_keys
    sshagent_keyname
};

# This is the name of the environment variable that contains a
# pregenerated token
our $TokenEnv = "KB_AUTH_TOKEN";
our $token_lifetime = 0;

# Your typical constructor - takes a hash that specifies the initial values to
# plug into the object.
# A special attribute is "ignore_kbase_config", if that it set then we will not bother
# trying to read the ~/.kbase_config file
sub new {
    my $class = shift;

    # Don't bother with calling the Object::Tiny::RW constructor,
    # since it doesn't do anything except return a blessed empty hash
    my $self = $class->SUPER::new(
        'token' => undef,
        'error_message' => undef,
        @_
    );
    
    my $conf_path = "$ENV{HOME}/.kbase_config";

    if (defined($ENV{ KB_CLIENT_CONFIG })) {
	if ( -r $ENV{ KB_CLIENT_CONFIG }) {
	    $conf_path = $ENV{ KB_CLIENT_CONFIG };
	} else {
	    die "\$ENV{KB_CLIENT_CONFIG} points to an unreadable file: ".$ENV{ KB_CLIENT_CONFIG };
	}
    }

    eval {
	my $conf = Config::Simple->new( filename => $conf_path);
	my %c = $conf->vars();
	
	# make ignore_kbase_config an alias for ignore_authrc if it isn't specified
	if ( !exists( $self->{'ignore_kbase_config'}) &&
	     exists( $self->{'ignore_authrc'})) {
	    $self->{'ignore_kbase_config'} = $self->{'ignore_authrc'};
	}

	# If we were given a token, try set that using the formal setter
	# elsif we have appropriate login credentials, try to get a
	# token
	if ($self->{'token'}) {
	    $self->token( $self->{'token'});
	} elsif ( defined( $ENV{$TokenEnv})) {
	    $self->token($ENV{$TokenEnv});
	} elsif (! $self->{'ignore_kbase_config'}) {
	    
	    # If we get a token, use that immediately and ignore the rest,
	    # otherwise set the other attributes and fetch the token
	    if (exists( $c{ 'authentication.token'})) {
		$self->token( $c{'authentication.token'});
		$self->validate();
	    }
	}
    };
    if ($@) {
	$self->error_message("Failed to acquire token: $@");
    }
    return($self);
}

sub validate {
    my $self = shift;
    my %p = @_;

    eval {
	unless ($self->{'token'}) {
	    die "No token.";
	}

	my ($sig_data) = $self->{'token'} =~ /^(.*)\|sig=/;
	unless ($sig_data) {
	    die "Token lacks signature fields";
	}
	my %vars = map { split /=/ } split /\|/, $self->{'token'};
	unless (defined($p{'lifetime'})) {
	    $p{'lifetime'} = $token_lifetime;
	}
	unless (($vars{'expiry'} + $p{'lifetime'}) >= time) {
	    die "Token expired at: ".scalar( localtime($vars{'expiry'} + $p{'lifetime'})) ;
	}
	unless (length($vars{'sig'}) == 256) {
	    die "Token has malformed signature field";
	}
    };
    if ($@) {
	$self->error_message("Failed to verify token: $@");
	return( undef);
    } else {
	$self->{'error_message'} = undef;
	return( 1 );
    }
}


# getter/setter for token, if we are given a token, parse it out
# and set the appropriate attributes 
{'fraction_non_alphanumeric': 0.14672566371681417, 'fraction_numerical': 0.0008849557522123894, 'mean_word_length': 3.418295543393276, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 44, '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': '6604286', 'n_tokens_mistral': 1954, 'n_tokens_neox': 1874, 'n_words': 576}