From 49dee35b10643d89e30cd20b9a5243c2fed112c5 Mon Sep 17 00:00:00 2001 From: Ziad Sawalha Date: Fri, 15 Apr 2011 02:35:16 -0500 Subject: Updates to identity.py and README --- README | 32 ++++++++++++++++++++++ db/keystone.db | Bin 2048 -> 3072 bytes keystone/identity.py | 76 +++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 105 insertions(+), 3 deletions(-) diff --git a/README b/README index 30a219e6..536ad2df 100644 --- a/README +++ b/README @@ -3,3 +3,35 @@ Description Dependencies: * Install SQLite3 + +Setup: +Install http://pypi.python.org/pypi/setuptools +sudo easy_install PasteDeploy +sudo easy_install simplejson +sudo easy_install -U bottle + +Tables: +CREATE TABLE tenants(tenant_id INTEGER, tenant_desc varchar(255), tenant_enabled INTEGER, PRIMARY KEY(tenant_id ASC)); + +Issues: +bottle not in path on Mac OS X (added exception handling to default path) + +Demo: +Start server: +python identity.py + +Add Tenant: +curl -i -X POST -H "Content-Type: application/json" -d '{"tenant": { "id": "123456", "description": "A description ...", "enabled": true } }' http://localhost:8080/tenants + +Get token: +curl -i -X POST -H "Content-Type: application/json" -d '{"username": "john", "password": "secret" }' http://localhost:8080/tokens + +Validate good token: +curl -i -X GET -H "Content-Type: application/json" http://localhost:8080/token/abcdefghijklmnopqrstuvwxy + +Validate bad token: +curl -i -X GET -H "Content-Type: application/json" http://localhost:8080/token/abcdefghijklmnopqrstuvbad + + + + diff --git a/db/keystone.db b/db/keystone.db index f8af172a..dc4ebd40 100644 Binary files a/db/keystone.db and b/db/keystone.db differ diff --git a/keystone/identity.py b/keystone/identity.py index db0281d8..cb244eed 100644 --- a/keystone/identity.py +++ b/keystone/identity.py @@ -14,10 +14,20 @@ # limitations under the License. import os -import simplejson +try: + import simplejson as json +except ImportError: + import json import sqlite3 -from bottle import route, run, request, debug +try: + from bottle import route, run, request, debug, abort +except ImportError: + import imp + imp.load_source("bottle", "/Library/Python/2.6/site-packages/bottle-0.8.5-py2.6.egg/bottle.py") + from bottle import route, run, request, debug, abort + +import sqlite3 class Tenants: @route ('/tenants', method='POST') @@ -39,7 +49,7 @@ class Tenants: content = request.environ['CONTENT_TYPE']; if content in content_types: if content == 'application/json': - body = simplejson.loads(request.body.readline()) + body = json.loads(request.body.readline()) tenant_id = body['tenant']['id'] tenant_desc = body['tenant']['description'] tenant_enabled = body['tenant']['enabled'] @@ -73,6 +83,66 @@ class Tenants: return 'it did NOT work\n' + @route ('/tokens', method='POST') + def create_token(): + ''' + Creating token by doing a POST on /tokens + ''' + if 'CONTENT_TYPE' in request.environ: + content_types = ['text/plain', 'application/json', + 'application/xml', 'text/xml'] + content = request.environ['CONTENT_TYPE']; + if content in content_types: + if content == 'application/json': + body = json.loads(request.body.readline()) + username = body['username'] + password = body['password'] + + dbpath = os.path.abspath( + os.path.join(os.path.dirname(__file__), + '../db/keystone.db')) + con = sqlite3.connect(dbpath) + cur = con.cursor() + cur.execute( + "SELECT COUNT(*) FROM users WHERE username='%s' AND password='%s'" % + (username, password)) + con.commit() + con.close() + + elif content == 'application/xml': + #TODO: Implement XML support + return "whatever, we don't have XML yet" + + accept_header = request.header.get('Accept') + if accept_header in content_types: + if accept_header == 'application/json': + return '{"token": "abcdefghijklmnopqrstuvwxyz"}' + elif accept_header == 'application/xml': + #TODO: Implement XML support + return "whatever, we don't have XML yet" + else: + # If there is no Accept header, the default is JSON. + #TODO: Make sure that the body is actually JSON. + return '{"token": "abcdefghijklmnopqrstuvwxyz"}' + + return 'it did NOT work\n' + + @route('/token/:token_id', method='GET') + def validate_token(token_id): + ''' + Validating token by doing a GET on /token/token_id + ''' + if 'CONTENT_TYPE' in request.environ: + content_types = ['text/plain', 'application/json', + 'application/xml', 'text/xml'] + content = request.environ['CONTENT_TYPE']; + if content in content_types: + if token_id == 'abcdefghijklmnopqrstuvwxyz': + return '{"auth" : { "token": {"id": "ab48a9efdfedb23ty3494", "expires": "2010-11-01T03:32:15-05:00"}, "user" :{"groups"{ "group": []}, "username": "jqsmith", "tenantId": "1234",}{"tenantId" : "1234", "name": "Admin"}}}' + else: + abort(401, "Token not valid") + + return 'it did NOT work\n' debug(True) run(host='localhost', port=8080, reloader=True) -- cgit