mirror of
https://github.com/ZwareBear/awx.git
synced 2026-03-20 07:43:35 -05:00
Operational authorization via tastypie.
This commit is contained in:
@@ -2,26 +2,15 @@ from tastypie.authentication import Authentication
|
|||||||
from tastypie.authorization import Authorization
|
from tastypie.authorization import Authorization
|
||||||
|
|
||||||
# FIXME: this is completely stubbed out at this point!
|
# FIXME: this is completely stubbed out at this point!
|
||||||
|
# INTENTIONALLY NOT IMPLEMENTED CORRECTLY :)
|
||||||
class AcomAuthentication(Authentication):
|
|
||||||
def is_authenticated(self, request, **kwargs):
|
|
||||||
return True
|
|
||||||
#if 'admin' in request.user.username:
|
|
||||||
# return True
|
|
||||||
|
|
||||||
#return False
|
|
||||||
|
|
||||||
# Optional but recommended
|
|
||||||
def get_identifier(self, request):
|
|
||||||
return request.user.username
|
|
||||||
|
|
||||||
class AcomAuthorization(Authorization):
|
class AcomAuthorization(Authorization):
|
||||||
|
|
||||||
def is_authorized(self, request, object=None):
|
def is_authorized(self, request, object=None):
|
||||||
return True
|
if request.user.username == 'admin':
|
||||||
#if request.user.username == 'admin':
|
return True
|
||||||
# return True
|
else:
|
||||||
#else:
|
return False
|
||||||
# return False
|
|
||||||
|
|
||||||
# Optional but useful for advanced limiting, such as per user.
|
# Optional but useful for advanced limiting, such as per user.
|
||||||
def apply_limits(self, request, object_list):
|
def apply_limits(self, request, object_list):
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
# myapp/api.py
|
# myapp/api.py
|
||||||
|
|
||||||
from tastypie.resources import ModelResource
|
from tastypie.resources import ModelResource
|
||||||
from lib.api.auth import AcomAuthentication, AcomAuthorization
|
from tastypie.authentication import BasicAuthentication
|
||||||
|
from lib.api.auth import AcomAuthorization
|
||||||
|
|
||||||
import lib.main.models as models
|
import lib.main.models as models
|
||||||
|
|
||||||
@@ -10,7 +11,7 @@ class Organizations(ModelResource):
|
|||||||
class Meta:
|
class Meta:
|
||||||
queryset = models.Organization.objects.all()
|
queryset = models.Organization.objects.all()
|
||||||
resource_name = 'organizations'
|
resource_name = 'organizations'
|
||||||
authentication = AcomAuthentication()
|
authentication = BasicAuthentication()
|
||||||
authorization = AcomAuthorization()
|
authorization = AcomAuthorization()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
import hammock
|
|
||||||
import os
|
import os
|
||||||
import requests
|
import requests
|
||||||
|
from requests.auth import HTTPBasicAuth
|
||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
|
|
||||||
# this is temporary
|
# this is temporary
|
||||||
username = os.getenv("ACOM_USER","admin")
|
username = os.getenv("ACOM_USER","admin")
|
||||||
password = os.getenv("ACOM_PASS","admin")
|
password = os.getenv("ACOM_PASS","admin")
|
||||||
server = os.getenv("ACOM_SERVER","127.0.0.1:8000")
|
print "USER=%s" % username
|
||||||
|
server = os.getenv("ACOM_SERVER","http://127.0.0.1:8000")
|
||||||
|
|
||||||
# TODO: error handling/output/etc
|
# TODO: error handling/output/etc
|
||||||
# TODO: format into actual command line
|
# TODO: format into actual command line
|
||||||
@@ -18,15 +19,24 @@ PARAMS = {
|
|||||||
HEADERS = {
|
HEADERS = {
|
||||||
'Content-Type' : 'application/json'
|
'Content-Type' : 'application/json'
|
||||||
}
|
}
|
||||||
AUTH = (username, password)
|
AUTH = HTTPBasicAuth(username, password)
|
||||||
|
|
||||||
handle = hammock.Hammock("http://%s/api/v1" % server, auth=AUTH, append_slash=True, params=PARAMS, headers=HEADERS)
|
def get(url_seg):
|
||||||
|
resp = requests.get("%s/api/v1/%s" % (server, url_seg), auth=AUTH)
|
||||||
|
return resp
|
||||||
|
|
||||||
|
def post(url_seg, data):
|
||||||
|
resp = requests.post("%s/api/v1/%s" % (server, url_seg), auth=AUTH, data=data, headers=HEADERS)
|
||||||
|
return resp
|
||||||
|
|
||||||
class Collection(object):
|
class Collection(object):
|
||||||
|
|
||||||
def __init__(self, handle):
|
def __init__(self):
|
||||||
self.handle = handle
|
|
||||||
self.response = self.accessor().GET(auth=AUTH, headers=HEADERS)
|
self.response = get(self.base_url())
|
||||||
|
|
||||||
|
print self.response.text
|
||||||
|
print self.response.status_code
|
||||||
assert self.response.status_code == 200
|
assert self.response.status_code == 200
|
||||||
# TODO: error handling on non-200
|
# TODO: error handling on non-200
|
||||||
print "RESPONSE=%s" % self.response.text
|
print "RESPONSE=%s" % self.response.text
|
||||||
@@ -36,13 +46,13 @@ class Collection(object):
|
|||||||
self.meta = self.data['meta']
|
self.meta = self.data['meta']
|
||||||
self.objects = self.data['objects']
|
self.objects = self.data['objects']
|
||||||
|
|
||||||
def accessor(self):
|
def base_url(self):
|
||||||
return exceptions.NotImplementedError()
|
return exceptions.NotImplementedError()
|
||||||
|
|
||||||
def add(self, data):
|
def add(self, data):
|
||||||
# TODO: error handling
|
# TODO: error handling
|
||||||
json_data = json.dumps(data)
|
json_data = json.dumps(data)
|
||||||
response = self.accessor().POST(data=json_data)
|
response = post(self.base_url(), data=json_data)
|
||||||
print response.status_code
|
print response.status_code
|
||||||
print response.text
|
print response.text
|
||||||
assert response.status_code == 201
|
assert response.status_code == 201
|
||||||
@@ -60,16 +70,14 @@ class Entry(object):
|
|||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
self.data = data
|
self.data = data
|
||||||
self.resource_uri = data.get('resource_uri', None)
|
self.resource_uri = data.get('resource_uri', None)
|
||||||
print "LOADING"
|
|
||||||
self.accessor = hammock.Hammock(self.resource_uri, auth=AUTH, append_slash=True, params=PARAMS, headers=HEADERS)
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return repr(self.data)
|
return repr(self.data)
|
||||||
|
|
||||||
class Organizations(Collection):
|
class Organizations(Collection):
|
||||||
|
|
||||||
def accessor(self):
|
def base_url(self):
|
||||||
return self.handle.organizations
|
return "organizations/"
|
||||||
|
|
||||||
#(Epdb) got.text
|
#(Epdb) got.text
|
||||||
#u'{"meta": {"limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 1}, "objects": [{"active": true, "creation_date": "2013-03-15", "description": "testorg!", "id": 1, "name": "testorg", "resource_uri": "/api/v1/organizations/1/"}]}'
|
#u'{"meta": {"limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 1}, "objects": [{"active": true, "creation_date": "2013-03-15", "description": "testorg!", "id": 1, "name": "testorg", "resource_uri": "/api/v1/organizations/1/"}]}'
|
||||||
@@ -77,7 +85,7 @@ class Organizations(Collection):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
print "---"
|
print "---"
|
||||||
orgs = Organizations(handle)
|
orgs = Organizations()
|
||||||
for x in orgs:
|
for x in orgs:
|
||||||
print x
|
print x
|
||||||
print "---"
|
print "---"
|
||||||
@@ -85,7 +93,7 @@ try:
|
|||||||
print "---"
|
print "---"
|
||||||
|
|
||||||
print "---"
|
print "---"
|
||||||
orgs = Organizations(handle)
|
orgs = Organizations()
|
||||||
for x in orgs:
|
for x in orgs:
|
||||||
print x
|
print x
|
||||||
|
|
||||||
|
|||||||
@@ -93,6 +93,7 @@ TEMPLATE_CONTEXT_PROCESSORS += (
|
|||||||
)
|
)
|
||||||
|
|
||||||
MIDDLEWARE_CLASSES += (
|
MIDDLEWARE_CLASSES += (
|
||||||
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||||
#'django.middleware.transaction.TransactionMiddleware',
|
#'django.middleware.transaction.TransactionMiddleware',
|
||||||
#'devserver.middleware.DevServerMiddleware',
|
#'devserver.middleware.DevServerMiddleware',
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user