Need documentation on Gbrowse session handling
Dear Gbrowse list,
I have, with a little desperation, started to search look for documentation on the Gbrowse2 Session
module. In particular, I am
interested in how to *correctly* initialize and build up a Gbrowse session which is fully authorized for a
user. Let's assume the
task is to: write a cgi script (in addition to the gbrowse cgi) to initialize a gbrowse session with an
authenticated user, such that in
the next invocation of gbrowse, the user is authenticated and logged in.
I found by reading through the source code and reverse
engineering a sequence of commands that will *almost* get me there. I am seemingly missing one step only:
- the example code below works in case I already have a Gbrowse session
- the example doesn't work when there is no session at all, because subsequent calls to gbrowse make a new
session which then is not authenticated.
So, what is the correct way to initialize a Gbrowse2 Session programmatically? Am I missing something.
I'd really appreciate your help, because now I am definitely stuck.
Michael Dondrup
Postdoctoral fellow
Sea Lice Research Centre/Department of Informatics
University of Bergen
Thormøhlensgate 55, N-5008 Bergen,
Norway
#!/usr/bin/env perl
use strict;
use warnings;
use Net::SAML;
use CGI::Fast qw(:standard);
use Bio::Graphics::Browser2;
use Bio::Graphics::Browser2::Render::HTML;
while (my $q = CGI::Fast->new) {
my $username ="valid user";
# it's not really important where this comes from,
# the user information is coming from an authentication module using Net::SAML
my $fullname = "Fully Valid";
my $mail = "valid@...";
if ($username) {
my $p = "https://url";
my $myurl = "https://url/gbrowse/lsalmonis/";
my $htmlhead = <<HTML
<script src="$p/gbrowse2/js/login.js" type="text/javascript"></script>
<script src="$p/gbrowse2/js/controller.js" type="text/javascript"></script>
HTML
;
## STEP 1:
## get the session and initialize it
my $session = $render->session;
$session->lock();
$session->username($username);
$session->flush;
## STEP 2:
## authorization handling:
my $userdb = $render->userdb;
my $id = $userdb->check_or_add_named_session($session->id,$username);
$userdb->set_fullname_from_username($username=>$fullname,$mail) if defined $fullname;
warn "username ".$session->username. " stored in session ".$session->id();
# now authenticate
my ($sid, $nonce) = $render->authorize_user($username,$id, 1,undef);
my $is_authorized = $render->user_authorized_for_source($username);
## STEP 3:
## prepare for JavaScript invocation
## seems like the final step is to invoke login_load_account to make the UI aware of the login
# convert the login data to json
my $result = to_json { userOK => 1,
sessionid => $id,
username => $username,
message => 'login ok',
};
## STEP 5:
## print an intermediate web page to invoke javascript
print header(-type=>"text/html");
print start_html(-head=>$htmlhead);
print b("user $username". (($is_authorized)?" is ": "is not ") . "authorized!");
print script({-type=>'text/javascript'},
<<SCRIPT
var p = $result;
login_load_account("$myurl",p);
SCRIPT
);
$session->unlock();
print end_html();
}
------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and
their applications. This 200-page book is written by three acclaimed
leaders in the field. The early access version is available now.
Download your free book today! http://p.sf.net/sfu/neotech_d2d_may