Browse Source

WIP consumer and openid client api

Arpit Singh 5 years ago
parent
commit
e035a958e2

+ 17 - 0
SampleApp/config.py

@@ -0,0 +1,17 @@
+# Provider
+OpenID_SERVER            = 'http://localhost:8000'
+
+# General urls on your provider:
+OpenID_AUTHORIZATION_URL = '/openid/authorize'   # Authorization URL
+OpenID_TOKEN_URL         = '/openid/token/'      # Access token URL
+
+# The URL of some protected resource on your oauth2 server which you have configured to serve
+# json-encoded user information (containing at least an email) for the user associated
+# with a given access token.
+OpenID_RESOURCE_URL = '/userinfo'
+
+# From the configuration of your client site in the oauth2 provider
+OpenID_CLIENT_ID         = '088411'
+OpenID_CLIENT_SECRET     = '92222d5cc1f3eb7f245317c10aff3edb86339f92418e7a37473ff18b'
+
+OpenID_CALLBACK_URL      = 'http://localhost:8005/consumer/exchange'

+ 1 - 19
SampleApp/settings.py

@@ -55,7 +55,7 @@ ROOT_URLCONF = 'SampleApp.urls'
 TEMPLATES = [
     {
         'BACKEND': 'django.template.backends.django.DjangoTemplates',
-        'DIRS': [],
+        'DIRS': ['templates',],
         'APP_DIRS': True,
         'OPTIONS': {
             'context_processors': [
@@ -119,21 +119,3 @@ USE_TZ = True
 # https://docs.djangoproject.com/en/2.0/howto/static-files/
 
 STATIC_URL = '/static/'
-
-# Provider
-OpenID_SERVER            = 'localhost:8000'
-
-# General urls on your provider:
-OpenID_AUTHORIZATION_URL = '/o/authorize'   # Authorization URL
-OpenID_TOKEN_URL         = '/o/token/'      # Access token URL
-
-# The URL of some protected resource on your oauth2 server which you have configured to serve
-# json-encoded user information (containing at least an email) for the user associated
-# with a given access token.
-OpenID_RESOURCE_URL = '/userinfo'
-
-# From the configuration of your client site in the oauth2 provider
-OpenID_CLIENT_ID         = 'SQm9g9AT7Rn4TKvnYlBZSszUQFnQOseq3O4XKxW6'
-OpenID_CLIENT_SECRET     = 'MvJ8u5xD9j0KVTQKCyDxf4La6nJeOmIlXQKYhygWGjZAcv8drK2TjgrtRvaMD9BPRiONnbMYZIG3Eih7ujeTZc88IH6VkvSpmP8nXA4aTi61h8ufOh6mWsq9xjlrwi21'
-
-OpenID_CALLBACK_URL      = 'localhost:8005/callback'

+ 2 - 4
SampleApp/urls.py

@@ -14,10 +14,8 @@ Including another URLconf
     2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
 """
 from django.contrib import admin
-from django.urls import path
-from testOpenId import views as testOpenId_views
+from django.urls import path, include
 
 urlpatterns = [
-    path('admin/', admin.site.urls),
-    path('home', testOpenId_views.home, name='home'),
+    path('consumer/', include('testOpenId.urls')),
 ]

+ 32 - 0
templates/testOpenId/auth_link.html

@@ -0,0 +1,32 @@
+<!DOCTYPE html>
+<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+    <title> Example</title>
+    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
+    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">
+    <style type="text/css">
+        body {
+            padding-top: 70px;
+        }
+
+        footer {
+            border-top: 1px solid #eee;
+            margin-top: 40px;
+            padding-top: 30px;
+            padding-bottom: 30px;
+        }
+    </style>
+    
+</head>
+<body>
+
+
+<div class="container">
+  
+  <h1>Test your OAuth2 provider, I'll be your consumer</h1>
+    <p>Now click, give your authorization and see you later, possibly with an access token</p>
+    <a href={{url}}>{{url}}</a>
+  
+
+</div>
+
+</body></html>

+ 32 - 0
templates/testOpenId/displaytoken.html

@@ -0,0 +1,32 @@
+<!DOCTYPE html>
+<html>
+<head>
+	<title>Auth Token</title>
+</head>
+<body>
+<table>
+	<tbody>
+		<tr>
+			<th>id_token</th>
+			<td>{{id_token}}</td>
+		</tr>
+		<tr>
+			<th>access_token</th>
+			<td>{{access_token}}</td>
+		</tr>
+		<tr>
+			<th>token_type</th>
+			<td>{{token_type}}</td>
+		</tr>
+		<tr>
+			<th>refresh_token</th>
+			<td>{{refresh_token}}</td>
+		</tr>
+		<tr>
+			<th>expires_in</th>
+			<td>{{expires_in}}</td>
+		</tr>
+	</tbody>
+</table>
+</body>
+</html>

+ 44 - 0
testOpenId/openid_api.py

@@ -0,0 +1,44 @@
+from SampleApp import config
+import requests
+
+authorization_url = config.OpenID_SERVER + config.OpenID_AUTHORIZATION_URL
+token_url = config.OpenID_SERVER + config.OpenID_TOKEN_URL
+userinfo_url = config.OpenID_SERVER + config.OpenID_RESOURCE_URL
+
+class OpenID_CLIENT(object):
+    """docstring for OpenID_CLIENT"""
+    def __init__(self, *arg):
+        super(OpenID_CLIENT, self).__init__()
+        
+    # def get_code(self):
+    #     auth_url = authorization_url + "?client_id="+ config.OpenID_CLIENT_ID+"&response_type=code&redirect_uri="+config.OpenID_CALLBACK_URL
+    #     response = requests.get(auth_url)
+    #     import pdb;pdb.set_trace()
+    #     return code
+
+    def get_access_token(self, code):
+        request_data =  {
+            'client_id' : config.OpenID_CLIENT_ID,
+            'client_secret' : config.OpenID_CLIENT_SECRET,
+            'redirect_uri' : config.OpenID_CALLBACK_URL,
+            'grant_type' : 'authorization_code',
+            'code' : code
+            }
+        response = requests.post(token_url, data = request_data)
+        return response.json()
+
+    def get_refresh_token(self, refresh_token):
+        request_data = {
+            'client_id' : config.OpenID_CLIENT_ID,
+            'client_secret' : config.OpenID_CLIENT_SECRET,
+            'redirect_uri' : config.OpenID_CALLBACK_URL,
+            'grant_type' : 'authorization_code',
+            'refresh_token' : refresh_token
+        }
+        response = requests.post(token_url, data = request_data)
+        return response.json()
+
+
+    def get_userinfo(self, access_token):
+        response = requests.get(userinfo_url+"?access_token=%s"%access_token)
+        return response

+ 7 - 0
testOpenId/urls.py

@@ -0,0 +1,7 @@
+from django.urls import path
+from . import views
+
+urlpatterns = [
+	path('', views.home, name = 'home'),
+	path('exchange/', views.exchange, name='exchange'),
+]

+ 21 - 0
testOpenId/views.py

@@ -1,3 +1,24 @@
 from django.shortcuts import render
+from django.http import HttpResponse, HttpResponseRedirect
+
+from SampleApp import config
+from testOpenId.openid_api import OpenID_CLIENT
+
+
+authorization_url = config.OpenID_SERVER + config.OpenID_AUTHORIZATION_URL
+token_url = config.OpenID_SERVER + config.OpenID_TOKEN_URL
+
 
 # Create your views here.
+
+def home(request):
+    url = authorization_url+"?client_id="+ config.OpenID_CLIENT_ID+"&response_type=code&redirect_uri="+config.OpenID_CALLBACK_URL
+    return render(request, 'testOpenId/auth_link.html', {'url':url})
+
+def exchange(request):
+    # import pdb;pdb.set_trace()
+    code = request.GET.get('code').strip(),
+    obj = OpenID_CLIENT()
+    response = obj.get_access_token(code)
+    print(response)
+    return render(request, 'testOpenId/displaytoken.html', response)