Skip to content

Commit

Permalink
[Social authentication] Remove server part && tests (cvat-ai#5691)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marishka17 committed Feb 22, 2023
1 parent f7838b1 commit 3066c6e
Show file tree
Hide file tree
Showing 28 changed files with 34 additions and 947 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ jobs:
run: |
docker load --input /tmp/cvat_server/image.tar
docker run --rm -v ${PWD}/cvat-sdk/schema/:/transfer \
-e USE_ALLAUTH_SOCIAL_ACCOUNTS="True" \
--entrypoint /bin/bash -u root cvat/server \
-c 'python manage.py spectacular --file /transfer/schema.yml'
pip3 install --user -r cvat-sdk/gen/requirements.txt
Expand Down Expand Up @@ -274,13 +273,11 @@ jobs:
- name: Run CVAT instance
run: |
docker compose \
--env-file="tests/python/social_auth/.env" \
-f docker-compose.yml \
-f docker-compose.dev.yml \
-f components/serverless/docker-compose.serverless.yml \
-f tests/docker-compose.minio.yml \
-f tests/docker-compose.file_share.yml \
-f tests/python/social_auth/docker-compose.yml up -d
-f tests/docker-compose.file_share.yml up -d
- name: Waiting for server
env:
Expand Down
5 changes: 1 addition & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ jobs:
run: |
docker load --input /tmp/cvat_server/image.tar
docker run --rm -v ${PWD}/cvat-sdk/schema/:/transfer \
-e USE_ALLAUTH_SOCIAL_ACCOUNTS="True" \
--entrypoint /bin/bash -u root cvat/server \
-c 'python manage.py spectacular --file /transfer/schema.yml'
pip3 install --user -r cvat-sdk/gen/requirements.txt
Expand Down Expand Up @@ -262,13 +261,11 @@ jobs:
- name: Run CVAT instance
run: |
docker compose \
--env-file="tests/python/social_auth/.env" \
-f docker-compose.yml \
-f docker-compose.dev.yml \
-f components/serverless/docker-compose.serverless.yml \
-f tests/docker-compose.minio.yml \
-f tests/docker-compose.file_share.yml \
-f tests/python/social_auth/docker-compose.yml up -d
-f tests/docker-compose.file_share.yml up -d
- name: Waiting for server
env:
API_ABOUT_PAGE: "localhost:8080/api/server/about"
Expand Down
4 changes: 1 addition & 3 deletions .github/workflows/schedule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,11 @@ jobs:
- name: Run CVAT instance
run: |
docker compose \
--env-file="tests/python/social_auth/.env" \
-f docker-compose.yml \
-f docker-compose.dev.yml \
-f tests/docker-compose.file_share.yml \
-f tests/docker-compose.minio.yml \
-f components/serverless/docker-compose.serverless.yml \
-f tests/python/social_auth/docker-compose.yml up -d
-f components/serverless/docker-compose.serverless.yml up -d
- name: Waiting for server
id: wait-server
Expand Down
6 changes: 4 additions & 2 deletions cvat-core/src/server-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2328,8 +2328,10 @@ async function receiveWebhookEvents(type: WebhookSourceType): Promise<string[]>
async function socialAuthentication(): Promise<any> {
const { backendAPI } = config;
try {
const response = await Axios.get(`${backendAPI}/auth/social/methods`);
return response.data;
const response = await Axios.get(`${backendAPI}/auth/social/methods`, {
validateStatus: (status) => status === 200 || status === 404,
});
return (response.status === 200) ? response.data : {};
} catch (errorData) {
throw generateError(errorData);
}
Expand Down
47 changes: 2 additions & 45 deletions cvat/apps/iam/adapters.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,12 @@
# Copyright (C) 2022 CVAT.ai Corporation
# Copyright (C) 2022-2023 CVAT.ai Corporation
#
# SPDX-License-Identifier: MIT

from django.contrib.auth import get_user_model
from django.http import HttpResponseRedirect, HttpResponseBadRequest
from django.http import HttpResponseRedirect
from django.conf import settings

from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
from allauth.account.adapter import DefaultAccountAdapter
from allauth.socialaccount.providers.amazon_cognito.views import AmazonCognitoOAuth2Adapter
from allauth.socialaccount.providers.github.views import GitHubOAuth2Adapter
from allauth.socialaccount.providers.google.views import GoogleOAuth2Adapter
from allauth.exceptions import ImmediateHttpResponse
from allauth.account.utils import filter_users_by_email

UserModel = get_user_model()

class DefaultAccountAdapterEx(DefaultAccountAdapter):
def respond_email_verification_sent(self, request, user):
return HttpResponseRedirect(settings.ACCOUNT_EMAIL_VERIFICATION_SENT_REDIRECT_URL)

class SocialAccountAdapterEx(DefaultSocialAccountAdapter):
def pre_social_login(self, request, sociallogin):
"""
Invoked just after a user successfully authenticates via a
social provider, but before the login is actually processed
(and before the pre_social_login signal is emitted).
"""
if sociallogin.is_existing:
return

if not sociallogin.email_addresses:
raise ImmediateHttpResponse(response=HttpResponseBadRequest('No email is associated with this social account'))

users = filter_users_by_email(sociallogin.user.email)
if len(users) > 1:
raise ImmediateHttpResponse(HttpResponseBadRequest(f'Cannot connect account with ${sociallogin.user.email} email.'))
elif users:
sociallogin.connect(request, users[0])
return

class GitHubAdapter(GitHubOAuth2Adapter):

def get_callback_url(self, request, app):
return settings.GITHUB_CALLBACK_URL

class GoogleAdapter(GoogleOAuth2Adapter):

def get_callback_url(self, request, app):
return settings.GOOGLE_CALLBACK_URL

class AmazonCognitoOAuth2AdapterEx(AmazonCognitoOAuth2Adapter):
def get_callback_url(self, request, app):
return settings.AMAZON_COGNITO_REDIRECT_URI
29 changes: 2 additions & 27 deletions cvat/apps/iam/serializers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Copyright (C) 2022 Intel Corporation
# Copyright (C) 2022 CVAT.ai Corporation
# Copyright (C) 2022-2023 CVAT.ai Corporation
#
# SPDX-License-Identifier: MIT

from dj_rest_auth.registration.serializers import RegisterSerializer, SocialLoginSerializer
from dj_rest_auth.registration.serializers import RegisterSerializer
from dj_rest_auth.serializers import PasswordResetSerializer, LoginSerializer
from rest_framework.exceptions import ValidationError
from rest_framework import serializers
Expand Down Expand Up @@ -79,28 +79,3 @@ def is_username_authentication():
raise ValidationError('Unable to login with provided credentials')

return self._validate_username_email(username, email, password)


class SocialLoginSerializerEx(SocialLoginSerializer):
auth_params = serializers.CharField(required=False, allow_blank=True, default='')
process = serializers.CharField(required=False, allow_blank=True, default='login')
scope = serializers.CharField(required=False, allow_blank=True, default='')

def get_social_login(self, adapter, app, token, response):
request = self._get_request()
social_login = adapter.complete_login(request, app, token, response=response)
social_login.token = token

social_login.state = {
'process': self.initial_data.get('process'),
'scope': self.initial_data.get('scope'),
'auth_params': self.initial_data.get('auth_params'),
}

return social_login


class SocialAuthMethodSerializer(serializers.Serializer):
is_enabled = serializers.BooleanField(default=False)
icon = serializers.CharField(allow_blank=True, allow_null=True, required=False)
public_name = serializers.CharField()
26 changes: 3 additions & 23 deletions cvat/apps/iam/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (C) 2021-2022 Intel Corporation
# Copyright (C) 2022 CVAT.ai Corporation
# Copyright (C) 2022-2023 CVAT.ai Corporation
#
# SPDX-License-Identifier: MIT

Expand All @@ -12,14 +12,8 @@
from allauth.account import app_settings as allauth_settings

from cvat.apps.iam.views import (
SigningView, CognitoLogin, RegisterViewEx, RulesView,
ConfirmEmailViewEx, LoginViewEx, GitHubLogin, GoogleLogin, SocialAuthMethods,
github_oauth2_login as github_login,
github_oauth2_callback as github_callback,
google_oauth2_login as google_login,
google_oauth2_callback as google_callback,
amazon_cognito_oauth2_login as amazon_cognito_login,
amazon_cognito_oauth2_callback as amazon_cognito_callback,
SigningView, RegisterViewEx, RulesView,
ConfirmEmailViewEx, LoginViewEx
)

urlpatterns = [
Expand All @@ -39,7 +33,6 @@
name='rest_password_reset_confirm'),
path('password/change', PasswordChangeView.as_view(),
name='rest_password_change'),
path('social/methods/', SocialAuthMethods.as_view(), name='social_auth_methods'),
]
if allauth_settings.EMAIL_VERIFICATION != \
allauth_settings.EmailVerificationMethod.NONE:
Expand All @@ -48,18 +41,5 @@
re_path(r'^account-confirm-email/(?P<key>[-:\w]+)/$', ConfirmEmailViewEx.as_view(),
name='account_confirm_email'),
]
if settings.USE_ALLAUTH_SOCIAL_ACCOUNTS:
# social accounts
urlpatterns += [
path('github/login/', github_login, name='github_login'),
path('github/login/callback/', github_callback, name='github_callback'),
path('github/login/token', GitHubLogin.as_view()),
path('google/login/', google_login, name='google_login'),
path('google/login/callback/', google_callback, name='google_callback'),
path('google/login/token', GoogleLogin.as_view()),
path('amazon-cognito/login/', amazon_cognito_login, name='amazon_cognito_login'),
path('amazon-cognito/login/callback/', amazon_cognito_callback, name='amazon_cognito_callback'),
path('amazon-cognito/login/token', CognitoLogin.as_view()),
]

urlpatterns = [path('auth/', include(urlpatterns))]
Loading

0 comments on commit 3066c6e

Please sign in to comment.