Martin von Löwis avatar Martin von Löwis committed 8adb13c

Issue #4: Deprecate get_username; add get_sreg.

Comments (0)

Files changed (3)

 Changes
 -------
 
+1.3:
+
+* Add get_sreg. Deprecate get_username.
+
 1.2 (2010-07-02):
 
 * port to Python 3
    Return a dictionary of attribute exchange (AX) properties,
    in the type-url : value format.
 
+.. function:: get_sreg(response, validated) -> dict
+
+   Return the dictionary of simple registration parameters in resp,
+   with the openid.sreg. prefix stripped.
+
 .. function:: get_email(response) -> str
 
    Return the user's proposed email address. It first tries the simple
    registration attribute (SREG), and then the axschema AX attribute.
    Return None if neither is available.
 
-.. function:: get_username(response) -> str
-
-   Propose a username for the user. It may return the SREG nickname as
-   a string, the AX first/last names as a tuple, or None.
-
 Response Fields
 ---------------
 
     if  'openid.identity' not in response:
         raise ValueError('missing openid.identity')
 
-    # Won't check nonce value - caller must verify this is not a replay
+    # Will not check nonce value - caller must verify this is not a replay
 
     signed = response['openid.signed'][0].split(',')
     query = []
                 continue
             res[v[0]] = resp[value_name][0]
     return res
-    
+
+def get_sreg(resp, validated):
+    """Return the dictionary of simple registration parameters in resp,
+    with the openid.sreg. prefix stripped."""
+    res = {}
+    for k, v in resp.items():
+        if k.startswith('openid.sreg.'):
+            k = k[len('openid.sreg.'):]
+            if 'sreg.'+k in validated:
+                res[k] = v[0]
+    return res
 
 def get_email(resp):
     "Return the email address embedded response, or None."
     return None
 
 def get_username(resp):
-    "Return either nickname or (first, last) or None."
+    """Return either nickname or (first, last) or None.
+    This function is deprecated; use get_ax and get_sreg instead.
+    """
 
     validated = resp['openid.signed'][0]
     if 'openid.sreg.nickname' in resp and \
                         return self.error('Incomplete signature')
                     claimed = query['openid.identity'][0]
                 payload = "Hello "+claimed+"\n"
+                ax = get_ax(query, get_namespaces(query), signed)
+                sreg = get_sreg(query, signed)
                 email = get_email(query)
                 if email:
                     payload += 'Your email is '+email+"\n"
                 else:
                     payload += 'No email address is known\n'
-                username = get_username(query)
-                if isinstance(username, tuple):
-                    username = " ".join(username)
+                if 'nickname' in sreg:
+                    username = sreg['nickname']
+                elif "http://axschema.org/namePerson/first" in ax:
+                    username = ax["http://axschema.org/namePerson/first"]
+                    if "http://axschema.org/namePerson/last" in ax:
+                        username += "." + ax["http://axschema.org/namePerson/last"]
+                else:
+                    username = None
                 if username:
                     payload += 'Your nickname is '+username+'\n'
                 else:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.