Exemple #1
0
def headless_theatre_view(env, get_vars, post_vars, csrf_clerk, session, user):
    page_data = basic_page_data('theatre-headless')
    status = '200 OK'
    given_password = post_vars.get('theatre_password', [''])[0]
    theatre_password = get_property('theatre_password', None)
    if (user is not None or given_password == theatre_password
            or patreon.validate_session(env)):
        page_data['chat_uri'] = turbo_views['chat-headless'].path
        page_data['youtube_stream_id'] = get_property('theatre_stream_id')
        response_body = templates.render('youtube_embed', page_data)
    else:
        callback_view = turbo_views['patreon-theatre-callback']
        redirect_uri = callback_view.uri
        oauth = turbo_session.OAuth2Session(config.patreon.client_id,
                                            redirect_uri=redirect_uri,
                                            scope=config.patreon.scope)
        authorization_url, state = oauth.authorization_url(
            config.patreon.authorize_url,
            turbo_session.generate_state(env, csrf_clerk))
        page_data['patreon_authorization_uri'] = authorization_url
        page_data['form_action'] = turbo_views['theatre-headless'].path
        page_data['login_uri'] = util.build_url(
            turbo_views['login'].path,
            query={'redirect_to': turbo_views['theatre'].path})
        response_body = templates.render('theatre_auth', page_data)
    response_headers = util.basic_response_header(response_body)
    return response_body, response_headers, status
Exemple #2
0
def discord_callback_view(env, get_vars, post_vars, csrf_clerk, session, user):
    redirect_uri = turbo_views['discord-callback'].uri
    authorization_response = redirect_uri + '?' + env['QUERY_STRING']
    discord_user_url = config.discord.api_endpoint + '/users/@me'
    access_level = User.get_access_level(user)
    try:
        oauth = turbo_session.OAuth2Session(config.discord.client_id,
                                            redirect_uri=redirect_uri,
                                            scope=config.discord.scope)
        oauth_state = turbo_session.retrieve_oauth_state(
            get_vars.get('state', [''])[0])
        if oauth_state and csrf_clerk.validate('oauth-authorization',
                                               oauth_state[0]):
            token = oauth.fetch_token(
                config.discord.token_url,
                authorization_response=authorization_response,
                client_secret=config.discord.client_secret)
            discord_user = oauth.get(discord_user_url).json()
            if discord_user is not None and discord_user.get('id') is not None:
                # If people link their discord to another account,
                # the old one should lose its turbo role
                discord_id = int(discord_user['id'])
                if user.discord_id and user.discord_id != discord_id:
                    if not discord.remove_turbo_role(user.discord_id):
                        return error_view(
                            'Unexpected error',
                            'Failed to reassign TURBO status to '
                            'a different Discord account. '
                            'Please try again.')
                user.set_discord_id(discord_id)
                discord.add_turbo_role(discord_id, token)

                redirect_to = str(oauth_state[1])
                if redirect_to.startswith('/'):
                    redirect_to = util.build_url(redirect_to)

                status = '307 Temporary Redirect'
                response_body = ''
                response_headers = [('Location', redirect_to)]
            else:
                return error_view('Unexpected error',
                                  'Failed to retrieve Discord user details.',
                                  access_level=access_level)
        else:
            return error_view('CSRF Verfication failed',
                              'Failed to authorize Discord account due to a '
                              'CSRF verfication error, try again.',
                              access_level=access_level)

    except OAuth2Error as error:
        # Might indicate a "deny" on granting access to the app
        logger.info(f'OAuth 2.0 Error occured: {error}',
                    exc_info=config.debug_mode)
        return error_view('OAuth Error',
                          'Failed to authorize Discord account, try again.',
                          access_level=access_level)
    else:
        # Normal function return without errors
        return response_body, response_headers, status
