def home(request): client = Client(settings.CAS_SETTINGS["client_id"], settings.CAS_SETTINGS["client_secret"], settings.SIGNIN_BACK, settings.CAS_SETTINGS["authorization_uri"], settings.CAS_SETTINGS["token_uri"], settings.CAS_SETTINGS["openid_uri"], settings.CAS_SETTINGS["user_api_uri"]) uri = client.get_authorization_code_uri(scope="get_user_info get_user_group") return render_to_response('home.html', {"uri": uri, "CBS_URL": settings.CBS_URL}, context_instance=RequestContext(request))
def signin(self, request, **kwargs): code = request.GET['code'] client = Client(settings.CAS_SETTINGS["client_id"], settings.CAS_SETTINGS["client_secret"], settings.SIGNIN_BACK, settings.CAS_SETTINGS["authorization_uri"], settings.CAS_SETTINGS["token_uri"], settings.CAS_SETTINGS["openid_uri"], settings.CAS_SETTINGS["user_api_uri"]) openid = None try: token = client.get_token(code=code) error = token.get('error') if error: token = None else: # 获取openid openid = client.get_openid(token['access_token'])['openid'] except Exception as e: print e if openid: access_token = token["access_token"] user_info = client.get_user_info(access_token, openid) try: User.objects.get(id=openid) except ObjectDoesNotExist: # 创建新的用户 user_info = client.get_user_info(access_token, openid) User.objects.create_user(user_info["username"], user_info.get("email"), id=openid) user = authenticate(openid=openid) if user: login(request, user) try: self.get_user_group_info(request, client, access_token) except Exception as e: pass return redirect(reverse("home"))