def test_user_consent_skipped(self): """ If users previously gave consent to some client (for a specific list of scopes) and because they might be prompted for the same authorization multiple times, the server skip it. """ data = { "client_id": self.client.client_id, "redirect_uri": self.client.default_redirect_uri, "response_type": "code", "scope": "openid email", "state": self.state, "allow": "Accept", } request = self.factory.post(reverse("oidc_provider:authorize"), data=data) # Simulate that the user is logged. request.user = self.user with self.settings(OIDC_SKIP_CONSENT_ALWAYS=True): response = self._auth_request("post", data, is_user_authenticated=True) self.assertIn("code", response["Location"], msg="Code is missing in the returned url.") response = self._auth_request("post", data, is_user_authenticated=True) is_code_ok = is_code_valid(url=response["Location"], user=self.user, client=self.client) self.assertEqual(is_code_ok, True, msg="Code returned is invalid.") del data["allow"] response = self._auth_request("get", data, is_user_authenticated=True) is_code_ok = is_code_valid(url=response["Location"], user=self.user, client=self.client) self.assertEqual(is_code_ok, True, msg="Code returned is invalid or missing.")
def test_response_uri_is_properly_constructed(self): """ Check that the redirect_uri matches the one configured for the client. Only 'state' and 'code' should be appended. """ data = { 'client_id': self.client.client_id, 'redirect_uri': self.client.default_redirect_uri, 'response_type': 'code', 'scope': 'openid email', 'state': self.state, 'allow': 'Accept', } response = self._auth_request('post', data, is_user_authenticated=True) parsed = urlsplit(response['Location']) params = parse_qs(parsed.query or parsed.fragment) state = params['state'][0] self.assertEqual(self.state, state, msg="State returned is invalid or missing") is_code_ok = is_code_valid(url=response['Location'], user=self.user, client=self.client) self.assertTrue(is_code_ok, msg='Code returned is invalid or missing') self.assertEqual( set(params.keys()), {'state', 'code'}, msg='More than state or code appended as query params') self.assertTrue( response['Location'].startswith(self.client.default_redirect_uri), msg='Different redirect_uri returned')
def test_response_uri_is_properly_constructed(self): """ Check that the redirect_uri matches the one configured for the client. Only 'state' and 'code' should be appended. """ data = { 'client_id': self.client.client_id, 'redirect_uri': self.client.default_redirect_uri, 'response_type': 'code', 'scope': 'openid email', 'state': self.state, 'allow': 'Accept', } response = self._auth_request('post', data, is_user_authenticated=True) parsed = urlsplit(response['Location']) params = parse_qs(parsed.query or parsed.fragment) state = params['state'][0] self.assertEquals(self.state, state, msg="State returned is invalid or missing") is_code_ok = is_code_valid(url=response['Location'], user=self.user, client=self.client) self.assertTrue(is_code_ok, msg='Code returned is invalid or missing') self.assertEquals( set(params.keys()), {'state', 'code'}, msg='More than state or code appended as query params') self.assertTrue( response['Location'].startswith(self.client.default_redirect_uri), msg='Different redirect_uri returned')
def test_user_consent_skipped(self): """ If users previously gave consent to some client (for a specific list of scopes) and because they might be prompted for the same authorization multiple times, the server skip it. """ data = { 'client_id': self.client.client_id, 'redirect_uri': self.client.default_redirect_uri, 'response_type': 'code', 'scope': 'openid email', 'state': self.state, 'allow': 'Accept', } request = self.factory.post(reverse('oidc_provider:authorize'), data=data) # Simulate that the user is logged. request.user = self.user with self.settings(OIDC_SKIP_CONSENT_ALWAYS=True): response = self._auth_request('post', data, is_user_authenticated=True) self.assertEqual('code' in response['Location'], True, msg='Code is missing in the returned url.') response = self._auth_request('post', data, is_user_authenticated=True) is_code_ok = is_code_valid(url=response['Location'], user=self.user, client=self.client) self.assertEqual(is_code_ok, True, msg='Code returned is invalid.') del data['allow'] response = self._auth_request('get', data, is_user_authenticated=True) is_code_ok = is_code_valid(url=response['Location'], user=self.user, client=self.client) self.assertEqual(is_code_ok, True, msg='Code returned is invalid or missing.')
def test_user_consent_response(self): """ First, if the user denied the consent we must ensure that the error response parameters are added to the query component of the Redirection URI. Second, if the user allow the RP then the server MUST return the parameters defined in Section 4.1.2 of OAuth 2.0 [RFC6749] by adding them as query parameters to the redirect_uri. """ data = { 'client_id': self.client.client_id, 'redirect_uri': self.client.default_redirect_uri, 'response_type': 'code', 'scope': 'openid email', 'state': self.state, # PKCE parameters. 'code_challenge': FAKE_CODE_CHALLENGE, 'code_challenge_method': 'S256', } response = self._auth_request('post', data, is_user_authenticated=True) # Because user doesn't allow app, SHOULD exists an error parameter # in the query. self.assertEqual('error=' in response['Location'], True, msg='error param is missing in query.') self.assertEqual('access_denied' in response['Location'], True, msg='"access_denied" code is missing in query.') # Simulate user authorization. data['allow'] = 'Accept' # Will be the value of the button. response = self._auth_request('post', data, is_user_authenticated=True) is_code_ok = is_code_valid(url=response['Location'], user=self.user, client=self.client) self.assertEqual(is_code_ok, True, msg='Code returned is invalid.') # Check if the state is returned. state = (response['Location'].split('state='))[1].split('&')[0] self.assertEqual(state == self.state, True, msg='State change or is missing.')
def test_code_idtoken_token_response(self): """ Implicit client requesting `id_token token` receives both id token and access token as the result of the authorization request. """ response = self._auth_request("post", self.data, is_user_authenticated=True) self.assertIn("#", response["Location"]) self.assertIn("access_token", response["Location"]) self.assertIn("id_token", response["Location"]) self.assertIn("state", response["Location"]) self.assertIn("code", response["Location"]) # Validate code. is_code_ok = is_code_valid(url=response["Location"], user=self.user, client=self.client_code_idtoken_token) self.assertEqual(is_code_ok, True, msg="Code returned is invalid.")
def test_code_idtoken_token_response(self): """ Implicit client requesting `id_token token` receives both id token and access token as the result of the authorization request. """ response = self._auth_request('post', self.data, is_user_authenticated=True) self.assertIn('#', response['Location']) self.assertIn('access_token', response['Location']) self.assertIn('id_token', response['Location']) self.assertIn('state', response['Location']) self.assertIn('code', response['Location']) # Validate code. is_code_ok = is_code_valid(url=response['Location'], user=self.user, client=self.client_code_idtoken_token) self.assertEqual(is_code_ok, True, msg='Code returned is invalid.')
def test_user_consent_response(self): """ First, if the user denied the consent we must ensure that the error response parameters are added to the query component of the Redirection URI. Second, if the user allow the RP then the server MUST return the parameters defined in Section 4.1.2 of OAuth 2.0 [RFC6749] by adding them as query parameters to the redirect_uri. """ data = { "client_id": self.client.client_id, "redirect_uri": self.client.default_redirect_uri, "response_type": "code", "scope": "openid email", "state": self.state, # PKCE parameters. "code_challenge": FAKE_CODE_CHALLENGE, "code_challenge_method": "S256", } response = self._auth_request("post", data, is_user_authenticated=True) # Because user doesn't allow app, SHOULD exists an error parameter # in the query. self.assertIn("error=", response["Location"], msg="error param is missing in query.") self.assertIn("access_denied", response["Location"], msg='"access_denied" code is missing in query.') # Simulate user authorization. data["allow"] = "Accept" # Will be the value of the button. response = self._auth_request("post", data, is_user_authenticated=True) is_code_ok = is_code_valid(url=response["Location"], user=self.user, client=self.client) self.assertEqual(is_code_ok, True, msg="Code returned is invalid.") # Check if the state is returned. state = (response["Location"].split("state="))[1].split("&")[0] self.assertEqual(state, self.state, msg="State change or is missing.")