mirror of
https://github.com/nikdoof/test-auth.git
synced 2025-12-13 22:32:15 +00:00
Basic django-piston based API package.
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -4,3 +4,6 @@
|
||||
django/
|
||||
registration/
|
||||
dbsettings.py
|
||||
django_cron
|
||||
django_evolution
|
||||
piston
|
||||
|
||||
0
api/__init__.py
Normal file
0
api/__init__.py
Normal file
96
api/handlers.py
Normal file
96
api/handlers.py
Normal file
@@ -0,0 +1,96 @@
|
||||
import re
|
||||
|
||||
from piston.handler import BaseHandler
|
||||
from piston.utils import rc, throttle
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from sso.models import ServiceAccount
|
||||
|
||||
class UserHandler(BaseHandler):
|
||||
allowed_methods = ('GET')
|
||||
fields = ('id', 'username', 'password' )
|
||||
model = User
|
||||
|
||||
def read(self, request, user=None, id=None, sid=None, suid=None):
|
||||
|
||||
if user:
|
||||
try:
|
||||
user = User.objects.get(username=user)
|
||||
except User.DoesNotExist:
|
||||
return rc.NOT_HERE
|
||||
if id:
|
||||
try:
|
||||
user = User.objects.get(id=id)
|
||||
except (User.DoesNotExist, ValueError):
|
||||
return rc.NOT_HERE
|
||||
if sid:
|
||||
try:
|
||||
sa = ServiceAccount.objects.get(service_id=sid, service_uid=suid)
|
||||
except ServiceAccount.DoesNotExist:
|
||||
return rc.NOT_HERE
|
||||
user = sa.user
|
||||
|
||||
enctype, salt, passwd = user.password.split("$")
|
||||
return { 'id': user.id, 'username': user.username, 'type': enctype, 'salt': salt }
|
||||
|
||||
|
||||
class LoginHandler(BaseHandler):
|
||||
allowed_methods = ('GET')
|
||||
fields = ('id', 'username', 'password' )
|
||||
model = User
|
||||
|
||||
def read(self, request):
|
||||
if 'hash' not in request.GET:
|
||||
return rc.BAD_REQUEST
|
||||
else:
|
||||
hash = request.GET['hash']
|
||||
|
||||
if 'username' in request.GET:
|
||||
try:
|
||||
user = User.objects.get(username=request.GET['username'])
|
||||
except (User.DoesNotExist, ValueError):
|
||||
return rc.NOT_HERE
|
||||
elif 'id' in request.GET:
|
||||
try:
|
||||
user = User.objects.get(id=request.GET['id'])
|
||||
except (User.DoesNotExist, ValueError):
|
||||
return rc.NOT_HERE
|
||||
elif 'suid' in request.GET:
|
||||
if 'sid' not in request.GET:
|
||||
return rc.BAD_REQUEST
|
||||
try:
|
||||
sa = ServiceAccount.objects.get(service_uid=request.GET['suid'], service=request.GET['sid'])
|
||||
user = sa.user
|
||||
except (ServiceAccount.DoesNotExist, ValueError):
|
||||
return rc.NOT_HERE
|
||||
else:
|
||||
return rc.BAD_REQUEST
|
||||
|
||||
enctype, salt, passwd = user.password.split("$")
|
||||
|
||||
if hash == passwd:
|
||||
return { 'auth': 'ok', 'id': user.id, 'username': user.username }
|
||||
else:
|
||||
return { 'auth': 'fail' }
|
||||
|
||||
|
||||
class ServiceAccountHandler(BaseHandler):
|
||||
allowed_methods = ('GET')
|
||||
fields = ('id', 'user_id', 'service_uid' )
|
||||
model = ServiceAccount
|
||||
|
||||
def read(self, request, id=None):
|
||||
if id:
|
||||
try:
|
||||
account = ServiceAccount.objects.get(id=id)
|
||||
except (ServiceAccount.DoesNotExist, ValueError):
|
||||
return rc.NOT_HERE
|
||||
else:
|
||||
if request.GET['serviceuid']:
|
||||
try:
|
||||
account = ServiceAccount.objects.get(service_uid=request.GET['serviceuid'])
|
||||
except (ServiceAccount.DoesNotExist, ValueError):
|
||||
return rc.NOT_HERE
|
||||
|
||||
return account
|
||||
|
||||
21
api/urls.py
Normal file
21
api/urls.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from django.conf.urls.defaults import *
|
||||
from piston.resource import Resource
|
||||
from piston.authentication import HttpBasicAuthentication
|
||||
|
||||
from api.handlers import *
|
||||
|
||||
auth = HttpBasicAuthentication(realm="My Realm")
|
||||
#ad = { 'authentication': auth }
|
||||
ad = {}
|
||||
|
||||
user_resource = Resource(handler=UserHandler, **ad)
|
||||
serviceaccount_resource = Resource(handler=ServiceAccountHandler, **ad)
|
||||
login_resource = Resource(handler=LoginHandler, **ad)
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^login/$', login_resource),
|
||||
url(r'^user/$', user_resource),
|
||||
url(r'^user/(?P<id>\d+)/$', user_resource),
|
||||
url(r'^serviceaccount/$', serviceaccount_resource),
|
||||
url(r'^serviceaccount/(?P<id>\d+)/$', serviceaccount_resource),
|
||||
)
|
||||
3
init.sh
3
init.sh
@@ -3,3 +3,6 @@
|
||||
svn checkout http://django-registration.googlecode.com/svn/trunk/registration
|
||||
svn co http://code.djangoproject.com/svn/django/branches/releases/1.1.X/django django
|
||||
svn co http://django-evolution.googlecode.com/svn/trunk/django_evolution
|
||||
hg clone http://bitbucket.org/jespern/django-piston/
|
||||
mv django-piston/piston ./
|
||||
rm -rf django-piston
|
||||
|
||||
@@ -81,6 +81,7 @@ INSTALLED_APPS = (
|
||||
'mumble',
|
||||
'reddit',
|
||||
'sso',
|
||||
'api',
|
||||
)
|
||||
|
||||
# Disable the service API, used for data imports
|
||||
|
||||
Reference in New Issue
Block a user