/
auth.py
1646 lines (1506 loc) · 59.6 KB
/
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright European Organization for Nuclear Research (CERN) since 2012
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import logging
import time
from typing import TYPE_CHECKING
from urllib.parse import urlparse
from flask import Blueprint, Flask, Response, redirect, render_template, request
from werkzeug.datastructures import Headers
from rucio.api.authentication import (
get_auth_oidc,
get_auth_token_gss,
get_auth_token_saml,
get_auth_token_ssh,
get_auth_token_user_pass,
get_auth_token_x509,
get_ssh_challenge_token,
get_token_oidc,
redirect_auth_oidc,
refresh_cli_auth_token,
validate_auth_token,
)
from rucio.common.config import config_get
from rucio.common.exception import AccessDenied, CannotAuthenticate, CannotAuthorize, IdentityError, IdentityNotFound
from rucio.common.extra import import_extras
from rucio.common.utils import date_to_str
from rucio.core.authentication import strip_x509_proxy_attributes
from rucio.web.rest.flaskapi.v1.common import ErrorHandlingMethodView, check_accept_header_wrapper_flask, error_headers, extract_vo, generate_http_error_flask, get_account_from_verified_identity
if TYPE_CHECKING:
from typing import Optional, Union
from rucio.web.rest.flaskapi.v1.common import HeadersType
EXTRA_MODULES = import_extras(['onelogin'])
if EXTRA_MODULES['onelogin']:
from onelogin.saml2.auth import OneLogin_Saml2_Auth # pylint: disable=import-error
from rucio.web.ui.flask.common.utils import prepare_saml_request
class UserPass(ErrorHandlingMethodView):
"""
Authenticate a Rucio account temporarily via username and password.
"""
def get_headers(self) -> "Optional[HeadersType]":
headers = Headers()
headers['Access-Control-Allow-Origin'] = request.environ.get('HTTP_ORIGIN')
headers['Access-Control-Allow-Headers'] = request.environ.get('HTTP_ACCESS_CONTROL_REQUEST_HEADERS')
headers['Access-Control-Allow-Methods'] = '*'
headers['Access-Control-Allow-Credentials'] = 'true'
headers['Access-Control-Expose-Headers'] = 'X-Rucio-Auth-Token, X-Rucio-Auth-Token-Expires, X-Rucio-Auth-Account, X-Rucio-Auth-Accounts'
return headers
def options(self) -> tuple[str, int, "Optional[HeadersType]"]:
"""
---
summary: UserPass Allow cross-site scripting
description: UserPass Allow cross-site scripting. Explicit for Authentication.
tags:
- Auth
responses:
200:
description: OK
headers:
Access-Control-Allow-Origin:
schema:
type: string
Access-Control-Allow-Headers:
schema:
type: string
Access-Control-Allow-Methods:
schema:
type: string
enum: ['*']
Access-Control-Allow-Credentials:
schema:
type: string
enum: ['true']
Access-Control-Expose-Headers:
schema:
type: string
enum: ['X-Rucio-Auth-Token']
404:
description: Not found
"""
return '', 200, self.get_headers()
@check_accept_header_wrapper_flask(['application/octet-stream'])
def get(self) -> 'Union[Response, tuple[str, int, "Optional[HeadersType]"]]':
"""
---
summary: UserPass
description: Authenticate a Rucio account temporarily via username and password.
tags:
- Auth
parameters:
- name: X-Rucio-Account
in: header
description: Account identifier as a string.
schema:
type: string
required: true
- name: X-Rucio-Username
in: header
description: Username as a string.
schema:
type: string
required: true
- name: X-Rucio-Password
in: header
description: password as a text-plain string.
schema:
type: string
required: true
- name: X-Rucio-AppID
in: header
description: Application identifier as a string.
schema:
type: string
- name: X-Forwarded-For
in: header
description: The forward ip address.
schema:
type: string
responses:
200:
description: OK
headers:
Access-Control-Allow-Origin:
schema:
type: string
Access-Control-Allow-Headers:
schema:
type: string
Access-Control-Allow-Methods:
schema:
type: string
Access-Control-Allow-Credentials:
schema:
type: string
Access-Control-Expose-Headers:
schema:
type: string
X-Rucio-Auth-Token:
schema:
type: string
X-Rucio-Auth-Token-Expires:
schema:
type: string
X-Rucio-Auth-Account:
schema:
type: string
description: The rucio account used for authentication
206:
description: Partial content containing X-Rucio-Auth-Accounts header
headers:
X-Rucio-Auth-Accounts:
schema:
type: string
description: The rucio accounts corresponding to the provided identity as a csv string
401:
description: Cannot authenticate
"""
headers = self.get_headers()
headers['Content-Type'] = 'application/octet-stream'
headers['Cache-Control'] = 'no-cache, no-store, max-age=0, must-revalidate'
headers.add('Cache-Control', 'post-check=0, pre-check=0')
headers['Pragma'] = 'no-cache'
vo = extract_vo(request.headers)
account = request.headers.get('X-Rucio-Account', default=None)
username = request.headers.get('X-Rucio-Username', default=None)
password = request.headers.get('X-Rucio-Password', default=None)
appid = request.headers.get('X-Rucio-AppID', default='unknown')
ip = request.headers.get('X-Forwarded-For', default=request.remote_addr)
if not username or not password:
return generate_http_error_flask(401, CannotAuthenticate.__name__, 'Cannot authenticate without passing all required arguments', headers=headers)
accounts: list[str] = []
if not account:
try:
accounts = get_account_from_verified_identity(identity_key=username, id_type='USERPASS', password=password)
except IdentityNotFound:
return generate_http_error_flask(401, IdentityNotFound.__name__, 'Cannot authenticate. Username/Password pair does not exist.', headers=headers)
except IdentityError:
return generate_http_error_flask(401, IdentityError.__name__, 'Cannot authenticate. The identity does not exist.', headers=headers)
else:
accounts = [account]
if len(accounts) > 1:
account_names: list[str] = []
for account in accounts:
if isinstance(account, str):
account_names.append(account)
else:
account_names.append(account.external)
headers['X-Rucio-Auth-Accounts'] = ','.join(accounts)
return json.dumps(account_names), 206, headers
account = accounts[0]
account_name = account if isinstance(account, str) else account.external
try:
result = get_auth_token_user_pass(account_name, username, password, appid, ip, vo=vo)
if not result:
return generate_http_error_flask(401, CannotAuthenticate.__name__, f'Cannot authenticate to account {account} with given credentials', headers=headers)
headers['X-Rucio-Auth-Account'] = account_name
headers['X-Rucio-Auth-Token'] = result['token']
headers['X-Rucio-Auth-Token-Expires'] = date_to_str(result['expires_at'])
return '', 200, headers
except AccessDenied:
return generate_http_error_flask(401, CannotAuthenticate.__name__, f'Cannot authenticate to account {account} with given credentials', headers=headers)
class OIDC(ErrorHandlingMethodView):
"""
Requests a user specific Authorization URL (assigning a user session state,
nonce, Rucio OIDC Client ID with the correct issuers authentication endpoint).
"""
def get_headers(self) -> "Optional[HeadersType]":
headers = Headers()
headers['Access-Control-Allow-Origin'] = request.environ.get('HTTP_ORIGIN')
headers['Access-Control-Allow-Headers'] = request.environ.get('HTTP_ACCESS_CONTROL_REQUEST_HEADERS')
headers['Access-Control-Allow-Methods'] = '*'
headers['Access-Control-Allow-Credentials'] = 'true'
return headers
def options(self):
"""
---
summary: OIDC Allow cross-site scripting
description: OIDC Allow cross-site scripting. Explicit for Authentication.
tags:
- Auth
responses:
200:
description: OK
headers:
Access-Control-Allow-Origin:
schema:
type: string
Access-Control-Allow-Headers:
schema:
type: string
Access-Control-Allow-Methods:
schema:
type: string
enum: ['*']
Access-Control-Allow-Credentials:
schema:
type: string
enum: ['true']
404:
description: Not found
"""
return '', 200, self.get_headers()
@check_accept_header_wrapper_flask(['application/octet-stream'])
def get(self):
"""
---
summary: OIDC
description: Authenticate a Rucio account via OIDC.
tags:
- Auth
parameters:
- name: HTTP_X_RUCIO_ACCOUNT
in: header
description: Account identifier as a string.
schema:
type: string
- name: HTTP_X_RUCIO_CLIENT_AUTHORIZE_SCOPE
in: header
schema:
type: string
- name: HTTP_X_RUCIO_CLIENT_AUTHORIZE_AUDIENCE
in: header
schema:
type: string
- name: HTTP_X_RUCIO_CLIENT_AUTHORIZE_AUTO
in: header
schema:
type: string
- name: HTTP_X_RUCIO_CLIENT_AUTHORIZE_ISSUER
in: header
schema:
type: string
- name: HTTP_X_RUCIO_CLIENT_AUTHORIZE_POLLING
in: header
schema:
type: string
- name: HTTP_X_RUCIO_CLIENT_AUTHORIZE_REFRESH_LIFETIME
in: header
schema:
type: string
- name: X-Forwarded-For
in: header
schema:
type: string
responses:
200:
description: OK
headers:
X-Rucio-OIDC-Auth-URL:
description: User & Rucio OIDC Client specific Authorization URL
schema:
type: string
401:
description: Cannot authenticate
"""
headers = self.get_headers()
headers.set('Content-Type', 'application/octet-stream')
headers.set('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate')
headers.add('Cache-Control', 'post-check=0, pre-check=0')
headers.set('Pragma', 'no-cache')
vo = extract_vo(request.headers)
account = request.environ.get('HTTP_X_RUCIO_ACCOUNT', 'webui')
auth_scope = request.environ.get('HTTP_X_RUCIO_CLIENT_AUTHORIZE_SCOPE', "")
audience = request.environ.get('HTTP_X_RUCIO_CLIENT_AUTHORIZE_AUDIENCE', "")
auto = request.environ.get('HTTP_X_RUCIO_CLIENT_AUTHORIZE_AUTO', False)
issuer = request.environ.get('HTTP_X_RUCIO_CLIENT_AUTHORIZE_ISSUER', None)
polling = request.environ.get('HTTP_X_RUCIO_CLIENT_AUTHORIZE_POLLING', False)
refresh_lifetime = request.environ.get('HTTP_X_RUCIO_CLIENT_AUTHORIZE_REFRESH_LIFETIME', None)
auto = (auto == 'True' or auto == 'true')
polling = (polling == 'True' or polling == 'true')
if refresh_lifetime == 'None':
refresh_lifetime = None
ip = request.headers.get('X-Forwarded-For', default=request.remote_addr)
try:
kwargs = {'auth_scope': auth_scope,
'audience': audience,
'issuer': issuer,
'auto': auto,
'polling': polling,
'refresh_lifetime': refresh_lifetime,
'ip': ip}
result = get_auth_oidc(account, vo=vo, **kwargs)
except AccessDenied:
return generate_http_error_flask(
status_code=401,
exc=CannotAuthenticate.__name__,
exc_msg=f'Cannot get authentication URL from Rucio Authentication Server for account {account}',
headers=headers
)
if not result:
return generate_http_error_flask(
status_code=401,
exc=CannotAuthenticate.__name__,
exc_msg=f'Cannot get authentication URL from Rucio Authentication Server for account {account}',
headers=headers
)
headers.set('X-Rucio-OIDC-Auth-URL', result)
return '', 200, headers
class RedirectOIDC(ErrorHandlingMethodView):
"""
Authenticate a Rucio account via
an Identity Provider (XDC IAM as of June 2019).
"""
def get_headers(self) -> "Optional[HeadersType]":
headers = Headers()
headers.set('Access-Control-Allow-Origin', request.environ.get('HTTP_ORIGIN'))
headers.set('Access-Control-Allow-Headers', request.environ.get('HTTP_ACCESS_CONTROL_REQUEST_HEADERS'))
headers.set('Access-Control-Allow-Methods', '*')
headers.set('Access-Control-Allow-Credentials', 'true')
return headers
def options(self):
"""
---
summary: RedirectOIDC Allow cross-site scripting
description: RedirectOIDC Allow cross-site scripting. Explicit for Authentication.
tags:
- Auth
responses:
200:
description: OK
headers:
Access-Control-Allow-Origin:
schema:
type: string
Access-Control-Allow-Headers:
schema:
type: string
Access-Control-Allow-Methods:
schema:
type: string
enum: ['*']
Access-Control-Allow-Credentials:
schema:
type: string
enum: ['true']
404:
description: Not found
"""
return '', 200, self.get_headers()
@check_accept_header_wrapper_flask(['application/octet-stream', 'text/html'])
def get(self):
"""
---
summary: RedirectOIDC
description: Authenticate a Rucio account via RedirectOIDC.
tags:
- Auth
parameters:
- name: X-Rucio-Client-Fetch-Token
in: header
schema:
type: string
responses:
200:
description: OK
headers:
X-Rucio-Auth-Token:
description: The authentication token
schema:
type: string
Content-Type:
schema:
type: string
enum: ['application/octet-stream']
303:
description: Redirect
401:
description: Cannot authenticate
"""
headers = self.get_headers()
# interaction with web browser - display response in html format
headers.set('Content-Type', 'text/html')
headers.set('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate')
headers.add('Cache-Control', 'post-check=0, pre-check=0')
headers.set('Pragma', 'no-cache')
try:
fetchtoken = (request.headers.get('X-Rucio-Client-Fetch-Token', default=None) == 'True')
query_string = request.query_string.decode(encoding='utf-8')
result = redirect_auth_oidc(query_string, fetchtoken)
except AccessDenied:
headers.extend(error_headers(CannotAuthenticate.__name__, 'Cannot authorize your access, please check your access credentials'))
return render_template('auth_crash.html', crashtype='contact'), 401, headers
except Exception as error:
logging.exception("Internal Error")
headers.extend(error_headers(error.__class__.__name__, str(error.args[0])))
return render_template('auth_crash.html', crashtype='internal_error'), 500, headers
if not result:
headers.extend(error_headers(CannotAuthenticate.__name__, 'Cannot finalize your token request, no authorization content returned from the auth server'))
return render_template('auth_crash.html', crashtype='no_result'), 401, headers
if fetchtoken:
# this is only a case of returning the final token to the Rucio Client polling
# or requesting token after copy-pasting the Rucio code from the web page page
headers.set('Content-Type', 'application/octet-stream')
headers.set('X-Rucio-Auth-Token', result)
return '', 200, headers
else:
response = redirect(result, code=303)
response.headers.extend(headers)
return response
class CodeOIDC(ErrorHandlingMethodView):
"""
IdP redirects to this endpoint with the AuthZ code
Rucio Auth server will request new token. This endpoint should be reached
only if the request/ IdP login has been made through web browser. Then the response
content will be in html (including the potential errors displayed).
The token will be saved in the Rucio DB, but only Rucio code will
be returned on the web page, or, in case of polling is True, successful
operation is confirmed waiting for the Rucio client to get the token automatically.
"""
def get_headers(self) -> "Optional[HeadersType]":
headers = Headers()
headers.set('Access-Control-Allow-Origin', request.environ.get('HTTP_ORIGIN'))
headers.set('Access-Control-Allow-Headers', request.environ.get('HTTP_ACCESS_CONTROL_REQUEST_HEADERS'))
headers.set('Access-Control-Allow-Methods', '*')
headers.set('Access-Control-Allow-Credentials', 'true')
return headers
def options(self):
"""
---
summary: CodeOIDC Allow cross-site scripting
description: CodeOIDC Allow cross-site scripting. Explicit for Authentication.
tags:
- Auth
responses:
200:
description: OK
headers:
Access-Control-Allow-Origin:
schema:
type: string
Access-Control-Allow-Headers:
schema:
type: string
Access-Control-Allow-Methods:
schema:
type: string
enum: ['*']
Access-Control-Allow-Credentials:
schema:
type: string
enum: ['true']
404:
description: Not found
"""
return '', 200, self.get_headers()
@check_accept_header_wrapper_flask(['application/octet-stream', 'text/html'])
def get(self):
"""
---
summary: CodeOIDC
description: Authenticate a Rucio account via CodeOIDC.
tags:
- Auth
parameters:
- name: X-Forwarded-For
in: header
schema:
type: string
responses:
200:
description: OK
400:
description: Invalid request
"""
headers = self.get_headers()
headers.set('Content-Type', 'text/html')
headers.set('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate')
headers.add('Cache-Control', 'post-check=0, pre-check=0')
headers.set('Pragma', 'no-cache')
query_string = request.query_string.decode(encoding='utf-8')
ip = request.headers.get('X-Forwarded-For', default=request.remote_addr)
try:
result = get_token_oidc(query_string, ip)
except AccessDenied:
headers.extend(error_headers(CannotAuthenticate.__name__, 'Cannot authorize your access, please check your access credentials'))
return render_template('auth_crash.html', crashtype='contact'), 401, headers
except Exception as error:
logging.exception("Internal Error")
headers.extend(error_headers(error.__class__.__name__, str(error.args[0])))
return render_template('auth_crash.html', crashtype='internal_error'), 500, headers
if not result:
headers.extend(error_headers(CannotAuthenticate.__name__, 'Cannot finalize your token request, no authorization content returned from the auth server'))
return render_template('auth_crash.html', crashtype='no_result'), 401, headers
if 'fetchcode' in result:
return render_template('auth_granted.html', authcode=result['fetchcode']), 200, headers
elif 'polling' in result and result['polling'] is True:
return render_template('auth_granted.html', authcode='allok'), 200, headers
else:
headers.extend(error_headers('InvalidRequest', 'Cannot recognize and process your request'))
return render_template('auth_crash.html', crashtype='bad_request'), 400, headers
class TokenOIDC(ErrorHandlingMethodView):
"""
Authenticate a Rucio account temporarily via ID,
access (eventually save new refresh token)
received from an Identity Provider.
"""
def get_headers(self) -> "Optional[HeadersType]":
headers = Headers()
headers.set('Access-Control-Allow-Origin', request.environ.get('HTTP_ORIGIN'))
headers.set('Access-Control-Allow-Headers', request.environ.get('HTTP_ACCESS_CONTROL_REQUEST_HEADERS'))
headers.set('Access-Control-Allow-Methods', '*')
headers.set('Access-Control-Allow-Credentials', 'true')
return headers
def options(self):
"""
---
summary: TokenOIDC Allow cross-site scripting
description: TokenOIDC Allow cross-site scripting. Explicit for Authentication.
tags:
- Auth
responses:
200:
description: OK
headers:
Access-Control-Allow-Origin:
schema:
type: string
Access-Control-Allow-Headers:
schema:
type: string
Access-Control-Allow-Methods:
schema:
type: string
enum: ['*']
Access-Control-Allow-Credentials:
schema:
type: string
enum: ['true']
404:
description: Not found
"""
return '', 200, self.get_headers()
@check_accept_header_wrapper_flask(['application/octet-stream'])
def get(self):
"""
---
summary: TokenOIDC
description: Authenticate a Rucio account via TokenOIDC.
tags:
- Auth
parameters:
- name: X-Forwarded-For
in: header
schema:
type: string
responses:
200:
description: OK
headers:
X-Rucio-Auth-Token:
description: The authentication token
schema:
type: string
X-Rucio-Auth-Token-Expires:
description: The time when the token expires
schema:
type: string
401:
description: Cannot authenticate
"""
headers = self.get_headers()
headers.set('Content-Type', 'application/octet-stream')
headers.set('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate')
headers.add('Cache-Control', 'post-check=0, pre-check=0')
headers.set('Pragma', 'no-cache')
query_string = request.query_string.decode(encoding='utf-8')
ip = request.headers.get('X-Forwarded-For', default=request.remote_addr)
try:
result = get_token_oidc(query_string, ip)
except AccessDenied:
return generate_http_error_flask(401, CannotAuthorize.__name__, 'Cannot authorize token request.', headers=headers)
if not result:
return generate_http_error_flask(401, CannotAuthorize.__name__, 'Cannot authorize token request.', headers=headers)
if 'token' in result and 'webhome' not in result:
headers.set('X-Rucio-Auth-Token', result['token']['token'])
headers.set('X-Rucio-Auth-Token-Expires', date_to_str(result['token']['expires_at']))
return '', 200, headers
elif 'webhome' in result:
webhome = result['webhome']
if webhome is None:
headers.extend(error_headers(CannotAuthenticate.__name__, 'Cannot find your OIDC identity linked to any Rucio account'))
headers.set('Content-Type', 'text/html')
return render_template('auth_crash.html', crashtype='unknown_identity'), 401, headers
# domain setting is necessary so that the token gets distributed also to the webui server
domain = '.'.join(urlparse(webhome).netloc.split('.')[1:])
response = redirect(webhome, code=303)
response.headers.extend(headers)
response.set_cookie('x-rucio-auth-token', value=result['token']['token'], domain=domain, path='/')
response.set_cookie('rucio-auth-token-created-at', value=str(time.time()), domain=domain, path='/')
# response.set_cookie('x-rucio-auth-token', value=result['token']['token'])
# response.set_cookie('rucio-auth-token-created-at', value=str(time.time()))
return response
else:
return '', 400, headers
class RefreshOIDC(ErrorHandlingMethodView):
"""
For a presented and access token which has equivalent in Rucio DB
(and also has refrech token in the Rucio DB) the class will attempt
token refresh and return a user a new refreshed token. If the presented token
is a result of a previous refresh happening in the last 10 min, the same token will be returned.
"""
def get_headers(self) -> "Optional[HeadersType]":
headers = Headers()
headers.set('Access-Control-Allow-Origin', request.environ.get('HTTP_ORIGIN'))
headers.set('Access-Control-Allow-Headers', request.environ.get('HTTP_ACCESS_CONTROL_REQUEST_HEADERS'))
headers.set('Access-Control-Allow-Methods', '*')
headers.set('Access-Control-Allow-Credentials', 'true')
headers.set('Access-Control-Expose-Headers', 'X-Rucio-Auth-Token')
return headers
def options(self):
"""
---
summary: RefreshOIDC Allow cross-site scripting
description: RefreshOIDC Allow cross-site scripting. Explicit for Authentication.
tags:
- Auth
responses:
200:
description: OK
headers:
Access-Control-Allow-Origin:
schema:
type: string
Access-Control-Allow-Headers:
schema:
type: string
Access-Control-Allow-Methods:
schema:
type: string
enum: ['*']
Access-Control-Allow-Credentials:
schema:
type: string
enum: ['true']
Access-Control-Expose-Headers:
schema:
type: string
enum: ['X-Rucio-Auth-Token']
404:
description: Not found
"""
return '', 200, self.get_headers()
@check_accept_header_wrapper_flask(['application/octet-stream'])
def get(self):
"""
---
summary: RefreshOIDC
description: Authenticate a Rucio account via RefreshOIDC.
tags:
- Auth
parameters:
- name: X-Rucio-Account
in: header
schema:
type: string
required: true
- name: X-Rucio-Auth-Token
in: header
schema:
type: string
required: true
responses:
200:
description: OK
headers:
X-Rucio-Auth-Token:
description: The authentication token
schema:
type: string
X-Rucio-Auth-Token-Expires:
description: The time when the token expires
schema:
type: string
401:
description: Cannot authenticate
"""
headers = self.get_headers()
headers.set('Content-Type', 'application/octet-stream')
headers.set('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate')
headers.add('Cache-Control', 'post-check=0, pre-check=0')
headers.set('Pragma', 'no-cache')
vo = extract_vo(request.headers)
account = request.headers.get('X-Rucio-Account', default=None)
token = request.headers.get('X-Rucio-Auth-Token', default=None)
if token is None or account is None:
return generate_http_error_flask(401, CannotAuthorize.__name__, 'Cannot authorize token request.', headers=headers)
try:
result = refresh_cli_auth_token(token, account, vo=vo)
except AccessDenied:
return generate_http_error_flask(401, CannotAuthorize.__name__, 'Cannot authorize token request.', headers=headers)
if result is not None and len(result) > 1:
headers.set('X-Rucio-Auth-Token', str(result[0]))
headers.set('X-Rucio-Auth-Token-Expires', str(result[1]))
else:
headers.set('X-Rucio-Auth-Token', '')
headers.set('X-Rucio-Auth-Token-Expires', '')
return '', 200, headers
class GSS(ErrorHandlingMethodView):
"""
Authenticate a Rucio account temporarily via a GSS token.
"""
def get_headers(self) -> "Optional[HeadersType]":
headers = Headers()
headers['Access-Control-Allow-Origin'] = request.environ.get('HTTP_ORIGIN')
headers['Access-Control-Allow-Headers'] = request.environ.get('HTTP_ACCESS_CONTROL_REQUEST_HEADERS')
headers['Access-Control-Allow-Methods'] = '*'
headers['Access-Control-Allow-Credentials'] = 'true'
headers['Access-Control-Expose-Headers'] = 'X-Rucio-Auth-Token'
return headers
def options(self):
"""
---
summary: GSS Allow cross-site scripting
description: GSS Allow cross-site scripting. Explicit for Authentication.
tags:
- Auth
responses:
200:
description: OK
headers:
Access-Control-Allow-Origin:
schema:
type: string
Access-Control-Allow-Headers:
schema:
type: string
Access-Control-Allow-Methods:
schema:
type: string
enum: ['*']
Access-Control-Allow-Credentials:
schema:
type: string
enum: ['true']
Access-Control-Expose-Headers:
schema:
type: string
enum: ['X-Rucio-Auth-Token']
404:
description: Not found
"""
return '', 200, self.get_headers()
@check_accept_header_wrapper_flask(['application/octet-stream'])
def get(self):
"""
---
summary: GSS
description: Authenticate a Rucio account via GSS.
tags:
- Auth
parameters:
- name: X-Rucio-Account
in: header
schema:
type: string
required: true
- name: REMOTE_USER
in: header
schema:
type: string
required: true
- name: X-Rucio-AppID
in: header
schema:
type: string
- name: X-Forwarded-For
in: header
schema:
type: string
responses:
200:
description: OK
headers:
X-Rucio-Auth-Token:
description: The authentication token
schema:
type: string
X-Rucio-Auth-Token-Expires:
description: The time when the token expires
schema:
type: string
401:
description: Cannot authenticate
"""
headers = self.get_headers()
headers['Content-Type'] = 'application/octet-stream'
headers['Cache-Control'] = 'no-cache, no-store, max-age=0, must-revalidate'
headers.add('Cache-Control', 'post-check=0, pre-check=0')
headers['Pragma'] = 'no-cache'
vo = extract_vo(request.headers)
account = request.headers.get('X-Rucio-Account', default=None)
gsscred = request.environ.get('REMOTE_USER')
appid = request.headers.get('X-Rucio-AppID', default='unknown')
ip = request.headers.get('X-Forwarded-For', default=request.remote_addr)
try:
result = get_auth_token_gss(account, gsscred, appid, ip, vo=vo)
except AccessDenied:
return generate_http_error_flask(
status_code=401,
exc=CannotAuthenticate.__name__,
exc_msg=f'Cannot authenticate to account {account} with given credentials',
headers=headers
)
if result is None:
return generate_http_error_flask(
status_code=401,
exc=CannotAuthenticate.__name__,
exc_msg=f'Cannot authenticate to account {account} with given credentials',
headers=headers
)
headers['X-Rucio-Auth-Token'] = result['token']
headers['X-Rucio-Auth-Token-Expires'] = date_to_str(result['expires_at'])
return '', 200, headers
class x509(ErrorHandlingMethodView):
"""
Authenticate a Rucio account temporarily via an x509 certificate.
"""
def get_headers(self) -> "Optional[HeadersType]":
headers = Headers()
headers['Access-Control-Allow-Origin'] = request.environ.get('HTTP_ORIGIN')
headers['Access-Control-Allow-Headers'] = request.environ.get('HTTP_ACCESS_CONTROL_REQUEST_HEADERS')
headers['Access-Control-Allow-Methods'] = '*'
headers['Access-Control-Allow-Credentials'] = 'true'
headers['Access-Control-Expose-Headers'] = 'X-Rucio-Auth-Token, X-Rucio-Auth-Token-Expires, X-Rucio-Auth-Account, X-Rucio-Auth-Accounts'
return headers
def options(self) -> tuple[str, int, "Optional[HeadersType]"]:
"""
---
summary: x509 Allow cross-site scripting
description: x509 Allow cross-site scripting. Explicit for Authentication.
tags:
- Auth
responses:
200:
description: OK
headers:
Access-Control-Allow-Origin:
schema:
type: string
Access-Control-Allow-Headers:
schema:
type: string
Access-Control-Allow-Methods:
schema:
type: string
enum: ['*']
Access-Control-Allow-Credentials:
schema:
type: string
enum: ['true']
Access-Control-Expose-Headers:
schema:
type: string
enum: ['X-Rucio-Auth-Token']
404:
description: Not found
"""
return '', 200, self.get_headers()
@check_accept_header_wrapper_flask(['application/octet-stream'])
def get(self) -> 'Union[Response, tuple[str, int, "Optional[HeadersType]"]]':
"""
---
summary: x509
description: Authenticate a Rucio account via x509.
tags:
- Auth
parameters: