<commit_before>#!/usr/bin/env python
import flask
import flask.json
import functools
import server
import urllib.request, urllib.parse
import secrets
import uuid
import utils

# See https://github.com/justintv/Twitch-API/blob/master/authentication.md#scopes
# We don't actually need, or want, any at present
REQUEST_SCOPES = []

SPECIAL_USERS = {
	'lrrbot': ['chat_login'],
	'loadingreadyrun': ['channel_subscriptions'],
}

# Needs to be the URI of this script, and also the registered URI for the app
REDIRECT_URI = 'http://lrrbot.mrphlip.com/login'
#REDIRECT_URI = 'http://localhost:5000/login'

def with_session(func):
	"""
	Pass the current login session information to the function

	Usage:
	@server.app.route('/path')
	@with_session
	def handler(session): # keyword argument must be "session"
		...
	"""
	@functools.wraps(func)
	def wrapper(*args, **kwargs):
		kwargs['session'] = load_session()
		return func(*args, **kwargs)
	return wrapper

def with_minimal_session(func):
	"""
	Pass the current login session information to the function

	Do not include extra session information, intended for master.html. Useful for
	places that need the current user id, but shouldn't (or don't need to) call
	botinteract.

	Usage:
	@server.app.route('/path')
	@with_minimal_session
	def handler(session):
		...
	"""
	@functools.wraps(func)
	def wrapper(*args, **kwargs):
		kwargs['session'] = load_session(include_url=False, include_header=False)
		return func(*args, **kwargs)
	return wrapper

def require_login(func):
	"""
	Like with_session, but if the user isn't logged in,
	send them via the login screen.
	"""
	@functools.wraps(func)
	def wrapper(*args, **kwargs):
		session = load_session()
		if session['user']:
			kwargs['session'] = session
			return func(*args, **kwargs)
		else:
			return login(session['url'])
	return wrapper

def require_mod(func):
	"""
	Like with_session, but if the user isn't logged in,
	send them via the login screen. If the user isn't
	a moderator, kick them out.
	"""
	@functools.wraps(func)
	def wrapper(*args, **kwargs):
		session = load_session()
		if session['user']:
			kwargs['session'] = session
			if session['header']['is_mod']:
				return func(*args, **kwargs)
			else:
				return flask.render_template('require_mod.html', session=session)
		else:
			return login(session['url'])
	return wrapper

def load_session(include_url=True, include_header=True):
	"""
	Get the login session information from the cookies.

	Includes all the information needed by the master.html template.
	"""
	import botinteract
	# could potentially add other things here in the future...
	session = {
		"user": flask.session.get('user', secrets.apipass.get(flask.request.values.get("apipass"))),
	}
	if include_url:
		session['url'] = flask.request.url
	else:
		session['url'] = None
	if include_header:
		session['header'] = botinteract.get_header_info()
	return session

@server.app.route('/login')
def login(return_to=None):
	if 'code' not in flask.request.values:
		if return_to is None:
			return_to = flask.request.values.get('return_to')
		flask.session['login_return_to'] = return_to

		if 'as' in flask.request.values:
			flask.session['login_as'] = flask.request.values['as']
			if flask.session['login_as'] not in SPECIAL_USERS:
				return utils.error_page("Not a recognised user name: %s" % flask.session['login_as'])
			scope = SPECIAL_USERS[flask.session['login_as']]
		else:
			flask.session['login_as'] = None
			scope = REQUEST_SCOPES
<commit_msg>Didn't end up using this flag<commit_after>#!/usr/bin/env python
import flask
import flask.json
import functools
import server
import urllib.request, urllib.parse
import secrets
import uuid
import utils

# See https://github.com/justintv/Twitch-API/blob/master/authentication.md#scopes
# We don't actually need, or want, any at present
REQUEST_SCOPES = []

SPECIAL_USERS = {
	'lrrbot': ['chat_login'],
	'loadingreadyrun': ['channel_subscriptions'],
}

# Needs to be the URI of this script, and also the registered URI for the app
REDIRECT_URI = 'http://lrrbot.mrphlip.com/login'
#REDIRECT_URI = 'http://localhost:5000/login'

def with_session(func):
	"""
	Pass the current login session information to the function

	Usage:
	@server.app.route('/path')
	@with_session
	def handler(session): # keyword argument must be "session"
		...
	"""
	@functools.wraps(func)
	def wrapper(*args, **kwargs):
		kwargs['session'] = load_session()
		return func(*args, **kwargs)
	return wrapper

def with_minimal_session(func):
	"""
	Pass the current login session information to the function

	Do not include extra session information, intended for master.html. Useful for
	places that need the current user id, but shouldn't (or don't need to) call
	botinteract.

	Usage:
	@server.app.route('/path')
	@with_minimal_session
	def handler(session):
		...
	"""
	@functools.wraps(func)
	def wrapper(*args, **kwargs):
		kwargs['session'] = load_session(include_url=False, include_header=False)
		return func(*args, **kwargs)
	return wrapper

def require_login(func):
	"""
	Like with_session, but if the user isn't logged in,
	send them via the login screen.
	"""
	@functools.wraps(func)
	def wrapper(*args, **kwargs):
		session = load_session()
		if session['user']:
			kwargs['session'] = session
			return func(*args, **kwargs)
		else:
			return login(session['url'])
	return wrapper

def require_mod(func):
	"""
	Like with_session, but if the user isn't logged in,
	send them via the login screen. If the user isn't
	a moderator, kick them out.
	"""
	@functools.wraps(func)
	def wrapper(*args, **kwargs):
		session = load_session()
		if session['user']:
			kwargs['session'] = session
			if session['header']['is_mod']:
				return func(*args, **kwargs)
			else:
				return flask.render_template('require_mod.html', session=session)
		else:
			return login(session['url'])
	return wrapper

def load_session(include_url=True, include_header=True):
	"""
	Get the login session information from the cookies.

	Includes all the information needed by the master.html template.
	"""
	import botinteract
	# could potentially add other things here in the future...
	session = {
		"user": flask.session.get('user', secrets.apipass.get(flask.request.values.get("apipass"))),
	}
	if include_url:
		session['url'] = flask.request.url
	else:
		session['url'] = None
	if include_header:
		session['header'] = botinteract.get_header_info()
	return session

@server.app.route('/login')
def login(return_to=None):
	if 'code' not in flask.request.values:
		if return_to is None:
			return_to = flask.request.values.get('return_to')
		flask.session['login_return_to'] = return_to

		if 'as' in flask.request.values:
			if flask.request.values['as'] not in SPECIAL_USERS:
				return utils.error_page("Not a recognised user name: %s" % flask.request.values['as'])
			scope = SPECIAL_USERS[flask.request.values['as']]
		else:
			scope = REQUEST_SCOPES

{'fraction_non_alphanumeric': 0.13023930384336477, 'fraction_numerical': 0.001160261058738216, 'mean_word_length': 4.77071129707113, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4173203', 'n_tokens_mistral': 2411, 'n_tokens_neox': 2247, 'n_words': 635}