changeset 2122:69412724ce93

Make it possible to get an auth token using jid and shared secret.
author Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
date Thu, 14 Jun 2007 03:44:13 +0200
parents ae2134211ac1
children 184975c4b86f
files MoinMoin/request/__init__.py MoinMoin/xmlrpc/__init__.py
diffstat 2 files changed, 49 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/request/__init__.py	Thu Jun 14 01:19:20 2007 +0200
+++ b/MoinMoin/request/__init__.py	Thu Jun 14 03:44:13 2007 +0200
@@ -661,6 +661,9 @@
 
         self._login_messages = login_msgs
         return user_obj
+    
+    def handle_jid_auth(self, jid):
+        return user.get_by_jabber_id(self, jid)
 
     def parse_cookie(self):
         try:
--- a/MoinMoin/xmlrpc/__init__.py	Thu Jun 14 01:19:20 2007 +0200
+++ b/MoinMoin/xmlrpc/__init__.py	Thu Jun 14 03:44:13 2007 +0200
@@ -587,38 +587,66 @@
             return userdata
 
     # authorization methods
+    
+    def _cleanup_stale_tokens(request):
+        items = caching.get_cache_list(request, 'xmlrpc-session', 'farm')
+        tnow = time.time()
+        for item in items:
+            centry = caching.CacheEntry(self.request, 'xmlrpc-session', item,
+                                        scope='farm', use_pickle=True)
+            try:
+                expiry, uid = centry.content()
+                if expiry < tnow:
+                    centry.remove()
+            except caching.CacheError:
+                pass
+            
+    def _generate_auth_token(self):
+        token = random_string(32, 'abcdefghijklmnopqrstuvwxyz0123456789')
+        centry = caching.CacheEntry(self.request, 'xmlrpc-session', token,
+                                    scope='farm', use_pickle=True)
+        centry.update((time.time() + 15*3600, u.id))
+        return token
 
     def xmlrpc_getAuthToken(self, username, password, *args):
         """ Returns a token which can be used for authentication
             in other XMLRPC calls. If the token is empty, the username
             or the password were wrong. """
 
-        def _cleanup_stale_tokens(request):
-            items = caching.get_cache_list(request, 'xmlrpc-session', 'farm')
-            tnow = time.time()
-            for item in items:
-                centry = caching.CacheEntry(self.request, 'xmlrpc-session', item,
-                                            scope='farm', use_pickle=True)
-                try:
-                    expiry, uid = centry.content()
-                    if expiry < tnow:
-                        centry.remove()
-                except caching.CacheError:
-                    pass
-
         if randint(0, 99) == 0:
             _cleanup_stale_tokens(self.request)
 
         u = self.request.handle_auth(None, username=username,
                                      password=password, login=True)
+        
         if u and u.valid:
-            token = random_string(32, 'abcdefghijklmnopqrstuvwxyz0123456789')
-            centry = caching.CacheEntry(self.request, 'xmlrpc-session', token,
-                                        scope='farm', use_pickle=True)
-            centry.update((time.time() + 15*3600, u.id))
-            return token
+            return _generate_auth_token()
         else:
             return ""
+        
+    def xmlrpc_getJabberAuthToken(self, jid, secret):
+        """Returns a token which can be used for authentication.
+        
+        This token can be used in other XMLRPC calls. Generation of
+        token depends on user's JID and a secret shared between wiki
+        and Jabber bot.
+        
+        @param jid: a bare Jabber ID
+        
+        """
+        
+        if self.cfg.secret != secret:
+            return ""
+        
+        if randint(0, 99) == 0:
+            _cleanup_stale_tokens(self.request)
+            
+        u = self.request.handle_jid_auth(jid)
+        
+        if u and u.valid:
+            return _generate_auth_token()
+        else:
+            return ""            
 
     def xmlrpc_applyAuthToken(self, auth_token):
         """ Applies the auth token and thereby authenticates the user. """