Exemple #3
0
 def __init__(self, display_name, path=None, view=None, uri=None):
     self.display_name = display_name
     if uri is None and path is not None:
         uri = util.build_url(path, base='base')
     if path is not None:
         path = config.base_path + path
     self.path = path
     self.uri = uri
     self.view = view
        def decorated_function(env, csrf_clerk):
            get_vars = util.retrieve_get_vars(env)
            post_vars = util.retrieve_post_vars(env)
            session = turbo_session.get_session(env)
            account = turbo_session.retrieve_oauth_account(session)

            # Start OAuth
            cookie_set = int(get_vars.get('cookie_set', [0])[0])
            # Failed to set cookie, tell user to enable cookies
            if(account is None and min_access_level >= ACL.turbo and
               cookie_set == 1):
                return error_view('Login Error',
                                  'Failed to create session. Try to enable '
                                  ' cookies for this site.')

            elif(account is None and min_access_level >= ACL.turbo):
                # Show Auth Error in headless mode
                if(headless):
                    return error_view('Auth Error', 'You are not logged in.',
                                      nav, headless=True)

                redirect_uri = turbo_views['oauth-callback'].uri
                oauth = turbo_session.OAuth2Session(
                    config.mastodon.client_id,
                    redirect_uri=redirect_uri,
                    scope=config.mastodon.scope)
                authorization_url, state = oauth.authorization_url(
                    config.mastodon.authorize_url,
                    turbo_session.generate_state(env, csrf_clerk)
                )

                status = '307 Temporary Redirect'
                response_body = ''
                response_headers = [('Location', str(authorization_url))]

            # Redirect to url without cookie_set parameter
            elif(cookie_set == 1):
                status = '307 Temporary Redirect'
                response_body = ''
                response_headers = [
                    ('Location', util.build_url(env['PATH_INFO']))
                ]

            # Display View
            else:
                user = User.create(account)
                access_level = User.get_access_level(user)
                if access_level < min_access_level:
                    return error_view('Missing Privileges',
                                      'You do not have the required '
                                      'permissions to access this.',
                                      access_level=access_level)
                response_body, response_headers, status = func(
                    env, get_vars, post_vars, csrf_clerk, session, user
                )

            return response_body, response_headers, status
Exemple #5
0
def frash_chat_view(env, get_vars, post_vars, csrf_clerk, session, user):
    page_data = basic_page_data('frash-chat')
    page_data['frash_mode'] = 'frash-show-mode'
    page_data['rules_uri'] = turbo_views['rules'].path
    page_data['rand_spinner'] = str(random.randint(1, 5))
    page_data['webchat_uri'] = util.build_url('/webchat', 'websockets')
    page_data['live_channel'] = config.discord.live_channel
    status = '200 OK'
    response_body = templates.render('chat_headless', page_data)
    response_headers = util.basic_response_header(response_body)
    return response_body, response_headers, status
Exemple #6
0
 def __init__(self, client_cls, name, path, min_access_level=ACL.turbo):
     self.client_cls = client_cls
     self.name = name
     self.path = config.websockets_path + path
     self.uri = util.build_url(path, base='websockets')
     self.min_access_level = min_access_level
     self.clients = {}
     self.jobs = []
     self.job_state = JobState.new
     self._redis_channel = None
     this.channels.append(self)
Exemple #7
0
def logout_view(env, csrf_clerk):
    response_body = 'Template Render Error.'
    response_headers = util.basic_response_header(response_body)
    status = '200 OK'

    session = turbo_session.get_session(env)
    turbo_session.delete_session(session)
    status = '307 Temporary Redirect'
    response_body = ''
    response_headers = [
        util.unset_cookie_header('TB_SESSION'),
        ('Location', util.build_url('/', base='base'))
    ]
    return response_body, response_headers, status
def oauth_callback_view(env, csrf_clerk):
    get_vars = util.retrieve_get_vars(env)
    redirect_uri = turbo_views['oauth-callback'].uri
    authorization_response = redirect_uri + '?' + env['QUERY_STRING']
    try:
        oauth = turbo_session.OAuth2Session(
            config.mastodon.client_id,
            redirect_uri=redirect_uri,
            scope=config.mastodon.scope
        )
        oauth_state = turbo_session.retrieve_oauth_state(
            get_vars.get('state', [''])[0]
        )
        if(oauth_state and
           csrf_clerk.validate('oauth-authorization', oauth_state[0])):
            token = oauth.fetch_token(
                config.mastodon.token_url,
                authorization_response=authorization_response,
                client_secret=config.mastodon.client_secret
            )
            session_token = turbo_session.create_session(token)

            if(session_token is not None):
                redirect_to = str(oauth_state[1])
                if redirect_to.startswith('/'):
                    redirect_to = util.build_url(redirect_to,
                                                 query={'cookie_set': 1})

                status = '307 Temporary Redirect'
                response_body = ''
                response_headers = [
                    util.set_cookie_header('TB_SESSION', session_token),
                    ('Location', redirect_to)
                ]
            else:
                return error_view('Internal Error',
                                  'Failed to create session.')
        else:
            return error_view('CSRF Verfication failed',
                              'Failed to authorize account due to a CSRF '
                              'verfication error, try again.')

    except OAuth2Error as error:
        # Might indicate a "deny" on granting access to the app
        util.print_exception('OAuth 2.0 Error occured: ', error)
        return error_view('OAuth Error',
                          'Failed to authorize account, try again.')
    else:
        # Normal function return without errors
        return response_body, response_headers, status
Exemple #9
0
 def __init__(self, name, path, min_access_level=ACL.turbo):
     self.name = name
     self.path = config.websockets_path + path
     self.uri = util.build_url(path, base='websockets')
     self.min_access_level = min_access_level
     this.channels.append(self)