async def authenticate(self, request): if 'Authorization' not in request.headers: return auth = request.headers['Authorization'] try: scheme, credentials = auth.split() if scheme.lower() != 'basic': return decoded = base64.b64decode(credentials).decode("ascii") except (ValueError, UnicodeDecodeError, binascii.Error): raise AuthenticationError('Invalid basic auth credentials') pseudo, _, password = decoded.partition(':') db = get_session(DATABASE_URL) user = db.query(User).filter(User.pseudo == pseudo).one_or_none() if user is None or not user.check_password(password): db.close() raise AuthenticationError(f'pseudo or password incorrect') user.is_authenticated = True scopes = ['authenticated'] for group in user.groups: scopes.extend([permission.name for permission in group.permissions]) db.close() return AuthCredentials(scopes), user
def add_styles_to_db(): """Add supported pygments styles to the database""" db = get_session(REAL_DATABASE_URL) register(db, transaction_manager=transaction.manager) with transaction.manager: for item in STYLES: style = Style(name=item) db.add(style) db.close() click.secho('successfully inserted styles to the database!', fg='green')
def add_languages_to_db(): """Add supported pygments languages to the database""" db = get_session(REAL_DATABASE_URL) register(db, transaction_manager=transaction.manager) with transaction.manager: for lang in LANGUAGES: language = Language(name=lang) db.add(language) db.close() click.secho('successfully inserted languages to the database!', fg='green')
def shell(): """Creates a python interpreter to interact with pastebin modules""" db = get_session(REAL_DATABASE_URL) register(db, transaction_manager=transaction.manager) with transaction.manager: code.interact(banner='Interactive pastebin console', exitmsg='Good bye!', local={ 'pastebin': pastebin, 'db': db, 'transaction': transaction }) db.close()
def create_admin_user(first_name, last_name, pseudo, password, email): """Creates admin user""" db = get_session(REAL_DATABASE_URL) register(db, transaction_manager=transaction.manager) with transaction.manager: user = User(email=email, pseudo=pseudo, first_name=first_name, last_name=last_name, admin=True) user.set_password(password) default_group = db.query(Group).filter_by( name=DEFAULT_USER_GROUP).one_or_none() if default_group is None: default_group = Group(name=DEFAULT_USER_GROUP) for name in DEFAULT_PERMISSIONS: default_group.permissions.append(Permission(name=name)) user.groups.append(default_group) db.add(user) db.close() click.secho(f'created admin user {pseudo}!', fg='green')