App-MFILE-WWW
view release on metacpan or search on metacpan
lib/App/MFILE/WWW/Resource.pm view on Meta::CPAN
$r .= ' exports: "QUnit",';
$r .= ' init: function () {';
$r .= ' QUnit.config.autoload = false;';
$r .= ' QUnit.config.autostart = false;';
$r .= ' }';
$r .= ' }';
$r .= '},';
# default waitSeconds is just 7, which is too little for production
$r .= 'waitSeconds: 30';
# end of require.config
$r .= "});";
# initialize configuration parameters that we need on JavaScript side
$r .= 'requirejs.config({ config: {';
$r .= '\'cf\': { ';
# appName, appVersion
$r .= 'appName: \'' . $site->MFILE_APPNAME . '\',';
$r .= 'appVersion: \'' . $meta->META_MFILE_APPVERSION . '\',';
# standaloneMode (boolean; false means "derived distro mode")
$r .= 'standaloneMode: \'' . ( $meta->META_WWW_STANDALONE_MODE ? 'true' : 'false' ) . '\',';
# currentUser
$r .= "currentUser: " . ( $ce ? to_json( $ce ) : 'null' ) . ',';
$r .= "currentUserPriv: " . ( $cepriv ? "\'$cepriv\'" : 'null' ) . ',';
# loginDialog
$r .= 'loginDialogChallengeText: \'' . $site->MFILE_WWW_LOGIN_DIALOG_CHALLENGE_TEXT . '\',';
$r .= 'loginDialogMaxLengthUsername: ' . $site->MFILE_WWW_LOGIN_DIALOG_MAXLENGTH_USERNAME . ',';
$r .= 'loginDialogMaxLengthPassword: ' . $site->MFILE_WWW_LOGIN_DIALOG_MAXLENGTH_PASSWORD . ',';
# session data
$r .= 'displaySessionData: ' . ( $site->MFILE_WWW_DISPLAY_SESSION_DATA ? 'true' : 'false' ) . ',';
if ( $site->MFILE_WWW_DISPLAY_SESSION_DATA ) {
$r .= 'sessionID: \'' . $self->session_id . '\',';
$r .= 'sessionLastSeen: \'' . ( exists $self->session->{'last_seen'} ? $self->session->{'last_seen'} : 'never' ) . '\',';
}
# REST server URI
if ( defined( $site->DOCHAZKA_WWW_BACKEND_URI ) ) {
$r .= 'restURI: \'' . $site->DOCHAZKA_WWW_BACKEND_URI . '\',';
}
# dummyParam in last position so we don't have to worry about comma/no comma
$r .= 'dummyParam: null,';
# unit tests running?
$r .= "testing: " . ( $testing ? 'true' : 'false' );
$r .= '} } });';
$r .= '</script>';
return $r;
}
=head2 login_status
Once the username and password are known (either from C<process_post> via the
login AJAX request generated by the login dialog, or from the site
configuration via the MFILE_WWW_BYPASS_LOGIN_DIALOG mechanism), the
C<validate_user_credentials> method is called. That method is implemented by
the derived application, so it can validate user credentials however it likes.
The C<validate_user_credentials> method is then expected to call this method -
C<login_status> - to generate a status object from the results of the user
credentials validation.
Now, C<App::MFILE::WWW> does expect the C<validate_user_credentials> method to
provide the results of user credentials validation in a peculiar format,
hinging on the argument C<$code>, where a value of 200 indicates successful
validation and any other value indicates a failure.
=cut
sub login_status {
my ( $self, $code, $message, $body_json ) = @_;
my $status;
if ( $code == 200 ) {
$self->session->{'ip_addr'} = $self->remote_addr;
my $cu = $body_json->{'payload'}->{'emp'};
delete $cu->{'passhash'};
delete $cu->{'salt'};
$self->session->{'currentUser'} = $cu;
$self->session->{'currentUserPriv'} = $body_json->{'payload'}->{'priv'};
$self->session->{'last_seen'} = time;
$log->debug(
"Login successful, currentUser is now " .
Dumper( $body_json->{'payload'}->{'emp'} ) .
" and privilege level is " . $body_json->{'payload'}->{'priv'}
);
$status = $CELL->status_ok( 'MFILE_WWW_LOGIN_OK', payload => $body_json->{'payload'} );
} else {
$self->session({});
$log->debug( "Login unsuccessful, reset session" );
$status = $CELL->status_not_ok(
'MFILE_WWW_LOGIN_FAIL: %s',
args => [ $code ],
payload => { code => $code, message => $message },
);
}
$self->response->header( 'Content-Type' => 'application/json' );
$self->response->body( to_json( $status->expurgate ) );
return $status;
}
=head2 ua
Returns the LWP::UserAgent object obtained from the lookup table.
Creates it first if necessary.
=cut
sub ua {
my $self = shift;
$log->debug( "Entering " . __PACKAGE__ . "::ua()" );
my $id = $self->session_id;
$log->debug( "ua: session_id is $id" );
# already in lookup table
if ( exists $ualt->{$id} ) {
$log->debug( "Session $id already has a LWP::UserAgent object" );
return $ualt->{$id};
}
# not in lookup table yet
my $tf = "";
( undef, $tf ) = tempfile();
$ualt->{$id} = LWP::UserAgent->new;
$ualt->{$id}->cookie_jar({ file => $tf });
$log->info("New user agent created with cookies in $tf");
return $ualt->{$id};
}
=head2 rest_req
Algorithm: send request to REST server, get JSON response, decode it, return
it.
Takes a single _mandatory_ parameter: a LWP::UserAgent object
Optionally takes PARAMHASH:
server => [URI OF REST SERVER] default is 'http://0:5000'
method => [HTTP METHOD TO USE] default is 'GET'
nick => [NICK FOR BASIC AUTH] optional
password => [PASSWORD FOR BASIC AUTH] optional
path => [PATH OF REST RESOURCE] default is '/'
req_body => [HASHREF] optional
Returns HASHREF containing:
hr => HTTP::Response object (stripped of the body)
body => [BODY OF HTTP RESPONSE, IF ANY]
=cut
sub rest_req {
my $self = shift;
# process arguments
my $ua = $self->ua();
die "Bad user agent object" unless ref( $ua ) eq 'LWP::UserAgent';
my %ARGS = validate( @_, {
server => { type => SCALAR, default => 'http://localhost:5000' },
method => { type => SCALAR, default => 'GET', regex => qr/^(GET|POST|PUT|DELETE)$/ },
nick => { type => SCALAR, optional => 1 },
password => { type => SCALAR, default => '' },
path => { type => SCALAR, default => '/' },
req_body => { type => HASHREF, optional => 1 },
} );
$ARGS{'path'} =~ s/^\/*/\//;
my $r;
{
no strict 'refs';
$r = &{ $ARGS{'method'} }( $ARGS{'server'} . encode_utf8( $ARGS{'path'} ),
Accept => 'application/json' );
}
if ( $ARGS{'nick'} ) {
$r->authorization_basic( $ARGS{'nick'}, $ARGS{'password'} );
}
if ( $ARGS{'method'} =~ m/^(POST|PUT)$/ ) {
$r->header( 'Content-Type' => 'application/json' );
if ( my $body = $ARGS{'req_body'} ) {
my $tmpvar = JSON->new->utf8(0)->encode( $body );
$r->content( encode_utf8( $tmpvar ) );
}
}
# request is ready - send it and get response
my $response = $ua->request( $r );
# process response
my $body_json = $response->decoded_content;
$log->debug( "rest_req: decoded JSON body " . Dumper $body_json );
$response->content('');
my $body;
try {
$body = JSON->new->decode( $body_json );
} catch {
$body = { 'code' => $body, 'text' => $body };
};
return {
hr => $response,
body => $body
};
}
1;
( run in 0.746 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )