+ raise AuthException("Token '{}' not found".format(token), http_code=HTTPStatus.NOT_FOUND)
+
+ def _internal_authorize(self, token):
+ try:
+ if not token:
+ raise AuthException("Needed a token or Authorization http header", http_code=HTTPStatus.UNAUTHORIZED)
+ if token not in self.tokens:
+ raise AuthException("Invalid token or Authorization http header", http_code=HTTPStatus.UNAUTHORIZED)
+ session = self.tokens[token]
+ now = time()
+ if session["expires"] < now:
+ del self.tokens[token]
+ raise AuthException("Expired Token or Authorization http header", http_code=HTTPStatus.UNAUTHORIZED)
+ return session
+ except AuthException:
+ if self.config["global"].get("test.user_not_authorized"):
+ return {"id": "fake-token-id-for-test",
+ "project_id": self.config["global"].get("test.project_not_authorized", "admin"),
+ "username": self.config["global"]["test.user_not_authorized"]}
+ else:
+ raise
+
+ def _internal_new_token(self, session, indata, remote):
+ now = time()
+ user_content = None
+
+ # Try using username/password
+ if indata.get("username"):
+ user_rows = self.db.get_list("users", {"username": indata.get("username")})
+ user_content = None
+ if user_rows:
+ user_content = user_rows[0]
+ salt = user_content["_admin"]["salt"]
+ shadow_password = sha256(indata.get("password", "").encode('utf-8') + salt.encode('utf-8')).hexdigest()
+ if shadow_password != user_content["password"]:
+ user_content = None
+ if not user_content:
+ raise AuthException("Invalid username/password", http_code=HTTPStatus.UNAUTHORIZED)
+ elif session:
+ user_rows = self.db.get_list("users", {"username": session["username"]})
+ if user_rows:
+ user_content = user_rows[0]
+ else:
+ raise AuthException("Invalid token", http_code=HTTPStatus.UNAUTHORIZED)
+ else:
+ raise AuthException("Provide credentials: username/password or Authorization Bearer token",
+ http_code=HTTPStatus.UNAUTHORIZED)
+
+ token_id = ''.join(random_choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
+ for _ in range(0, 32))
+ if indata.get("project_id"):
+ project_id = indata.get("project_id")
+ if project_id not in user_content["projects"]:
+ raise AuthException("project {} not allowed for this user"
+ .format(project_id), http_code=HTTPStatus.UNAUTHORIZED)
+ else:
+ project_id = user_content["projects"][0]
+ if project_id == "admin":
+ session_admin = True
+ else:
+ project = self.db.get_one("projects", {"_id": project_id})
+ session_admin = project.get("admin", False)
+ new_session = {"issued_at": now, "expires": now + 3600,
+ "_id": token_id, "id": token_id, "project_id": project_id, "username": user_content["username"],
+ "remote_port": remote.port, "admin": session_admin}
+ if remote.name:
+ new_session["remote_host"] = remote.name
+ elif remote.ip:
+ new_session["remote_host"] = remote.ip
+
+ self.tokens[token_id] = new_session
+ return deepcopy(new_session)
+
+ def _internal_get_token_list(self, session):
+ token_list = []
+ for token_id, token_value in self.tokens.items():
+ if token_value["username"] == session["username"]:
+ token_list.append(deepcopy(token_value))
+ return token_list
+
+ def _internal_get_token(self, session, token_id):
+ token_value = self.tokens.get(token_id)
+ if not token_value:
+ raise AuthException("token not found", http_code=HTTPStatus.NOT_FOUND)
+ if token_value["username"] != session["username"] and not session["admin"]:
+ raise AuthException("needed admin privileges", http_code=HTTPStatus.UNAUTHORIZED)
+ return token_value
+
+ def _internal_del_token(self, token_id):
+ try:
+ del self.tokens[token_id]
+ return "token '{}' deleted".format(token_id)
+ except KeyError:
+ raise AuthException("Token '{}' not found".format(token_id), http_code=HTTPStatus.NOT_FOUND)