def existing_tacacsplus_user(): try: user = User.objects.get(username="******") except User.DoesNotExist: user = User(username="******") user.save() enterprise_auth = UserEnterpriseAuth(user=user, provider='tacacs+') enterprise_auth.save() return user
def test_token_creation_disabled_for_external_accounts(oauth_application, post, alice, allow_oauth, status): UserEnterpriseAuth(user=alice, provider='radius').save() url = drf_reverse('api:oauth_authorization_root_view') + 'token/' with override_settings(RADIUS_SERVER='example.org', ALLOW_OAUTH2_FOR_EXTERNAL_USERS=allow_oauth): resp = post( url, data='grant_type=password&username=alice&password=alice&scope=read', content_type='application/x-www-form-urlencoded', HTTP_AUTHORIZATION='Basic ' + smart_str( base64.b64encode( smart_bytes(':'.join([ oauth_application.client_id, oauth_application.client_secret ])))), status=status) if allow_oauth: assert AccessToken.objects.count() == 1 else: assert 'OAuth2 Tokens cannot be created by users associated with an external authentication provider' in smart_str( resp.content) # noqa assert AccessToken.objects.count() == 0
def test_existing_token_enabled_for_external_accounts(oauth_application, get, post, admin): UserEnterpriseAuth(user=admin, provider='radius').save() url = drf_reverse('api:oauth_authorization_root_view') + 'token/' with override_settings(RADIUS_SERVER='example.org', ALLOW_OAUTH2_FOR_EXTERNAL_USERS=True): resp = post( url, data='grant_type=password&username=admin&password=admin&scope=read', content_type='application/x-www-form-urlencoded', HTTP_AUTHORIZATION='Basic ' + smart_str(base64.b64encode(smart_bytes(':'.join([ oauth_application.client_id, oauth_application.client_secret ])))), status=201 ) token = json.loads(resp.content)['access_token'] assert AccessToken.objects.count() == 1 with immediate_on_commit(): resp = get( drf_reverse('api:user_me_list', kwargs={'version': 'v2'}), HTTP_AUTHORIZATION='Bearer ' + token, status=200 ) assert json.loads(resp.content)['results'][0]['username'] == 'admin' with override_settings(RADIUS_SERVER='example.org', ALLOW_OAUTH2_FOR_EXTERNAL_USER=False): with immediate_on_commit(): resp = get( drf_reverse('api:user_me_list', kwargs={'version': 'v2'}), HTTP_AUTHORIZATION='Bearer ' + token, status=200 ) assert json.loads(resp.content)['results'][0]['username'] == 'admin'
def _decorate_enterprise_user(user, provider): user.set_unusable_password() user.save() enterprise_auth = UserEnterpriseAuth(user=user, provider=provider) enterprise_auth.save() return enterprise_auth