]> git.lizzy.rs Git - cheatdb.git/blob - app/blueprints/users/claim.py
Improve user authentication error handling
[cheatdb.git] / app / blueprints / users / claim.py
1 # Content DB
2 # Copyright (C) 2018  rubenwardy
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17
18 from . import bp
19 from flask import redirect, render_template, session, request, flash, url_for
20 from flask_user import current_user
21 from app.models import db, User, UserRank
22 from app.utils import randomString, loginUser, rank_required
23 from app.tasks.forumtasks import checkForumAccount
24 from app.tasks.phpbbparser import getProfile
25
26 @bp.route("/user/claim/", methods=["GET", "POST"])
27 def claim():
28         username = request.args.get("username")
29         if username is None:
30                 username = ""
31         else:
32                 method = request.args.get("method")
33                 user = User.query.filter_by(forums_username=username).first()
34                 if user and user.rank.atLeast(UserRank.NEW_MEMBER):
35                         flash("User has already been claimed", "danger")
36                         return redirect(url_for("users.claim"))
37                 elif method == "github":
38                         if user is None or user.github_username is None:
39                                 flash("Unable to get Github username for user", "danger")
40                                 return redirect(url_for("users.claim"))
41                         else:
42                                 return redirect(url_for("github.start"))
43                 elif user is None and request.method == "POST":
44                         flash("Unable to find user", "danger")
45                         return redirect(url_for("users.claim"))
46
47
48         token = None
49         if "forum_token" in session:
50                 token = session["forum_token"]
51         else:
52                 token = randomString(32)
53                 session["forum_token"] = token
54
55         if request.method == "POST":
56                 ctype   = request.form.get("claim_type")
57                 username = request.form.get("username")
58
59                 if username is None or len(username.strip()) < 2:
60                         flash("Invalid username", "danger")
61                 elif ctype == "github":
62                         task = checkForumAccount.delay(username)
63                         return redirect(url_for("tasks.check", id=task.id, r=url_for("users.claim", username=username, method="github")))
64                 elif ctype == "forum":
65                         user = User.query.filter_by(forums_username=username).first()
66                         if user is not None and user.rank.atLeast(UserRank.NEW_MEMBER):
67                                 flash("That user has already been claimed!", "danger")
68                                 return redirect(url_for("users.claim"))
69
70                         # Get signature
71                         sig = None
72                         try:
73                                 profile = getProfile("https://forum.minetest.net", username)
74                                 sig = profile.signature if profile else None
75                         except IOError as e:
76                                 if hasattr(e, 'message'):
77                                         message = e.message
78                                 else:
79                                         message = str(e)
80
81                                 flash("Error whilst attempting to access forums: " + message, "danger")
82                                 return redirect(url_for("users.claim", username=username))
83
84                         if profile is None:
85                                 flash("Unable to get forum signature - does the user exist?", "danger")
86                                 return redirect(url_for("users.claim", username=username))
87
88                         # Look for key
89                         if token in sig:
90                                 if user is None:
91                                         user = User(username)
92                                         user.forums_username = username
93                                         db.session.add(user)
94                                         db.session.commit()
95
96                                 if loginUser(user):
97                                         return redirect(url_for("users.set_password"))
98                                 else:
99                                         flash("Unable to login as user", "danger")
100                                         return redirect(url_for("users.claim", username=username))
101
102                         else:
103                                 flash("Could not find the key in your signature!", "danger")
104                                 return redirect(url_for("users.claim", username=username))
105                 else:
106                         flash("Unknown claim type", "danger")
107
108         return render_template("users/claim.html", username=username, key=token)