Commits

Alexis Metaireau committed b1df5b4

Rename Token to ConsumerToken, minor changes to the API. W.I.P.

Comments (0)

Files changed (5)

oauthclient/management/__init__.py

 from django.core.management import call_command
 
 def create_oauth_token(app, created_models, verbosity, **kwargs):
-    from oauthclient.models import Token
-    if Token in created_models and kwargs.get('interactive', True):
+    from oauthclient.models import ConsumerToken
+    if ConsumerToken in created_models and kwargs.get('interactive', True):
         msg = "\nYou just installed oauthclient app, wich means you" \
-                "don't have any token defined yet.\n Would you like to define " \
+                "don't have any consumer token defined yet.\n Would you like to define " \
                 "them now ? (yes/no): "
         confirm = raw_input(msg)
         while 1:

oauthclient/management/commands/oauthcreatetoken.py

 from django.core.management.base import BaseCommand, CommandError
 from django.utils.translation import ugettext as _
 
-from oauthclient.models import OAuthServer, Token
+from oauthclient.models import OAuthServer, ConsumerToken
+
+DEFAULT_REQUEST_TOKEN_URL = '/oauth/request_token/'
+DEFAULT_ACCESS_TOKEN_URL = '/oauth/access_token/'
+DEFAULT_AUTHORIZE_URL = '/oauth/authorize/'
 
 class Command(BaseCommand):
     option_list = BaseCommand.option_list + (
         make_option('--serverurl', dest='serverurl', default=None, 
             help='Specify the OAuth server root url.'),
         make_option('--request-token-url', dest='request_token_url',
-            default='/oauth/request_token/', 
-            help='Specify the request token url'),
+            default=DEFAULT_REQUEST_TOKEN_URL, 
+            help='Specify the request token url. Default is %s' % DEFAULT_REQUEST_TOKEN_URL),
         make_option('--access_token_url', dest = 'access_token_url',
-            default='/oauth/access_token/',
-            help='Specify the access token url'),
+            default=DEFAULT_ACCESS_TOKEN_URL,
+            help='Specify the access token url. Default is %s' % DEFAULT_ACCESS_TOKEN_URL),
         make_option('--authorize-url', dest = 'authorize_url', 
-            default='/oauth/authorize/', 
-            help='Specify the authorize url'),
+            default=DEFAULT_AUTHORIZE_URL, 
+            help='Specify the authorize url. Default is %s' % DEFAULT_AUTHORIZE_URL),
         make_option('--noinput', action='store_false', dest='interactive', default=True,
             help='Tells Django to NOT prompt the user for input of any kind. '    \
                  'You must use --server-url, --key and --secret with --noinput.')
             authorize_url=authorize_url)
         server.save()
 
-        token = Token(identifier=identifier, key=key, secret=secret, server=server)
+        token = ConsumerToken(identifier=identifier, key=key, secret=secret, server=server)
         token.save()
-        print "Token and Server successfully configured"
+        print "Consumer Token and Server successfully configured"

oauthclient/models.py

         return self.server_url + self.authorize_url
 
 
-class Token(models.Model):
+class ConsumerToken(models.Model):
     """Define a couple key-secret provided by the oauth server for this
     application.
 
     server = models.ForeignKey(OAuthServer)
     last_modification = models.DateField(auto_now=True)
 
-    def _get_consumer(self):
+    def get_consumer(self):
         """Return a consumer object, configured with settings values.
         
         """

oauthclient/utils.py

 from django.shortcuts import redirect
 from django.core.urlresolvers import reverse
 
-from models import Token, OAuthServer
+from models import ConsumerToken, OAuthServer
 
-def get_token_field(identifier, field):
-    return getattr(Token.objects.get(identifier=identifier), field)
+def get_consumer_token(identifier):
+    return ConsumerToken.objects.get(identifier=identifier)
 
-def get_server_field(identifier, field):
-    return getattr(Token.objects.get(identifier=identifier).server, field) 
-
-def oauth_need_authentication(request, force=False):
+def oauth_need_authentication(request, identifier, force=False):
     """Authenticate user using oauth flow, if the an authentication does not
     already exists.
     
     """
-    return not ('oauth_token' and 'oauth_token_secret' in request.session)
+    return not (identifier + 'oauth_token' and identifier + 'oauth_token_secret' in request.session)
     
-def is_oauthenticated(force=False):
+def is_oauthenticated(identifier, force=False):
     """Decorator when oauth authentication is needed.
     
     If the user is not authenticated, redirect the user to the oauth 
     def wrapper(func):
         def wrapped(*args, **kwargs):
             request = args[0]
-            if force or oauth_need_authentication(request=request, force=force):
-                return redirect('%s?next=%s' % (reverse('oauth:request_token'), request.path))
+            if force or oauth_need_authentication(request=request,
+                    identifier=identifier, force=force):
+                return redirect('%s?next=%s' % (
+                    reverse('oauth:request_token', kwargs={'identifier':identifier}), 
+                    request.path))
             else:
                 return func(*args, **kwargs)
         return wrapped

oauthclient/views.py

 
 #oauthclient import
 from utils import is_oauthenticated
-from models import Token, OAuthServer
+from models import ConsumerToken, OAuthServer
 
 def get_request_token(request, identifier):
     """First and second step of the three-legged OAuth flow:
     view.
     
     """
-    token = Token.objects.get(identifier=identifier)
+    token = ConsumerToken.objects.get(identifier=identifier)
     client = oauth2.Client(token.get_consumer())
     resp, content = client.request(token.server.get_request_token_url(), "GET")
 
     callback_url = 'http://%s%s' % (Site.objects.get_current().domain,
         reverse('oauth:access_token_ready', args=[identifier]))
     
-    redirect_url = "%s?oauth_token=%s&oauth_callback=%s" %
-        (token.server.get_authorize_url(), request_token['oauth_token'], callback_url)
+    redirect_url = "%s?oauth_token=%s&oauth_callback=%s" % (
+        token.server.get_authorize_url(), 
+        request_token['oauth_token'], 
+        callback_url)
+
     if 'next' in request.GET:
         request.session['next'] = request.GET['next']
     request.session.save()
     if not 'oauth_verifier' in request.GET:
         raise Exception('oauth_verifier must be present in request.GET')
     
-    token = Token.objects.get(identifier=identifier)
+    token = ConsumerToken.objects.get(identifier=identifier)
 
     # Echange the request token against a access token.
     request_token = oauth2.Token(request.session[identifier + '_request_token'],