OpenID

Zarutian 29. desember 2006 (UTC): I am not quite understanding the spec http://openid.net/specs/openid-authentication-1_1.html

Zarutian 00:00 29. desember 2006 (UTC): Starting of an OpenID consumer package

Zarutian 22:49 30. desember 2006 (UTC): I cant figure out why the regexp's dont work propperly.

 package require http
 proc geturl {url} {
   if {![regexp {^http://.*} $url]} { set url "http://[set url]" }
   set token [http::geturl $url]
   set ncode [http::ncode $token]
   if {($ncode == "301") || ($ncode == "302")} {
     # an redirect
     upvar #0 $token state
     if {![info exists state(meta)]} { error }
     array set meta $state(meta)
     if {![info exists meta(Location)]} { error }
     return [geturl $meta(Location)]
   }

   set data [http::data $token]  
   http::cleanup $token
   return $data
 }

 proc find_openid.server {input} {
   # <link rel="openid.server" href="http://openid.example.com/">
   if {[regexp {<link rel="openid.server" href="([^"]*)"} $input dummy server]} {
     return $server
   } else {
     return {}
   }
 }
 proc find_openid.delegate {input} {
   # <link rel="openid.delegate" href="http://exampleuser.livejournal.com/">
   if {[regexp {<link rel="openid.delegate" href="([^"]*)"} $input dummy delegate]} {
     return $delegate
   } else {
     return {}
   }
 }
 
 proc get_openid_server_and_delegate {url} {
   set stuff [geturl $arg(url)]
   set r(openid.server)   [find_openid.server $stuff]
   set r(openid.delegate) [find_openid.delegater $stuff]
   return [array get r]
 }

 proc openid_assoc_request   {identity_provider} {}
 proc openid_check_immediate {identity_provider identity} {}
 proc openid_check_setup {} {}
 proc openid_check_authentication {} {}

CJL - Which regexps are causing problems? I'm going to assume the first one is OK, but the ones in find_openid.server and find_openid.delegate aren't. Firstly, there's no need to match the text before and after the section of interest. Secondly '.*' is greedy - it will match as much as possible (i.e. it will match up to the LAST '>' in the input string, rather than just as far as the next one). To ask for the minimal match, use '.*?' instead, although I have found that doesn't always behave as I'd expect it to. In general, I'd say try to avoid the combination of '.*' if you can, and in this case it works if you match 'any number of non-quote characters'. In summary, I think your patterns should be more like:

   regexp {<link rel="openid.server" href="([^"]*)"} $input dummy server

Zarutian 21:02 5. januar 2006: Thank you, I am never entirely at ease with regexpes.