As a Domino developer you have probably worked on a workflow application where one person creates a document, some others have to approve it and then the creator has to do another action.
To test these types of applications, you probably have a list of test IDs and you probably spend a lot of time logging in and out to see if buttons appear at the right time.
I got tired of doing this and looked into a way to speed this process up. The solution I found really saves you some time, so I hope you can use it! The final version allows you to switch to any test ID from the page you are working on (in only two clicks) and returns you to the same page.
- Single or multi-server session authentication
As discussed in other tips on this site, you can log into a database using a special "?login" URL with parameter username, password and redirectto.
For example:
http://myserver.com/mydb.nsf?Login&Username=myName&Password=wouldntyouliketoknow&RedirectTo=http://myserver.com/mydb.nsfFor this to work, you need to set session authentication to single or multi-server in your server document. You can easily see if one of these two is selected, because you can then use a custom form to log in, which is set into the domcfg.nsf. (Basic authentication, covered later, uses the standard Windows popup box). So this is nothing new -- you create a page, list all of your test IDs, put in a little JavaScript and you are on your way.
- Basic authentication
My old login page did not seem to work anymore when I joined another project. I discovered the test server document had session authentication disabled -- so no cookies where used for the login and you used Basic authentication to get into the database.
Basic authentication is a system where your password and ID are hashed and send on each HTTP request to the server to identify you.
So how can I log in an automated way? This used to be possible by creating a URL of the following format:
http://myName:myPassword@myserver.com/mydb.nsf
This has obvious issues and was disabled in Internet Explorer. As I found on one site, it is possible to re-activate this, but it is not advisable.
So what to do? Reading some other tips on this site, I saw an example that uses the Microsoft.XMLHTTP ActiveX object and guess what? You can provide a username and password to the call. What is even better -- you stay logged in with this ID on the next requests.
So using the following code, you can easily login using basic authentication:
(referingURL is the URL we are coming from) -------------------------------------------- ----------------------------------- function BasicLoginAs(id, pw) { req = new ActiveXObject ("Microsoft.XMLHTTP"); // create the object req.onreadystatechange = BasicLoginReady; // set the event to trigger when the request is ready req.open("GET", referingURL + "?openicon", true, id, pw) // do the call req.send(); } // event triggered when the icon is loaded, so the login is ready function BasicLoginReady() { if(req.readyState == 4) { // when the load is ready, open the refering url again top.document.location.href = referingURL; // if the call is complete, go back to the calling URL } } -------------------------------------------------- ----------------------------- - Getting to the page fast
At first the page was not easy to use: you needed to surf to it and then go back with the back button to the page you were working on. To speed this up, I created a Favorite with some JavaScript in it -- this is called a bookmarklet. Internet Explorer can complain when you create, but just ignore it as usual.
The code:
javascript:top.document.location.href= "file:////D:\Documents and Settings\joris bijnens\ My Documents\TestIDLogins.htm?ref=" + top.document.location.href;
This code is for if you store the page locally, you can also put it on a server -- just don't forget to escape the location string.
This Favorite takes you to the page, with the current URL in it as a parameter, so we can extract it on the test ID page and use it to go back to where you came from. If you put it in the Links folder of Internet Explorer and display the Links toolbar, you can get to it the fastest.
- Putting IDs into the page
The entire code for the page is listed below. The IDs are stored in an array, so to put yours in just add them to the array.
One entry looks like this:
el[el.length] = new Array("<string id name>", "<string password>", <integer login type>);
- <string ID name>: The name of your ID -- you can add an empty one to group the links on the page a bit.
- <string password>: The password. You can also add you own ID. Because you probably don't want to store you password in the code of the page, replace the password by "<secure>". The cell then gets a textbox where you can type in your password. Hitting Enter starts the login as normal.
- <integer login type>: 0 for basic authentication, 1 for single or multi server
- <string ID name>: The name of your ID -- you can add an empty one to group the links on the page a bit.
- How to set up the page
The only thing you need to do to get started is:
- Copy the code below and save it somewhere.
- Create the favorite in Internet Explorer and adjust the code with the location of your page.
- Put in the list of test IDs.
- Copy the code below and save it somewhere.
Enjoy!
Code: <HTML>
<body>
<STYLE>
TABLE
{
FONT-SIZE: 16pt;
WIDTH: 100%;
HEIGHT: 100%
}
TD
{
BORDER-RIGHT: black 1px solid;
BORDER-TOP: black 1px solid;
BORDER-LEFT: black 1px solid;
BORDER-BOTTOM: black 1px solid;
TEXT-ALIGN: center;
}
</STYLE>
<table id="tabMain"></table>
<SCRIPT>
var el = new Array(), referingURL, dbURL;
// get the refering url from the ref
parameter, this is the document where we
come from referingURL =
document.location.href; referingURL =
unescape
(referingURL.toLowerCase().
substr(referingURL.indexOf("?ref=") + 5));
// this is the url used to login from, needs
to be a database dbURL =
referingURL; dbURL = unescape
(dbURL.toLowerCase().substr(0, dbURL.indexOf
(".nsf") + 4));
// compose the list of Test IDs and
passwords // userID - password - login
type // userID: use empty entry to
get a blank cell // password: use
<secure> to get an input box
added to provide your password // login
type: user 0 for basic authentication,
1 for Single or Multi server
el[el.length] = new Array("joris bijnens",
"<secure>", 0); //
My ID, basic authentication, but you
can't have the password ;)
el[el.length] = new Array("Basic ID 1",
"password", 0); // A
login using basic authentication
el[el.length] = new Array("Test ID 2",
"lotusnotes", 1); // A
login using single server session
authentication
// this is the function used to log in:
the icon is requested from the server
function BasicLoginAs(id, pw)
{ req = new ActiveXObject
("Microsoft.XMLHTTP");
req.onreadystatechange =
BasicLoginReady; req.open("GET",
referingURL + "?
openicon", true, id, pw) req.send(); }
// event triggered when the icon is
loaded, so the login is ready function
BasicLoginReady()
{ if(req.readyState == 4)
{
// when the load is ready, open the
refering url again
top.document.location.href = referingURL;
}
}
// event when password is provided
function securekeyDown()
{
var subject = event.srcElement;
if(event.keyCode == 13)
{
if(subject.parentElement.logintype == 0)
{
// basic authentication
BasicLoginAs(subject.parentElement.
userID, subject.value);
}
else
{
// single or multi
document.location.href = dbURL +
"?Login&Username=" +
subject.parentElement.userID +
"&Password=" + subject.value + "&RedirectTo=" +
referingURL;
}
}
}
// special function to log out
function cellLogoutClicked()
{
var subject = event.srcElement;
document.location.href = dbURL +
"?Logout&RedirectTo=" + referingURL; }
// event triggered when a cell is
clicked function cellClicked() { var subject
= event.srcElement; if(subject.userPW
== "<secure>")
{
document.all[subject.secBox].focus();
}
else if(subject.tagName == "TD")
{
if(subject.logintype == 0)
{
// basic authentication
BasicLoginAs(subject.userID,
subject.userPW);
}
else
{
// single or multi
document.location.href = dbURL +
"?Login&Username=" +
subject.userID + "&Password=" +
subject.userPW + "&RedirectTo="
+ referingURL;
}
}
}
// mouseOver and mouseOut function
to change the cell color function mo() {
var
subject = event.srcElement;
if(subject.style.backgroundColor == "")
subject.style.backgroundColor =
"#ffaa55"; else subject.style.backgroundColor
= ""; }
// function to compose the table
function writeTable()
{
var elIndex = 0, elLength=el.length,
nbrOfColumns=3, rowIndex=-1, currCell;
while(elIndex < elLength)
{
if(elIndex%nbrOfColumns == 0)
{
// insert a new row
tabMain.insertRow();
rowIndex++;
}
currCell =
tabMain.rows[rowIndex].insertCell();
if(el[elIndex][0] == "")
{
// if no user name, add an
empty cell for grouping
currCell.innerHTML = " ";
}
else
{
if(el[elIndex][1] == "<secure>")
{
// add box to provide password
currCell.innerHTML = el[elIndex][0]
+ "<br><input type=
password id='pw" + elIndex + "'
onkeydown='securekeyDown()'>";
currCell.secBox = "pw" + elIndex;
}
else
{
currCell.innerHTML = el[elIndex][0];
}
currCell.userID = el[elIndex][0];
currCell.userPW = el[elIndex][1];
currCell.logintype = el[elIndex][2];
currCell.onmouseover = mo;
currCell.onmouseout = mo;
currCell.onclick = cellClicked;
}
elIndex++;
}
// add logout link
tabMain.insertRow();
rowIndex++;
currCell =
tabMain.rows[rowIndex].insertCell();
currCell.colSpan = nbrOfColumns;
currCell.onmouseover = mo;
currCell.onmouseout = mo;
currCell.onclick = cellLogoutClicked;
currCell.innerHTML = "Logout !";
}
// run the table function
writeTable();
</SCRIPT>
</body>
</HTML>
Do you have comments on this tip? Let us know.
This tip was submitted to the SearchDomino.com tip exchange by member Joris Bijnens. Please let others know how useful it is via the rating scale below. Do you have a useful Notes/Domino tip or code to share? Submit it to our monthly tip contest and you could win a prize and a spot in our Hall of Fame.
This was first published in February 2005