|
1 | 1 | import datetime |
2 | 2 | import logging |
3 | 3 | import re |
4 | | - |
5 | | -from flask import abort, current_app, flash, g, redirect, request, session, url_for |
| 4 | +from typing import Optional |
| 5 | +from urllib.parse import urlparse |
| 6 | + |
| 7 | +from flask import ( |
| 8 | + abort, |
| 9 | + current_app, |
| 10 | + flash, |
| 11 | + g, |
| 12 | + redirect, |
| 13 | + request, |
| 14 | + Response, |
| 15 | + session, |
| 16 | + url_for, |
| 17 | +) |
6 | 18 | from flask_babel import lazy_gettext |
7 | 19 | from flask_login import login_user, logout_user |
8 | 20 | import jwt |
@@ -537,53 +549,6 @@ def login(self): |
537 | 549 | self.login_template, title=self.title, form=form, appbuilder=self.appbuilder |
538 | 550 | ) |
539 | 551 |
|
540 | | - """ |
541 | | - For Future Use, API Auth, must check howto keep REST stateless |
542 | | - """ |
543 | | - |
544 | | - """ |
545 | | - @expose_api(name='auth',url='/api/auth') |
546 | | - def auth(self): |
547 | | - if g.user is not None and g.user.is_authenticated: |
548 | | - http_return_code = 401 |
549 | | - response = make_response( |
550 | | - jsonify( |
551 | | - { |
552 | | - 'message': 'Login Failed already authenticated', |
553 | | - 'severity': 'critical' |
554 | | - } |
555 | | - ), |
556 | | - http_return_code |
557 | | - ) |
558 | | - username = str(request.args.get('username')) |
559 | | - password = str(request.args.get('password')) |
560 | | - user = self.appbuilder.sm.auth_user_ldap(username, password) |
561 | | - if not user: |
562 | | - http_return_code = 401 |
563 | | - response = make_response( |
564 | | - jsonify( |
565 | | - { |
566 | | - 'message': 'Login Failed', |
567 | | - 'severity': 'critical' |
568 | | - } |
569 | | - ), |
570 | | - http_return_code |
571 | | - ) |
572 | | - else: |
573 | | - login_user(user, remember=False) |
574 | | - http_return_code = 201 |
575 | | - response = make_response( |
576 | | - jsonify( |
577 | | - { |
578 | | - 'message': 'Login Success', |
579 | | - 'severity': 'info' |
580 | | - } |
581 | | - ), |
582 | | - http_return_code |
583 | | - ) |
584 | | - return response |
585 | | - """ |
586 | | - |
587 | 552 |
|
588 | 553 | class AuthOIDView(AuthView): |
589 | 554 | login_template = "appbuilder/general/security/login_oid.html" |
@@ -641,7 +606,9 @@ class AuthOAuthView(AuthView): |
641 | 606 | @expose("/login/") |
642 | 607 | @expose("/login/<provider>") |
643 | 608 | @expose("/login/<provider>/<register>") |
644 | | - def login(self, provider=None, register=None): |
| 609 | + def login( |
| 610 | + self, provider: Optional[str] = None, register: Optional[str] = None |
| 611 | + ) -> Response: |
645 | 612 | log.debug("Provider: {0}".format(provider)) |
646 | 613 | if g.user is not None and g.user.is_authenticated: |
647 | 614 | log.debug("Already authenticated {0}".format(g.user)) |
@@ -690,8 +657,12 @@ def login(self, provider=None, register=None): |
690 | 657 | return redirect(self.appbuilder.get_url_for_index) |
691 | 658 |
|
692 | 659 | @expose("/oauth-authorized/<provider>") |
693 | | - def oauth_authorized(self, provider): |
| 660 | + def oauth_authorized(self, provider: str) -> Response: |
694 | 661 | log.debug("Authorized init") |
| 662 | + if provider not in self.appbuilder.sm.oauth_remotes: |
| 663 | + flash(u"Provider not supported.", "warning") |
| 664 | + log.warning("OAuth authorized got an unknown provider %s", provider) |
| 665 | + return redirect(self.appbuilder.get_url_for_login) |
695 | 666 | resp = self.appbuilder.sm.oauth_remotes[provider].authorize_access_token() |
696 | 667 | if resp is None: |
697 | 668 | flash(u"You denied the request to sign in.", "warning") |
@@ -735,11 +706,14 @@ def oauth_authorized(self, provider): |
735 | 706 | except jwt.InvalidTokenError: |
736 | 707 | raise Exception("State signature is not valid!") |
737 | 708 |
|
738 | | - try: |
739 | | - next_url = state["next"][0] or self.appbuilder.get_url_for_index |
740 | | - except (KeyError, IndexError): |
741 | | - next_url = self.appbuilder.get_url_for_index |
742 | | - |
| 709 | + next_url = self.appbuilder.get_url_for_index |
| 710 | + # Check if there is a next url on state |
| 711 | + if "next" in state and len(state["next"]) > 0: |
| 712 | + parsed_uri = urlparse(state["next"][0]) |
| 713 | + if parsed_uri.netloc != request.host: |
| 714 | + log.warning("Got an invalid next URL: %s", parsed_uri.netloc) |
| 715 | + else: |
| 716 | + next_url = state["next"][0] |
743 | 717 | return redirect(next_url) |
744 | 718 |
|
745 | 719 |
|
|
0 commit comments