Commits

hideki nara committed 18693bc

Introspect Request seems to work

  • Participants
  • Parent commits d031c6d

Comments (0)

Files changed (5)

nov-sample-admin/rp/management/__init__.py

Empty file added.

nov-sample-admin/rp/management/commands/__init__.py

Empty file added.

nov-sample-admin/rp/management/commands/rp.py

+# -*- coding: utf-8 -*-
+
+from django.core.management.base import BaseCommand, CommandError
+from django.contrib.auth.models import User
+from optparse import make_option
+from datetime import datetime
+
+from ...models import *
+
+class Command(BaseCommand):
+    args = ''
+    help = ''
+    option_list = BaseCommand.option_list + (
+
+        make_option('--charset',
+            action='store',
+            dest='charset',
+            default='utf8',
+            help='end record'),
+
+        )
+
+    def handle(self, *args, **options):
+        if len(args) > 0 :
+            getattr(self, 'handle_%s'% args[0],self.handle_help)(*args,**options)
+        else:
+            self.handle_help(*args,**options )  
+
+    def handle_help(self,*args,**options):
+        print args,options
+
+    ####
+    def handle_introspect_id_token(self,*args, **options):
+        if len(args)>1:
+            print OpenIds.objects.get(id=args[1]).introspect_id_token()
+            return
+
+        print OpenIds.objects.all()[0].introspect_id_token()
+        
+    def handle_introspect_access_token(self,*args, **options):
+        if len(args)>1:
+            print OpenIds.objects.get(id=args[1]).introspect_access_token()
+            return
+
+        print OpenIds.objects.all()[0].introspect_access_token()

nov-sample-admin/rp/models.py

 from __future__ import unicode_literals
 
 from django.db import models
+import requests
+from django.utils import simplejson
 
 class Accounts(models.Model):
     id = models.IntegerField(primary_key=True)
     id_token = models.CharField(max_length=1024, blank=True)
     created_at = models.DateTimeField()
     updated_at = models.DateTimeField()
+
     class Meta:
         db_table = 'open_ids'
 
+    def __init__(self,*args,**kwargs):
+        super(OpenIds,self).__init__(*args,**kwargs)
+        self._provider = None
+
+    @property 
+    def provider(self):
+        if self._provider == None:
+            self._provider =  Providers.objects.get(id = self.provider_id)
+        return self._provider
+
+    @property
+    def authorization_header (self):
+        return {"Authorization": "Bearer %s" % self.access_token}
+
+    def get_resource(self,endpoint):
+        res = requests.get( endpoint, headers=self.authorization_header )
+        return res.json() 
+
+    def post_resource(self,endpoint,**kwargs):
+        res = requests.post( endpoint, data=kwargs,headers=self.authorization_header )
+        return res.json()
+
+    def get_user_info(self):
+        return self.get_resource( self.provider.userinfo_endpoint)
+
+    def introspect_test(self):
+        return self.get_resource( self.provider.introspect_endpoint)
+
+    def introspect_id_token(self):
+        return self.post_resource( self.provider.introspect_endpoint,
+            token = self.id_token,
+            token_type_hint = "id_token",
+            )
+    def introspect_access_token(self):
+        return self.post_resource( self.provider.introspect_endpoint,
+            token = self.access_token,
+            token_type_hint = "access_token",
+            )
+        
+
 class Providers(models.Model):
     id = models.IntegerField(primary_key=True)
     account_id = models.IntegerField(null=True, blank=True)
     expires_at = models.DateTimeField(null=True, blank=True)
     created_at = models.DateTimeField()
     updated_at = models.DateTimeField()
+
+    #
     class Meta:
         db_table = 'providers'
 
+    @property
+    def introspect_endpoint(self):
+        return self.userinfo_endpoint.replace('user_info','introspect')
+
 class SchemaMigrations(models.Model):
     version = models.CharField(max_length=255, unique=True)
     class Meta:
+BeautifulSoup
+Django
+Sphinx
+pysqlite
+pytz