1 #!/usr/bin/env python 2 # -*- coding: iso-8859-1 -*- 3 4 "An OpenID login provider." 5 6 # Site map imports. 7 8 from WebStack.Generic import ContentType 9 from WebStack.Resources.OpenIDLogin import Authenticator 10 from WebStack.Resources.ResourceMap import MapResource 11 from WebStack.Resources.Selectors import EncodingSelector, PathSelector 12 from WebStack.Resources.Static import StringResource 13 import XSLForms.Resources.OpenIDLogin 14 from XSLForms.Resources.WebResources import output, prepare_resources as pr, resources 15 import urllib 16 17 # Configuration settings. 18 19 encoding = "utf-8" 20 21 # Resource classes. 22 23 class LoginResource(XSLForms.Resources.OpenIDLogin.OpenIDLoginResource): 24 25 "Special login screen for this application." 26 27 resource_dir = resources(__file__) 28 template_resources = { 29 "login" : output("login_template.xhtml"), 30 "success" : output("success_template.xhtml") 31 } 32 33 # Site map initialisation. 34 35 def get_site(host, use_redirect=1): 36 37 "Return a simple Web site resource." 38 39 # NOTE: Support "special" username due to stupid HTML 4 restrictions. 40 41 special_username = urllib.quote(u"???".encode("utf-8")) 42 43 # Authentication objects. 44 45 credentials = [ 46 # Local identifier, username, password 47 ((host + "/badger", "badger"), "abc"), 48 ((host + "/vole", "vole"), "xyz"), 49 ((host + u"/???", u"???"), u"???"), 50 ((host + "/badger2", "badger"), "abc"), 51 ((host + "/vole2", "vole"), "xyz"), 52 ((host + u"/???2", u"???"), u"???"), 53 ((host + "/" + special_username, u"???"), u"???"), 54 ] 55 login_authenticator = Authenticator(credentials) 56 57 # Get a login resource. 58 59 login_resource = LoginResource(host, login_authenticator, use_redirect=use_redirect) 60 61 # A container for the login resource and user pages. 62 63 resource = MapResource({ 64 "login" : login_resource, 65 66 # Some local OpenID pages. 67 68 "badger" : 69 StringResource(""" 70 <html> 71 <head> 72 <meta http-equiv="Content-Type" content="text/html; charset="iso-8859-1" /> 73 <link rel="openid2.provider openid.server" href="%s/login" /> 74 <link rel="openid2.local_id openid.delegate" href="%s/badger" /> 75 <title>Badger's Home Page</title> 76 </head> 77 <body> 78 <p>Home page for the OpenID authenticated user, <code>badger</code>.</p> 79 </body> 80 </html> 81 """ % (host, host), ContentType("text/html", "iso-8859-1")), 82 u"???" : 83 StringResource(u""" 84 <html> 85 <head> 86 <meta http-equiv="Content-Type" content="text/html; charset="iso-8859-1" /> 87 <link rel="openid2.provider openid.server" href="%s/login" /> 88 <link rel="openid2.local_id openid.delegate" href="%s/%s" /> 89 <title>Home Page for ???</title> 90 </head> 91 <body> 92 <p>Home page for the OpenID authenticated user, <code>???</code>.</p> 93 </body> 94 </html> 95 """ % (host, host, special_username), ContentType("text/html", "iso-8859-1")), 96 "vole" : 97 StringResource(""" 98 <html> 99 <head> 100 <meta http-equiv="Content-Type" content="text/html; charset="iso-8859-1" /> 101 <link rel="openid2.provider openid.server" href="%s/login" /> 102 <link rel="openid2.local_id openid.delegate" href="%s/vole" /> 103 <title>Vole's Home Page</title> 104 </head> 105 <body> 106 <p>Home page for the OpenID authenticated user, <code>vole</code>.</p> 107 </body> 108 </html> 109 """ % (host, host), ContentType("text/html", "iso-8859-1")), 110 111 # OpenID 2.0 resources. 112 # See: http://docs.oasis-open.org/xri/2.0/specs/cd02/xri-resolution-V2.0-cd-02.html#_Ref129424065 113 # ("3.3 Media Types for XRI Resolution") 114 # See: http://openid.net/specs/openid-authentication-2_0.html#discovery 115 # ("7.3. Discovery") 116 117 "badger2" : 118 StringResource(""" 119 <?xml version="1.0"?> 120 <Service xmlns="xri://$xrd*($v*2.0)"> 121 <Type>http://specs.openid.net/auth/2.0/signon</Type> 122 <URI>%s/login</URI> 123 <LocalID>%s/badger2</LocalID> 124 </Service> 125 """ % (host, host), ContentType("application/xrd+xml", "iso-8859-1")), 126 u"???2" : 127 StringResource(u""" 128 <?xml version="1.0" encoding="iso-8859-1"?> 129 <Service xmlns="xri://$xrd*($v*2.0)"> 130 <Type>http://specs.openid.net/auth/2.0/signon</Type> 131 <URI>%s/login</URI> 132 <LocalID>%s/???2</LocalID> 133 </Service> 134 """ % (host, host), ContentType("application/xrd+xml", "iso-8859-1")), 135 "vole2" : 136 StringResource(""" 137 <?xml version="1.0"?> 138 <Service xmlns="xri://$xrd*($v*2.0)"> 139 <Type>http://specs.openid.net/auth/2.0/signon</Type> 140 <URI>%s/login</URI> 141 <LocalID>%s/vole2</LocalID> 142 </Service> 143 """ % (host, host), ContentType("application/xrd+xml", "iso-8859-1")), 144 145 }) 146 147 # Wrap the resource up with information about the application root. 148 149 return EncodingSelector(PathSelector(resource), encoding) 150 151 # Resource preparation ahead of time - useful for making installations. 152 153 def prepare_resources(): 154 for cls in [LoginResource]: 155 pr(cls) 156 157 # vim: tabstop=4 expandtab shiftwidth=4