def test_compare_url(self): # query 안에 url이 있고, 그 url 안에 query가 또 있는 경우. base_url_path = 'https://account.ridibooks.com/ridi/callback/' first_random_str = generate_random_str(30) second_random_str = generate_random_str(30) query_url = generate_query_url(base_url_path, {'a': first_random_str, 'b': second_random_str}) different_order_query_url = generate_query_url(base_url_path, {'b': second_random_str, 'a': first_random_str}) self.assertTrue(is_same_url(query_url, different_order_query_url))
def wrapper(self, request, *args, **kwargs): ridibooks_session_id = request.COOKIES.get(PHP_SESSION_COOKIE_KEY, None) user = AnonymousUser() if ridibooks_session_id is not None: try: account_info = StoreApi( phpsession_id=ridibooks_session_id).get_account_info() except RidibooksException: pass else: user, _ = get_user_model().objects.get_or_create( idx=account_info['result']['idx'], id=account_info['result']['id']) if not user.is_authenticated: redirect_uri = generate_query_url( reverse('account:login'), {'next': request.get_full_path()}) return HttpResponseRedirect(redirect_uri) request.user = user return _func(self, request, *args, **kwargs)
def test_compare_complex_url(self): base_url_path = 'https://account.ridibooks.com/ridi/callback/' first_random_str = generate_random_str(30) second_random_str = generate_random_str(30) random_str = generate_random_str(30) query_url = generate_query_url(base_url_path, {'a': first_random_str, 'b': second_random_str}) different_order_query_url = generate_query_url(base_url_path, {'b': second_random_str, 'a': first_random_str}) url = generate_query_url(base_url_path, {'a': query_url, 'b': random_str}) different_order_query_url = generate_query_url(base_url_path, {'a': different_order_query_url, 'b': random_str}) different_param_url = generate_query_url( base_url_path, {'a': query_url, 'b': random_str, 'c': generate_random_str(30)} ) self.assertTrue(is_same_url(url, different_order_query_url)) self.assertFalse(is_same_url(url, different_param_url))
def get_oauth2_authorize_url(cls, client_id: str, redirect_uri: str, u_idx: str) -> str: cls._validate_client_and_redirect_uri(client_id, redirect_uri) params = { 'client_id': client_id, 'redirect_uri': UrlHelper.get_redirect_url(redirect_uri, client_id), 'response_type': 'code', 'state': StateHelper.create_encrypted_state(u_idx), } url = generate_query_url(reverse('oauth2_provider:authorize'), params) return url
def _login_with_store(self, otp: str, redirect_uri: str): try: u_idx, _ = SSOOtpService.verify(SSOConfig.get_sso_otp_key(), otp) except FailVerifyOtpException: return HttpResponseForbidden() new_otp = SSOOtpService.generate(SSOConfig.get_sso_otp_key(), u_idx) return redirect( generate_query_url(SSOConfig.get_sso_store_login_url(), { 'token': new_otp, 'return_url': redirect_uri }))
def get(self, request, *args, **kwargs): if request.user.is_authenticated: # 로그인 되어 있으면 Next로 이동한다. redirect_to = self.get_success_url() if redirect_to == request.path: raise ErrorException('LOGIN REDIRECT URL IS SAME!') else: # 로그인 안되어 있으면 리디북스 홈페이지로 이동하고 돌아온다. params = {'return_url': request.build_absolute_uri()} url = GeneralConfig.get_ridibooks_login_url() redirect_to = generate_query_url(url, params) return HttpResponseRedirect(redirect_to)
def get(self, request): authorize_form = AuthorizationCodeForm(request.GET) if not authorize_form.is_valid(): return get_invalid_form_template_response(request, authorize_form) cleaned_data = authorize_form.clean() try: code = OAuth2AuthorizationCodeService.create_code( cleaned_data['client_id'], cleaned_data['redirect_uri'], request.user.idx) except OAuth2Error as e: return get_template_response(request, e.error, e.description, e.status_code) redirect_param = {'code': code} if cleaned_data['state']: redirect_param['state'] = cleaned_data['state'] redirect_uri = generate_query_url(cleaned_data['redirect_uri'], redirect_param) return HttpResponseRedirect(redirect_uri)
def get_redirect_url(cls, in_house_redirect_uri: str, client_id: str) -> str: return generate_query_url(cls.get_callback_view_url(), { 'in_house_redirect_uri': in_house_redirect_uri, 'client_id': client_id })