Shop OBEX P1 Docs P2 Docs Learn Events
Roll Your Own Basic Authentication — Parallax Forums

Roll Your Own Basic Authentication

Mike GMike G Posts: 2,702
edited 2012-10-07 18:31 in Accessories
Once you get your Spinneret or embedded app up and running the next logic step is to password protect your stuff. I know this because I have helped a few folks with their Spinneret projects and inevitably I’m asked, “How do I password protect my web site?”.

This is about basic authentication. Basic authentication not the best solution but it’s better than nothin’.

More information from wikipedia.org
http://en.wikipedia.org/wiki/Basic_access_authentication

Basic authentication uses the browser and HTTP headers to persist base-64 encoded user credentials. However, you know this because you just read the Wikipedia wiki above - hint hint.

First, the logic flow.
Does the current request contain an Authorization header?
NO: Respond with the WWW-Authenticate: Basic realm="insert realm" header
YES: Are the username and password correct?
	NO:  Respond with the WWW-Authenticate: Basic realm="insert realm" header
	YES: Send the requested page


Authorization header
The authorization header line contains the base-64 encode username and password; web:web
Authorization: Basic d2ViOndlYg==

If the Authorization header is not part of the request, then send the WWW-Authenticate header.
WWW-Authenticate: Basic realm="localhost"

The WWW-Authenticate: header field cause the browser to popup an Authentication Required alert. Enter a username and password and click Ok. The browser will base-64 encode the credentials and send credential to the server using the Authorization header.

The base-64 credentials are not decoded on the server. When the server starts, the expected username and password are base-64 encoded and placed in a buffer. When the request comes in from the client only the base-64 strings are compared. This is not a standard, it’s simply the way I wrote the code.

In the attached demo, you will find a Header.Spin object. This object is used to tokenize the requested header and easily extract URL, header, GET, and POST collections. The demo is using a W5200. Sorry Spinneret users :( As soon as I get a chance, I plan to update the Spinneret driver so It can use the same socket interface found in the W5200 driver.

Comments

  • Igor_RastIgor_Rast Posts: 357
    edited 2012-10-07 14:29
    Nice Work Mike ,

    Question, Logged in fine, when I close the brouwser window , and open a new one up , its already authenticated . Anyway to kick you out each time you close the brouwser ?

    Verry nice pice , Any Multsocket demos ?:p. the parseresourse is kinda trikky, with the ongoing xml replys
  • Mike GMike G Posts: 2,702
    edited 2012-10-07 18:19
    I'm not sure. I tested the Basic Authentication on Win XP and Win 7 in IE, Firefox, Safari, Opera, and Chrome. All functioned as expected. User credentials persist until the browser is closed. You must close the browser. Closing a tab will not clear the persistence.
    the parseresourse is kinda trikky, with the ongoing xml replys
    Do you mean the TokenizeHeader method in HttpHeader?

    If you are sending XML on a POST to the server then update the TokenizeHeader method to look for the Content-Type: text/xml. If "text/xml" is found then do not decode the body. Or use GET over POST.
  • Mike GMike G Posts: 2,702
    edited 2012-10-07 18:31
    One more thing... I forgot to mention that the base-64 encoding object was created by Phil Pilgrim (PhiPi). I removed methods related to images but the actual encoding is all Phil's. Thanks PhiPi.
Sign In or Register to comment.