f5327d6b by 周伟奇

add iwa logiin

1 parent 3d620b3b
...@@ -4,4 +4,6 @@ from . import views ...@@ -4,4 +4,6 @@ from . import views
4 4
5 urlpatterns = [ 5 urlpatterns = [
6 path(r'login/', views.LoginView.as_view()), 6 path(r'login/', views.LoginView.as_view()),
7 path(r'iwalogin/', views.IWALoginView.as_view()),
8 path(r'iwaurl/', views.IWAUrlView.as_view()),
7 ] 9 ]
......
1 from common.mixins import GenericView 1 import base64
2 from common.mixins import GenericView, IWABaseView
2 from rest_framework import status 3 from rest_framework import status
3 from rest_framework_jwt.views import ObtainJSONWebToken 4 from rest_framework_jwt.views import ObtainJSONWebToken
4 from common import response 5 from common import response
5 from common.redis_cache import redis_handler as rh 6 from common.redis_cache import redis_handler as rh
6 from .consts import LOGIN_TIMES_LIMIT_EXPIRES, LOGIN_TIMES_LIMIT 7 from .consts import LOGIN_TIMES_LIMIT_EXPIRES, LOGIN_TIMES_LIMIT
8 from settings import conf
7 9
8 # Create your views here. 10 # Create your views here.
9 11
12 # https://auth-i.bmwgroup.net/auth/oauth2/intranetb2x/
13 iwa_url_params = {
14 'scope': 'openid',
15 'response_type': 'code',
16 'redirect_uri': conf.REDIRECT_URI,
17 'client_id': conf.IWA_CLIENT_ID
18 }
19 iwa_url_params_str = '&'.join(['{0}={1}'.format(k, v) for k, v in iwa_url_params])
20 iwa_url = '{0}authorize?{1}'.format(conf.IWA_URL, iwa_url_params_str)
21 client_id_base64 = base64.b64encode('{0}:{1}'.format(
22 conf.IWA_CLIENT_ID, conf.IWA_CLIENT_SECRET).encode('utf-8')).decode('utf-8')
23
10 24
11 class LoginView(ObtainJSONWebToken, GenericView): 25 class LoginView(ObtainJSONWebToken, GenericView):
12 26
...@@ -32,3 +46,27 @@ class LoginView(ObtainJSONWebToken, GenericView): ...@@ -32,3 +46,27 @@ class LoginView(ObtainJSONWebToken, GenericView):
32 'token': res.data.get('token'), 46 'token': res.data.get('token'),
33 } 47 }
34 return response.ok(data=data) 48 return response.ok(data=data)
49
50
51 class IWALoginView(IWABaseView, GenericView):
52
53 def post(self, request, *args, **kwargs):
54 code = request.data.get('code', '')
55 redirect_uri = request.data.get('redirect_uri', '')
56 q_number = self.get_q_number(conf.IWA_URL, code, redirect_uri, client_id_base64)
57
58 is_valid, data = self.validate(q_number)
59
60 if is_valid:
61 return response.ok(data=data)
62 else:
63 self.no_permission(data)
64
65
66 class IWAUrlView(IWABaseView, GenericView):
67
68 def get(self, request, *args, **kwargs):
69 data = {
70 'iwa_url': iwa_url,
71 }
72 return response.ok(data=data)
......
1 import logging 1 import logging
2 import requests
3 from django.contrib.auth import get_user_model
2 from django.contrib.auth.models import AnonymousUser 4 from django.contrib.auth.models import AnonymousUser
3 from rest_framework.generics import GenericAPIView 5 from rest_framework.generics import GenericAPIView
6 from rest_framework_jwt.utils import jwt_payload_handler, jwt_encode_handler
4 from common.exceptions import ( 7 from common.exceptions import (
5 NeedLoginException, 8 NeedLoginException,
6 InvalidParamsException, 9 InvalidParamsException,
...@@ -100,3 +103,59 @@ class GenericView(LoggerMixin, GenericExceptionMixin, GenericAPIView): ...@@ -100,3 +103,59 @@ class GenericView(LoggerMixin, GenericExceptionMixin, GenericAPIView):
100 103
101 def get_object(self): 104 def get_object(self):
102 return None 105 return None
106
107
108 class IWABaseView:
109
110 # def __init__(self):
111 # self.iwa_url_base = 'https://auth-i.bmwgroup.net/auth/oauth2/intranetb2x/'
112
113 @staticmethod
114 def get_token(iwa_url_base, code, redirect_uri, client_id_base64):
115 headers = {
116 'authorization': 'Basic {0}'.format(client_id_base64), # client_id:secret做base64encode
117 'content-type': 'application/x-www-form-urlencoded',
118 }
119 get_params_dict = {
120 'grant_type': 'authorization_code',
121 'code': code,
122 'redirect_uri': redirect_uri,
123 }
124 get_params_str = '&'.join(['{0}={1}'.format(k, v) for k, v in get_params_dict])
125 iwa_token_url = '{0}access_token?{1}'.format(iwa_url_base, get_params_str)
126 res = requests.post(iwa_token_url, headers=headers)
127
128 return res.json().get('access_token', '')
129
130 def get_q_number(self, iwa_url_base, code, redirect_uri, client_id_base64):
131 access_token = self.get_token(iwa_url_base, code, redirect_uri, client_id_base64)
132 headers = {
133 'authorization', 'Bearer {0}'.format(access_token)
134 }
135 iwa_user_url = '{0}userinfo'.format(iwa_url_base)
136 res = requests.get(iwa_user_url, headers=headers)
137
138 return res.json().get('sub', '')
139
140 @staticmethod
141 def validate(q_number):
142 if not q_number:
143 return False, 'get q_number empty'
144 user = get_user_model().objects.filter(username=q_number).first()
145 if user:
146 if not user.is_active:
147 msg = 'User account is disabled.'
148 return False, msg
149
150 payload = jwt_payload_handler(user)
151
152 user_info = {
153 'user_id': user.id,
154 'user_name': user.username,
155 'token': jwt_encode_handler(payload),
156 }
157
158 return True, user_info
159 else:
160 msg = 'q_number user not found'
161 return False, msg
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!