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