Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def getInternal(self, critic):
if not self.__internal:
if self.isAnonymous():
self.__internal = dbutils.User.makeAnonymous()
else:
self.__internal = dbutils.User.fromId(critic.database, self.id)
return self.__internal
def performHTTPAuthentication(self, db, username, password):
cursor = db.readonly_cursor()
cursor.execute("""SELECT id, access_type, uid
FROM accesstokens
WHERE part1=%s
AND part2=%s""",
(username, password))
row = cursor.fetchone()
if row:
token_id, access_type, user_id = row
if access_type == "anonymous":
db.setUser(dbutils.User.makeAnonymous())
elif access_type == "system":
db.setUser(dbutils.User.makeSystem())
else:
user = dbutils.User.fromId(db, user_id)
authentication_labels = self.getAuthenticationLabels(user)
db.setUser(user, authentication_labels)
import api
db.critic.setAccessToken(api.accesstoken.fetch(db.critic, token_id))
cursor.execute("""SELECT id
FROM accesscontrolprofiles
WHERE access_token=%s""",
(token_id,))
row = cursor.fetchone()
def finishOAuth(db, req, provider):
try:
provider.finish(db, req)
except (auth.InvalidRequest, auth.Failure):
_, error_body = handleException(
db, req, dbutils.User.makeAnonymous(), as_html=True)
raise page.utils.DisplayMessage(
title="Authentication failed",
body="".join(error_body),
html=True)
def handleDisplayMessage(db, req, message):
user = db.user
if user is None:
user = dbutils.User.makeAnonymous()
document = page.utils.displayMessage(
db, req, user, title=message.title, message=message.body,
review=message.review, is_html=message.html)
req.setContentType("text/html")
req.setStatus(message.status)
req.start()
return [str(document)]
def execute(self, db, req):
import page.utils
self.body = str(page.utils.displayMessage(
db, req, dbutils.User.makeAnonymous(),
title="Authentication required",
message=("You must provide valid HTTP authentication to access "
"this system.")))
self.content_type = "text/html"
req.addResponseHeader("WWW-Authenticate", "Basic realm=\"Critic\"")
return super(RequestHTTPAuthentication, self).execute(db, req)
# Step 3(b): If the request has a "use_httpauth" cookie, request/require
# HTTP authentication. This is a just a convenience feature for
# clients using HTTP stacks that only send credentials in
# response to server challenges. (If cookie sessions are used,
# no such challenge would normally be returned, we'd rather
# redirect to the login page.)
if req.cookies.get("use_httpauth"):
raise request.RequestHTTPAuthentication()
# Also do this for requests with a "httpauth=yes" query parameter.
if req.getParameter("httpauth", "no") == "yes":
raise request.RequestHTTPAuthentication()
# Step 4: If anonymous access is supported or if it should be allowed as an
# exception for the accessed path, leave the session anonymous.
if configuration.base.ALLOW_ANONYMOUS_USER or isInsecurePath(req):
db.setUser(dbutils.User.makeAnonymous())
req.session_type = None
return
# Step 5: If HTTP authentication is required (i.e. no session cookies) then
# request that now.
if configuration.base.SESSION_TYPE == "httpauth":
raise request.RequestHTTPAuthentication()
# Step 6: Cookie based sessions are enabled, and not anonymous access. If
# this is a POST or PUT request, respond with 403 Forbidden, and
# otherwise redirect to the login page.
if not req.allowRedirect(307):
raise request.Forbidden("Valid user session required")
raise request.NeedLogin(req, optional=req.cookies.has_key("has_sid"))