Skip to content

Commit

Permalink
added a session object class
Browse files Browse the repository at this point in the history
  • Loading branch information
ewolf committed Oct 22, 2016
1 parent d6810c3 commit bdfe8fa
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 122 deletions.
15 changes: 12 additions & 3 deletions ServerYote/lib/Yote/Server.pm
Expand Up @@ -70,6 +70,7 @@ sub load_options {
use_ssl => 0,
SSL_cert_file => '',
SSL_key_file => '',
session_class => 'Yote::Server::Session',
};

#
Expand Down Expand Up @@ -839,6 +840,16 @@ sub _log {
Yote::Server::_log(shift);
}

#
# fetches or creates session which has a _token field
#
sub fetch_session {
my( $self, $token ) = @_;
my $session = $self->_fetch_session( $token ) || $self->_create_session;
$self->{SESSION} = $session;
$session;
}

sub _fetch_session {
my( $self, $token ) = @_;

Expand Down Expand Up @@ -903,7 +914,7 @@ sub _create_session {
#
my $session = $self->{STORE}->newobj( {
_has_ids2times => {},
_token => $token } );
_token => $token }, $self->{session_class} );
if( $slot_data->[ 0 ] == $current_time_chunk ) {
$slots->[ 0 ]{ $token } = $session;
} else {
Expand All @@ -922,7 +933,6 @@ sub _create_session {
$self->{STORE}->_stow( $slots );
$self->{STORE}->_stow( $slot_data );
$self->{STORE}->unlock( 'token_mutex' );

return $session;

} #_create_session
Expand Down Expand Up @@ -966,7 +976,6 @@ sub fetch_app {
$app = $app_name->_new( $self->{STORE} );
$apps->{$app_name} = $app;
}

return $app, $self->{SESSION} ? $self->{SESSION}->get_acct : undef;
} #fetch_app

