-
Notifications
You must be signed in to change notification settings - Fork 0
/
old_auth.py
195 lines (148 loc) · 6.18 KB
/
old_auth.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#© 2020 By The Rector And Visitors Of The University Of Virginia
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.import requests
import requests,jwt
import flask
import json
import os
from functools import wraps
AUTH_SERVICE = os.environ.get("AUTH_SERVICE", "http://clarklab.uvarc.io/auth")
KEY = os.environ.get('AUTH_KEY')
ISSUER = "ors:transfer"
def check_token(handler):
'''
Function Wrapper for all endpoints that checks that an Authorization is present in request headers.
If not the wrapper will return an error.
Used for API service calls where a Globus Token is required.
'''
@wraps(handler)
def wrapped_handler(*args, **kwargs):
if os.environ.get("NO_AUTH",False):
return handler(*args, **kwargs)
if flask.request.headers.get("Authorization") is None:
return flask.Response(
response= json.dumps({"error": "Request Missing Authorization Header"}),
status=403,
content_type="application/json"
)
encoded_token = flask.request.headers.get("Authorization")
json_token = jwt.decode(encoded_token, KEY, algorithms='HS256',,audience = 'https://fairscape.org')
if json_token.get('role',None) == 'admin' or json_token.get('role',None) == 'user':
return handler(*args, **kwargs)
elif allowed_user(json_token):
return handler(*args, **kwargs)
else:
return flask.Response(
response=json.dumps({"error": "failed to authorize user"}),
status=401,
content_type="application/json"
)
return wrapped_handler
def allowed_user(json):
if json_token.get('role',None) == 'user':
return True
return False
def token_required(handler):
'''
Function Wrapper for all endpoints that checks that an Authorization is present in request headers.
If not the wrapper will return an error.
Used for API service calls where a Globus Token is required.
'''
@wraps(handler)
def wrapped_handler(*args, **kwargs):
if os.environ.get("NO_AUTH",False):
return handler(*args, **kwargs)
print('cookies are: ')
print(flask.request.cookies)
token = flask.request.cookies.get("fairscapeAuth")
token_response = requests.post(
url = AUTH_SERVICE + "/inspect",
headers = {"Authorization": token}
)
print(token_response.content.decode())
if token_response.status_code == 204:
return handler(*args, **kwargs)
else:
return flask.Response(
response=json.dumps({"error": "failed to authorize user"}),
status=401,
content_type="application/json"
)
return wrapped_handler
def token_redirect(handler):
'''
Function Wrapper for all endpoints that checks for an Authorization token in request headers, if not
the wrapper will redirect the user to login.
Used for frontend views where a user must be logged in to use some part of the page.
i.e. deleting a identifier from landing page interface
'''
@wraps(handler)
def wrapped_handler(*args, **kwargs):
if flask.request.headers.get("Authorization") is not None:
return handler(*args, **kwargs)
else:
return flask.redirect(AUTH_SERVICE + "login")
return wrapped_handler
def check_permission(user_token, resource, action):
'''
Issues a permissions challenge to the token for the request
'''
challenge_body = {
"principal": user_token,
"resource": resource,
"action": action,
"issuer": ISSUER
}
challenge_response = requests.post(
AUTH_SERVICE + "challenge",
data=json.dumps(challenge_body)
)
if challenge_response.status_code == 200:
return True
else:
return False
def register_resource(user_token, resource):
'''
Post a record of a created object in the Auth service
'''
resp = requests.post(
url = AUTH_SERVICE + "resource",
data = json.dumps({
"@id": resource,
"owner": user_token
})
)
if resp.status_code == 200:
return True
else:
return False
def delete_resource(user_token, resource):
resp = requests.delete(
url = AUTH_SERVICE + "resource/" + resource,
headers = {"Authorization": f"Bearer {user_token}"}
)
if resp.statuse_code != 200:
return False
return True
def create_policy(user_token, resource, principal, action, allow):
'''
Used to change set permissions on objects from this service at the centrilized auth service
'''
policy_body = {
"resouce": resource,
"principal": principal,
"action": action,
"allow": allow,
"issuer": ISSUER
}
policy_response = requests.post(
url = AUTH_SERVICE + "policy",
data=json.dumps(policy_body),
headers = {"Authorization": f"Bearer {user_token}"}
)
if policy_response.status_code == 200:
return True
# FIXME: handle different errors and return
else:
return False