Skip to content

Commit

Permalink
Token auth for non-REST API apps (cvat-ai#889)
Browse files Browse the repository at this point in the history
* Token authorization for non REST API apps (e.g. git, tf annotation, tf segmentation)
  • Loading branch information
nmanovic authored and Chris Lee-Messer committed Mar 5, 2020
1 parent 6247509 commit 21d190f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
42 changes: 17 additions & 25 deletions cvat-ui/src/utils/plugin-checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,34 @@ const core = getCore();
class PluginChecker {
public static async check(plugin: SupportedPlugins): Promise<boolean> {
const serverHost = core.config.backendAPI.slice(0, -7);
const isReachable = async (url: string): Promise<boolean> => {
try {
await core.server.request(url);
return true;
} catch (error) {
if (error.code === 404) {
return false;
}

throw error;
}
};

switch (plugin) {
case SupportedPlugins.GIT_INTEGRATION: {
const response = await fetch(`${serverHost}/git/repository/meta/get`);
if (response.ok) {
return true;
}
return false;
return isReachable(`${serverHost}/git/repository/meta/get`);
}
case SupportedPlugins.AUTO_ANNOTATION: {
const response = await fetch(`${serverHost}/auto_annotation/meta/get`);
if (response.ok) {
return true;
}
return false;
return isReachable(`${serverHost}/auto_annotation/meta/get`);
}
case SupportedPlugins.TF_ANNOTATION: {
const response = await fetch(`${serverHost}/tensorflow/annotation/meta/get`);
if (response.ok) {
return true;
}
return false;
return isReachable(`${serverHost}/tensorflow/annotation/meta/get`);
}
case SupportedPlugins.TF_SEGMENTATION: {
const response = await fetch(`${serverHost}/tensorflow/segmentation/meta/get`);
if (response.ok) {
return true;
}
return false;
return isReachable(`${serverHost}/tensorflow/segmentation/meta/get`);
}
case SupportedPlugins.ANALYTICS: {
const response = await fetch(`${serverHost}/analytics/app/kibana`);
if (response.ok) {
return true;
}
return false;
return isReachable(`${serverHost}/analytics/app/kibana`);
}
default:
return false;
Expand Down
7 changes: 7 additions & 0 deletions cvat/apps/authentication/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.http import JsonResponse
from django.contrib.auth.views import redirect_to_login
from django.conf import settings
from rest_framework.authentication import TokenAuthentication

def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME,
login_url=None, redirect_methods=['GET']):
Expand All @@ -19,6 +20,12 @@ def _wrapped_view(request, *args, **kwargs):
if request.user.is_authenticated:
return view_func(request, *args, **kwargs)
else:
tokenAuth = TokenAuthentication()
auth = tokenAuth.authenticate(request)
if auth is not None:
request.user = auth[0]
return view_func(request, *args, **kwargs)

if request.method not in redirect_methods:
return JsonResponse({'login_page_url': reverse('login')}, status=403)

Expand Down

0 comments on commit 21d190f

Please sign in to comment.