]> git.lizzy.rs Git - cheatdb.git/blob - app/blueprints/users/claim.py
Fix crash on no signature
[cheatdb.git] / app / blueprints / users / claim.py
1 # ContentDB
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
34                 user = User.query.filter_by(forums_username=username).first()
35                 if user and user.rank.atLeast(UserRank.NEW_MEMBER):
36                         flash("User has already been claimed", "danger")
37                         return redirect(url_for("users.claim"))
38                 elif method == "github":
39                         if user is None or user.github_username is None:
40                                 flash("Unable to get Github username for user", "danger")
41                                 return redirect(url_for("users.claim", username=username))
42                         else:
43                                 return redirect(url_for("github.start"))
44                 elif user is None and request.method == "POST":
45                         flash("Unable to find user", "danger")
46                         return redirect(url_for("users.claim"))
47
48
49         token = None
50         if "forum_token" in session:
51                 token = session["forum_token"]
52         else:
53                 token = randomString(32)
54                 session["forum_token"] = token
55
56         if request.method == "POST":
57                 ctype   = request.form.get("claim_type")
58                 username = request.form.get("username")
59
60                 if username is None or len(username.strip()) < 2:
61                         flash("Invalid username", "danger")
62                 elif ctype == "github":
63                         task = checkForumAccount.delay(username)
64                         return redirect(url_for("tasks.check", id=task.id, r=url_for("users.claim", username=username, method="github")))
65                 elif ctype == "forum":
66                         user = User.query.filter_by(forums_username=username).first()
67                         if user is not None and user.rank.atLeast(UserRank.NEW_MEMBER):
68                                 flash("That user has already been claimed!", "danger")
69                                 return redirect(url_for("users.claim"))
70
71                         # Get signature
72                         sig = None
73                         try:
74                                 profile = getProfile("https://forum.minetest.net", username)
75                                 sig = profile.signature if profile else None
76                         except IOError as e:
77                                 if hasattr(e, 'message'):
78                                         message = e.message
79                                 else:
80                                         message = str(e)
81
82                                 flash("Error whilst attempting to access forums: " + message, "danger")
83                                 return redirect(url_for("users.claim", username=username))
84
85                         if profile is None:
86                                 flash("Unable to get forum signature - does the user exist?", "danger")
87                                 return redirect(url_for("users.claim", username=username))
88
89                         # Look for key
90                         if sig and token in sig:
91                                 if user is None:
92                                         user = User(username)
93                                         user.forums_username = username
94                                         db.session.add(user)
95                                         db.session.commit()
96
97                                 if loginUser(user):
98                                         return redirect(url_for("users.set_password"))
99                                 else:
100                                         flash("Unable to login as user", "danger")
101                                         return redirect(url_for("users.claim", username=username))
102
103                         else:
104                                 flash("Could not find the key in your signature!", "danger")
105                                 return redirect(url_for("users.claim", username=username))
106                 else:
107                         flash("Unknown claim type", "danger")
108
109         return render_template("users/claim.html", username=username, key=token)