Expand Down
1 change: 0 additions & 1 deletion ServerYote/lib/Yote/Server/App.pm
Expand Up @@ -57,7 +57,6 @@ sub login {
# doing it like this so a failed attempt has about the same amount of time
# as an attempt against a nonexistant account. maybe random microsleep?
my $pwh = crypt( $pw, length( $pw ) . Digest::MD5::md5_hex($acct ? $acct->{ID} : $self->{ID} ) );

if( $acct && $pwh eq $acct->get__password_hash ) {
# this and Yote::ServerRoot::fetch_app are the only ways to expose the account obj
# to the UI. If the UI calls for an acct object it wasn't exposed to, Yote::Server
Expand Down
199 changes: 81 additions & 118 deletions ServerYote/lib/Yote/Server/ModperlOperator.pm
Expand Up @@ -11,148 +11,111 @@ use Text::Xslate qw(mark_raw);
use Yote::Server;

sub new {
my( $pkg, $r, %options ) = @_;
my( $pkg, %options ) = @_;

#
# Setup the yote part of this
#
my $yote_root_dir = '/opt/yote';
eval {
require Yote::ConfigData;
$yote_root_dir = Yote::ConfigData->config( 'yote_root' );
};
unshift @INC, "$yote_root_dir/lib";
my $yote_options = Yote::Server::load_options( $yote_root_dir );
my $server = new Yote::Server( $yote_options );
my $store = $server->store;
my $root = $store->fetch_server_root;

my $jar = Apache2::Cookie::Jar->new($r);
my $token_cookie = $jar->cookies("token");
my $token = $token_cookie ? $token_cookie->value : 0;
my( @path ) = grep { $_ } split '/', $r->uri;

bless {
r => $r,
cookie_path => $options{cookie_path},
apps => $options{apps},
template_path => $options{template_path},
app_name => $options{app_name},
main_template => $options{main_template},
token => $token,
path => \@path,
root => $root,
tx => new Text::Xslate,
}, $pkg;

} #new

sub path {
shift->{path};
}

sub req {
shift->{r}
}
} #new

sub _load_app {
my( $self, $appname ) = @_;
sub handle_request {
my( $self, $req ) = @_;

eval('use Yote::ConfigData');
my $yote_root_dir = $@ ? '/opt/yote' : Yote::ConfigData->config( 'yote_root' );
unshift @INC, "$yote_root_dir/lib";
my( $app_path, @path ) = grep { $_ } split '/', $req->uri;

my $options = Yote::Server::load_options( $yote_root_dir );
my $server = new Yote::Server( $options );
my $store = $server->store;
my $root = $store->fetch_server_root;

my $session = $root->_fetch_session( $self->{token} ) || $self->_init_session;
$self->{session} = $session;
$root->{SESSION} = $session; # _fetch_session doens't attach the session to the root oddly. TODO - look into this.

$self->{root} = $root;
$self->{store} = $store;

my( $app, $login ) = $root->fetch_app( $appname );
if( $app ) {
$app->{SESSION} = $session;
$self->{app} = $app;
$self->{login} = $login;
my $jar = Apache2::Cookie::Jar->new($req);
my $token_cookie = $jar->cookies("token");
my $root = $self->{root};
my $appinfo = $self->{apps}{$app_path};

my( $app, $login, $session );
$session = $root ? $root->fetch_session( $token_cookie ? $token_cookie->value : 0 ) : undef;
unless( $token_cookie && $token_cookie->value eq $session->get__token ) {
my $cookie_path = $appinfo ? $appinfo->{cookie_path} : '/';
$token_cookie = Apache2::Cookie->new( $req,
-name => "token",
-path => $cookie_path,
-value => $session->get__token );

$token_cookie->bake( $req );
}
my $template = 'main';
if( $appinfo && $root ) {
$root->{SESSION} = $session;
( $app, $login ) = $root->fetch_app( $appinfo->{app_name} );
$app->{SESSION} = $session;
if( $login ) {
$login->{SESSION} = $session;
}
return $app;
$template = "$app_path/main";
}
# oh, no app here. That's not good. TODO - figure out what to do.
} #_load_app

sub _init_session {
my $self = shift;
my($root, $token) = $self->{root}->init_root;
$self->{token} = $token;
my $token_cookie = Apache2::Cookie->new( $self->{r},
-name => "token",
-path => "/$self->{cookie_path}",
-value => $self->{token} );
$token_cookie->bake( $self->{r} );
$root->{SESSION};
}

sub haslogin {
defined shift->{login};
}
my $state = {
app_info => $appinfo,
app_path => $app_path,
app => $app,
login => $login,
op => $self,
req => $req,
session => $session,
path => \@path,
template => $template,
};

sub _err {
my( $self, $err ) = @_;
$err //= $@;
if( ref $err ) {
$self->{last_err} = $err->{err};
return $err->{err};
} elsif( $err ) {
die $err;
eval {
$self->_check_actions( $state );
$self->make_page( $state );
$root->{STORE}->stow_all;
};
if( $@ ) {
print STDERR Data::Dumper->Dump([$@,"ERRY"]);
}
} #_err

sub lasterr {
shift->{last_err};
}

sub logout {
my $self = shift;
my $token_cookie = Apache2::Cookie->new( $self->{r},
-name => "token",
-path => "/$self->{cookie_path}",
-value => 0 );
$token_cookie->bake( $self->{r} );
delete $self->{token};
delete $self->{login};

#re-establish a new session
$self->_init_session;
'';
} #logout

sub login {
my $self = shift;
return $self->{login} if $self->{login};
my $r = $self->{r};
my( $un, $pw ) = ( $r->param('un'), $r->param('pw') );
my $login;
if( $un && $pw ) {
$login = $self->{app}->login( $un, $pw );
$login->{SESSION} = $self->{session};
$self->{login} = $login;
}
$login;
} #login
} #handle_request

sub tmpl {
my( $self, $tname ) = @_;
"$self->{template_path}/$tname.tx";
} #tmpl
my( $self, @path ) = @_;
join( '/', $self->{template_path}, @path ).'.tx';
}

sub _check_actions {
my( $self, $state );
# login check, et al go here
}

sub make_page {
my $self = shift;
my( $self, $state ) = @_;

my $tx = new Text::Xslate;
$self->_load_app($self->{app_name});
eval {
$self->login;
};
$self->_err;
my( @path ) = @{$self->{path}};
$self->{r}->print( $tx->render( $self->tmpl($self->{main_template}), {
op => $self, } ) );

$self->{store}->stow_all;

return $self->{rv};
my $req = $state->{req};
my $template = $state->{template};

my $html = $self->{tx}->render( $self->tmpl( $template ), $state );

$req->print( mark_raw($html) );

return OK;
} #make_page


1;

__END__
Expand Down
13 changes: 13 additions & 0 deletions ServerYote/lib/Yote/Server/Session.pm
@@ -0,0 +1,13 @@
package Yote::Server::Session;

use strict;
use warnings;

use Yote::Server;

use base 'Yote::ServerObj';


1;

__END__

0 comments on commit bdfe8fa

Please sign in to comment.