-
-
Notifications
You must be signed in to change notification settings - Fork 266
/
__init__.py
139 lines (121 loc) · 4.38 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# -*- coding: utf8 -*-
# This file is part of PYBOSSA.
#
# Copyright (C) 2018 Scifabric LTD.
#
# PYBOSSA is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PYBOSSA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PYBOSSA. If not, see <http://www.gnu.org/licenses/>.
import inspect
from flask import abort
from flask_login import current_user
from pybossa.core import announcement_repo, task_repo, project_repo, result_repo
from pybossa.core import project_stats_repo
from pybossa.auth.errcodes import *
import jwt
from flask import jsonify
from jwt import exceptions
import project
import projectstats
import task
import taskrun
import category
import user
import token
import announcement
import blogpost
import auditlog
import webhook
import result
import helpingmaterial
assert project
assert projectstats
assert task
assert taskrun
assert category
assert user
assert token
assert announcement
assert blogpost
assert auditlog
assert webhook
assert result
_actions = ['create', 'read', 'update', 'delete']
_auth_classes = {'project': project.ProjectAuth,
'projectstats': projectstats.ProjectStatsAuth,
'auditlog': auditlog.AuditlogAuth,
'announcement': announcement.AnnouncementAuth,
'blogpost': blogpost.BlogpostAuth,
'category': category.CategoryAuth,
'task': task.TaskAuth,
'taskrun': taskrun.TaskRunAuth,
'token': token.TokenAuth,
'user': user.UserAuth,
'webhook': webhook.WebhookAuth,
'result': result.ResultAuth,
'helpingmaterial': helpingmaterial.HelpingMaterialAuth}
def is_authorized(user, action, resource, **kwargs):
is_class = inspect.isclass(resource)
name = resource.__name__ if is_class else resource.__class__.__name__
if resource == 'token':
name = resource
resource = None if is_class else resource
auth = _authorizer_for(name.lower())
actions = _actions + auth.specific_actions
assert action in actions, "%s is not a valid action" % action
return auth.can(user, action, resource, **kwargs)
def ensure_authorized_to(action, resource, **kwargs):
authorized = is_authorized(current_user, action, resource, **kwargs)
if authorized is False:
if current_user.is_anonymous():
raise abort(401)
else:
raise abort(403)
return authorized
def _authorizer_for(resource_name):
kwargs = {}
if resource_name in ('project', 'taskrun'):
kwargs.update({'task_repo': task_repo})
if resource_name in ('auditlog', 'blogpost', 'task',
'taskrun', 'webhook', 'result',
'helpingmaterial'):
kwargs.update({'project_repo': project_repo})
if resource_name in ('project', 'task', 'taskrun'):
kwargs.update({'result_repo': result_repo})
return _auth_classes[resource_name](**kwargs)
def handle_error(error):
"""Return authentication error in JSON."""
resp = jsonify(error)
resp.status_code = 401
return resp
def jwt_authorize_project(project, payload):
"""Authorize the project for the payload."""
try:
if payload is None:
return handle_error(INVALID_HEADER_MISSING)
parts = payload.split()
if parts[0].lower() != 'bearer':
return handle_error(INVALID_HEADER_BEARER)
elif len(parts) == 1:
return handle_error(INVALID_HEADER_TOKEN)
elif len(parts) > 2:
return handle_error(INVALID_HEADER_BEARER_TOKEN)
data = jwt.decode(parts[1],
project.secret_key,
'H256')
if (data['project_id'] == project.id
and data['short_name'] == project.short_name):
return True
else:
return handle_error(WRONG_PROJECT_SIGNATURE)
except exceptions.DecodeError:
return handle_error(DECODE_ERROR_SIGNATURE)