Package: django-locksmith
Django utilities for shared api authentication & analytics.django-locksmith provides a few Django applications, locksmith.hub, locksmith.auth, and locksmith.mongoauth.locksmith.hub is an application that facilitates signup and has generic analytics support that supports reports from any APIlocksmith.auth provides mechanisms for being issued an API key from a hub server and authenticating API calls.  It also includes a management command to submit nightly reports to a locksmith.hub instance.locksmith.mongoauth provies the same interface as locksmith.auth but instead of using Django’s ORM it relies upon pymongo and uses a MongoDB database.  Like locksmith.auth it includes a way to submit nightly usage reports to a locksmith.hub instance.django-locksmith is a project of Sunlight Labs (c) 2011.  Written by James Turk <jturk@sunlightfoundation.com>.All code is under a BSD-style license, see LICENSE for details.RequirementsFor locksmith.hub:
* python >= 2.6
* Django >= 1.4
* South >= 0.7.6
* django-taggit
* celery >= 3.0.16For locksmith.auth and locksmith.mongoauth:
* python >= 2.5
* Django >= 1.2locksmith.hublocksmith.hub provides several views, an authentication backend, and a management command.  A single instance of locksmith.hub can serve as the core of any number of related APIs.SettingsTo use locksmith.hub it is necessary to add a few things to settings.py:InINSTALLED_APPSadd ‘locksmith.hub’.InAUTHENTICATION_BACKENDSadd ‘locksmith.hub.auth_backend.LocksmithBackend’This will make the locksmith.hub models and management command available and allow users to sign in using their email and apikey.Optional settings:LOCKSMITH_EMAIL_SUBJECT: if set will be used as the subject of the email sent when users request a key. (defaults to ‘API Registration’)LOCKSMITH_REPLICATED_APIS: Locksmith has two key synchronization mechanisms. The older, stateful mechanism is used by default. To opt into using the newer, stateless mechanism for an API, add the name of the API here. This is a temporary setting. In 0.8 all APIs will use the new mechanism and this setting will prompt a deprecation warning.TemplatesThe locksmith.hub app provides a set of front-end templates that can be integrated into your website. To provide for maximum flexibility, these templates extend your project base template. You should specify the path to that base template usingLOCKSMITH_BASE_TEMPLATE. You should also specify a template to display when the user lacks authorization for the given view by settingLOCKSMITH_UNAUTHORIZED_TEMPLATE.Each of the locksmith.hub views allow you to specify a view to render instead of the default. This allows you to use {% include %} tags in both the base and top-level template. If you need to, you can override each view’s template using these settings:LOCKSMITH_ANALYTICS_INDEX_TEMPLATELOCKSMITH_API_ANALYTICS_TEMPLATELOCKSMITH_KEY_ANALYTICS_TEMPLATELOCKSMITH_KEYS_LIST_TEMPLATELOCKSMITH_KEYS_LEADERBOARD_TEMPLATELOCKSMITH_REGISTER_TEMPLATEURLslocksmith.hub provides two sets of URL patterns, one for registration and the other for analytics.They can be added to your urls.py with the following line:(r’^’, include(‘locksmith.hub.urls’)),This adds an accounts/ path that contains user-facing urls and an analytics/ path that adds analytics views that are only accessible by staff members.locksmith.auth and locksmith.mongoauthlocksmith.auth and locksmith.mongoauth serve the same purpose and are basically interchangable.Both apps provide URL endpoints and a management command that aim to make writing an API that gets its authentication details from a locksmith.hub instance as simple as possible.In most places below they are interchangable and are denoted aslocksmith.(mongo)auth, uselocksmith.mongoauthif you prefer your keys/logs to be stored in MongoDB, and uselocksmith.authif you prefer to use standard Django models.Installationadd ‘locksmith.(mongo)auth’ toINSTALLED_APPSadd a line like: (r’^api/’, include(‘locksmith.(mongo)auth.urls’)) to urls.pyAdd the following settings to your settings.py:LOCKSMITH_HUB_URLURL to your locksmith.hub instanceLOCKSMITH_SIGNING_KEYsigning key for this locksmith instanceLOCKSMITH_API_NAMEname of this locksmith instanceYou can optionally enable theAPIKeyMiddlewareby adding'locksmith.(mongo)auth.middleware.APIKeyMiddleware'to yourMIDDLEWARE_CLASSES.  This middleware makes use of two more optional settings:LOCKSMITH_QS_PARAMQuery String parameter to check for API key (default: apikey)LOCKSMITH_HTTP_HEADERHTTP header to check for API key (default: HTTP_X_APIKEY)locksmith.auth settingsIf using locksmith.auth it is necessary to add a few extra settings to enable reporting statistics via./manage.py apireport:LOCKSMITH_STATS_APPapplication of the API log model (default: api)LOCKSMITH_STATS_MODELname of the API log model (default: LogEntry)LOCKSMITH_STATS_DATE_FIELDname of the timestamp field on the log model (default: timestamp)LOCKSMITH_STATS_ENDPOINT_FIELDname of the endpoint field on the log model (default: method)LOCKSMITH_STATS_USER_FIELDname of the key field on the log model (default: caller_key)locksmith.mongoauth settingsIf using locksmith.mongoauth several settings can be provided to configure the MongoDB connection.LOCKSMITH_MONGO_HOSTaddress of mongodb server (default: localhost)LOCKSMITH_MONGO_PORTport of mongodb server (default: 27017)LOCKSMITH_MONGO_DATABASEname of mongodb database (default: locksmith)UsageIf usinglocksmith.auththelocksmith.auth.models.ApiKeymodel is used to store information on the API key.If usinglocksmith.mongoautha collection namedlocksmith.keyswill be created with ‘_id’, ‘status’, and ‘email’ fields.When a user passes a key to your API you should check if such anApiKeyobject exists and if it is active (ie. status=’A’) before serving the request.  This check is handled automatically if you are using the providedAPIKeyMiddleware.Reporting StatisticsTo report usage of your API back to thelocksmith.hubinstance you can call./manage.py apireportdaily.connecting a locksmith.hub and locksmith.auth instanceAssuming that you have alocksmith.hubinstance and alocksmith.(mongo)authinstance running as indicated above, the final step is to connect the two so that API signups become actual usable keys and analytics get back.# hub: Add a newlocksmith.hub.Apiinstance for the new API (choosing a name and signing key)
# hub: Push all existing keys to the new API’s locksmith auth endpoints by calling./manage.py pushkeysAssuming you already have a regular cronjob that pushes out new keys the new API will now get notified of new keys along with all of your other APIs.TODO: document how non-locksmith.auth-enabled APIs can push to locksmith.hub