Added Callback support for API apps

This commit is contained in:
2011-09-24 17:54:24 +01:00
parent 46045db0e0
commit c206c49ceb
3 changed files with 70 additions and 2 deletions

View File

@@ -0,0 +1,40 @@
# encoding: utf-8
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
# Adding field 'AuthAPIKey.callback'
db.add_column('api_authapikey', 'callback', self.gf('django.db.models.fields.CharField')(default='', max_length=200, blank=True), keep_default=False)
def backwards(self, orm):
# Deleting field 'AuthAPIKey.callback'
db.delete_column('api_authapikey', 'callback')
models = {
'api.authapikey': {
'Meta': {'object_name': 'AuthAPIKey'},
'active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
'callback': ('django.db.models.fields.CharField', [], {'max_length': '200', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'key': ('django.db.models.fields.CharField', [], {'max_length': '200', 'blank': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '200'}),
'url': ('django.db.models.fields.CharField', [], {'max_length': '200', 'blank': 'True'})
},
'api.authapilog': {
'Meta': {'ordering': "['access_datetime']", 'object_name': 'AuthAPILog'},
'access_datetime': ('django.db.models.fields.DateTimeField', [], {}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'key': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['api.AuthAPIKey']"}),
'url': ('django.db.models.fields.CharField', [], {'max_length': '200'})
}
}
complete_apps = ['api']

View File

@@ -6,9 +6,10 @@ class AuthAPIKey(models.Model):
""" Auth API Key storage model """
name = models.CharField("Service Name", max_length=200)
url = models.CharField("Service URL", max_length=200, blank=True)
url = models.CharField("Service URL", max_length=200, blank=True, help_text="URL that the service is available at")
active = models.BooleanField(default=True)
key = models.CharField("API Key", max_length=200, blank=True)
key = models.CharField("API Key", max_length=200, blank=True, help_text="API key for the service to use")
callback = models.CharField("Callback URL", max_length=200, blank=True, help_text="URL for the callback service to connect to")
def save(self, *args, **kwargs):
if not self.key or self.key == '':

View File

@@ -1,12 +1,18 @@
import sys
import logging
import urllib
import urllib2
from hashlib import sha512
from django.conf import settings
from django.db.models import signals
from django.utils import simplejson as json
from django.contrib.auth.models import User
from celery.signals import task_failure
from celery.decorators import task
from api.models import AuthAPIKey
from eve_api.models import EVEAccount, EVEPlayerCorporation, EVEPlayerAlliance
from eve_api.app_defines import *
from sso.models import ServiceAccount, SSOUser
@@ -94,6 +100,27 @@ def update_user_access(user, **kwargs):
servacc.save()
pass
notifyurls = AuthAPIKey.objects.filter(active=True).exclude(callback='')
if notifyurls.count():
data = {'username': user.username, 'groups': list(user.groups.all().values('id', 'name'))}
# Update remote services with poking the notification URLs
for endpoint in notifyurls:
url, key = endpoint.callback, endpoint.key
jsonstr = json.dumps(data)
hash = sha512('%s-%s' % (key, jsonstr)).hexdigest()
req = urllib2.Request(url, urllib.urlencode({'data': jsonstr, 'auth': hash}))
try:
if sys.version_info < (2, 6):
conn = urllib2.urlopen(req)
else:
conn = urllib2.urlopen(req, timeout=5)
except (urllib2.HTTPError, urllib2.URLError) as e:
# logger.error('Error notifying SSO service: %s' % e.code, exc_info=sys.exc_info(), extra={'data': {'url': url}})
pass
else:
if settings.DEBUG:
print conn.read()
update_service_groups.delay(user_id=user.id)