Пример #1
0
    def __init__(self, app, entry_point_group=None):
        """Initialize state."""
        self.app = app
        self.scopes = {}

        # Initialize OAuth2 provider
        oauth2.init_app(app)

        # Flask-OAuthlib does not support CACHE_REDIS_URL
        if app.config['OAUTH2_CACHE_TYPE'] == 'redis' and app.config.get(
                'CACHE_REDIS_URL'):
            from redis import from_url as redis_from_url
            app.config.setdefault(
                'OAUTH2_CACHE_REDIS_HOST',
                redis_from_url(app.config['CACHE_REDIS_URL'])
            )

        # Configures an OAuth2Provider instance to use configured caching
        # system to get and set the grant token.
        bind_cache_grant(app, oauth2, lambda: OAuthUserProxy(current_user))

        # Disables oauthlib's secure transport detection in in debug mode.
        if app.debug or app.testing:
            os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'

        if entry_point_group:
            self.load_entry_point_group(entry_point_group)
Пример #2
0
def setup_app():
    """Setup OAuth2 provider."""
    # Initialize OAuth2 provider
    oauth2.init_app(current_app)

    # Configures the OAuth2 provider to use the SQLALchemy models for getters
    # and setters for user, client and tokens.
    bind_sqlalchemy(oauth2, db.session, client=Client)

    # Flask-OAuthlib does not support CACHE_REDIS_URL
    if cfg['OAUTH2_CACHE_TYPE'] == 'redis' and \
       cfg.get('CACHE_REDIS_URL'):
        from redis import from_url as redis_from_url
        cfg.setdefault(
            'OAUTHLIB_CACHE_REDIS_HOST',
            redis_from_url(cfg['CACHE_REDIS_URL'])
        )

    # Configures an OAuth2Provider instance to use configured caching system
    # to get and set the grant token.
    bind_cache_grant(current_app, oauth2, OAuthUserProxy.get_current_user)

    # Disables oauthlib's secure transport detection in in debug mode.
    if current_app.debug or current_app.testing:
        os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
Пример #3
0
    def __init__(self, app, entry_point_group=None):
        """Initialize state."""
        self.app = app
        self.scopes = {}

        # Initialize OAuth2 provider
        oauth2.init_app(app)

        # Configures the OAuth2 provider to use the SQLALchemy models for
        # getters and setters for user, client and tokens.
        bind_sqlalchemy(oauth2, db.session, client=Client)

        # Flask-OAuthlib does not support CACHE_REDIS_URL
        if app.config['OAUTH2_CACHE_TYPE'] == 'redis' and app.config.get(
                'CACHE_REDIS_URL'):
            from redis import from_url as redis_from_url
            app.config.setdefault(
                'OAUTH2_CACHE_REDIS_HOST',
                redis_from_url(app.config['CACHE_REDIS_URL'])
            )

        # Configures an OAuth2Provider instance to use configured caching
        # system to get and set the grant token.
        bind_cache_grant(app, oauth2, lambda: OAuthUserProxy(current_user))

        # Disables oauthlib's secure transport detection in in debug mode.
        if app.debug or app.testing:
            os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'

        if entry_point_group:
            self.load_entry_point_group(entry_point_group)
Пример #4
0
def setup_app():
    """
    Setup OAuth2 provider
    """
    # Initialize OAuth2 provider
    oauth2.init_app(current_app)

    # Register default scopes (note, each module will)
    for scope, options in current_app.config['OAUTH2_DEFAULT_SCOPES'].items():
        if scope not in scopes:
            scopes.register(Scope(scope, options))

    # Configures the OAuth2 provider to use the SQLALchemy models for getters
    # and setters for user, client and tokens.
    bind_sqlalchemy(oauth2, db.session, client=OAuthClient)

    # Configures an OAuth2Provider instance to use configured caching system
    # to get and set the grant token.
    bind_cache_grant(current_app, oauth2, OAuthUserProxy.get_current_user)
    
    for x in ['oauthlib', 'flask_oauthlib']:
        logger = logging.getLogger('flask_oauthlib')
        logger.setLevel(current_app.logger.getEffectiveLevel())
        for h in current_app.logger.handlers:
            if h not in logger.handlers:
                logger.addHandler(h)
Пример #5
0
def setup_app():
    """
    Setup OAuth2 provider
    """
    # Initialize OAuth2 provider
    oauth2.init_app(current_app)

    # Register default scopes (note, each module will)
    for scope, options in current_app.config['OAUTH2_DEFAULT_SCOPES'].items():
        if scope not in scopes:
            scopes.register(Scope(scope, options))

    # Configures the OAuth2 provider to use the SQLALchemy models for getters
    # and setters for user, client and tokens.
    bind_sqlalchemy(oauth2, db.session, client=OAuthClient)

    # Configures an OAuth2Provider instance to use configured caching system
    # to get and set the grant token.
    bind_cache_grant(current_app, oauth2, OAuthUserProxy.get_current_user)

    for x in ['oauthlib', 'flask_oauthlib']:
        logger = logging.getLogger('flask_oauthlib')
        logger.setLevel(current_app.logger.getEffectiveLevel())
        for h in current_app.logger.handlers:
            if h not in logger.handlers:
                logger.addHandler(h)
Пример #6
0
def cache_provider(app):
    bind_sqlalchemy(oauth, db.session, user=User,
                    token=Token, client=Client)

    app.config.update({'OAUTH2_CACHE_TYPE': 'simple'})
    bind_cache_grant(app, oauth, current_user)
    return oauth
Пример #7
0
def cache_provider(app):
    oauth = OAuth2Provider(app)

    bind_sqlalchemy(oauth, db.session, user=Fhiruser, token=Token, client=Client)

    app.config.update({"OAUTH2_CACHE_TYPE": "simple"})
    bind_cache_grant(app, oauth, current_user)
    return oauth
Пример #8
0
def cache_provider(app):
    oauth = OAuth2Provider(app)

    bind_sqlalchemy(oauth, db.session, user=User, token=Token, client=Client)

    app.config.update({"OAUTH2_CACHE_TYPE": "simple"})
    bind_cache_grant(app, oauth, current_user)
    return oauth
Пример #9
0
def bind_oauth(app):
    # bind oauth getters and setters
    oauth.init_app(app)

    @oauth.usergetter
    def oauth_user_getter(username, password, *args, **kwargs):
        user = User.cache.filter_first(username=username)
        if user and user.check_password(password):
            return user
        return None

    @oauth.clientgetter
    def oauth_client_getter(client_id):
        return OAuthClient.cache.filter_first(client_id=client_id)

    @oauth.tokengetter
    def oauth_token_getter(access_token=None, refresh_token=None):
        if access_token:
            return OAuthToken.cache.filter_first(access_token=access_token)
        if refresh_token:
            return OAuthToken.cache.filter_first(refresh_token=refresh_token)

    @oauth.tokensetter
    def oauth_token_setter(token, req, *args, **kwargs):
        if hasattr(req, 'user') and req.user:
            user = req.user
        else:
            user = UserSession.get_current_user()

        client = req.client
        exist_token = OAuthToken.query.get((client.id, user.id))
        if exist_token:
            with db.auto_commit():
                db.session.delete(exist_token)

        tok = OAuthToken(**token)
        tok.user_id = user.id
        tok.client_id = client.id
        with db.auto_commit():
            db.session.add(tok)
        return tok

    # use the same cache
    bind_cache_grant(
        app,
        oauth,
        UserSession.get_current_user,
        config_prefix='ZERQU',
    )
Пример #10
0
def bind_oauth(app):
    # bind oauth getters and setters
    oauth.init_app(app)

    @oauth.usergetter
    def oauth_user_getter(username, password, *args, **kwargs):
        user = User.cache.filter_first(username=username)
        if user and user.check_password(password):
            return user
        return None

    @oauth.clientgetter
    def oauth_client_getter(client_id):
        return OAuthClient.cache.filter_first(client_id=client_id)

    @oauth.tokengetter
    def oauth_token_getter(access_token=None, refresh_token=None):
        if access_token:
            return OAuthToken.cache.filter_first(access_token=access_token)
        if refresh_token:
            return OAuthToken.cache.filter_first(refresh_token=refresh_token)

    @oauth.tokensetter
    def oauth_token_setter(token, req, *args, **kwargs):
        if hasattr(req, 'user') and req.user:
            user = req.user
        else:
            user = UserSession.get_current_user()

        client = req.client
        exist_token = OAuthToken.query.get((client.id, user.id))
        if exist_token:
            with db.auto_commit():
                db.session.delete(exist_token)

        tok = OAuthToken(**token)
        tok.user_id = user.id
        tok.client_id = client.id
        with db.auto_commit():
            db.session.add(tok)
        return tok

    # use the same cache
    bind_cache_grant(
        app, oauth,
        UserSession.get_current_user,
        config_prefix='ZERQU',
    )
Пример #11
0
def setup_app():
    """
    Setup OAuth2 provider
    """
    # Initialize OAuth2 provider
    oauth2.init_app(current_app)

    # Register default scopes (note, each module will)
    for scope, options in current_app.config['OAUTH2_DEFAULT_SCOPES'].items():
        if scope not in scopes:
            scopes.register(scope, options)

    # Configures the OAuth2 provider to use the SQLALchemy models for getters
    # and setters for user, client and tokens.
    bind_sqlalchemy(oauth2, db.session, client=Client)

    # Configures an OAuth2Provider instance to use configured caching system
    # to get and set the grant token.
    bind_cache_grant(current_app, oauth2, OAuthUserProxy.get_current_user)
Пример #12
0

# ======== Init OAuth =======


def get_current_user():
    if 'user_id' not in session:
        return None

    return User.get_by_id(session['user_id'])


oauth = OAuth2Provider(app)
app.config.update({'OAUTH2_CACHE_TYPE': 'simple'})

bind_cache_grant(app, oauth, get_current_user)

# ======== Import (initialize) oauth2 handlers =====
import api.oauth_handlers
# ======== Import (initialize) routes =========
import api.auth
import api.avatars
import api.bugreports
import api.mods
import api.maps
import api.oauth_client
import api.oauth_token
import api.deployment.slack
import api.achievements
import api.events
import api.query_commons
Пример #13
0
            

# ======== Init OAuth =======


def get_current_user():
    if 'user_id' not in session:
        return None

    return User.get_by_id(session['user_id'])


oauth = OAuth2Provider(app)
app.config.update({'OAUTH2_CACHE_TYPE': 'simple'})

bind_cache_grant(app, oauth, get_current_user)

# ======== Import (initialize) oauth2 handlers =====
import api.oauth_handlers
# ======== Import (initialize) routes =========
import api.deploy
import api.auth
import api.avatars
import api.bugreports
import api.games
import api.mods
import api.maps
import api.github
import api.oauth_client
import api.oauth_token
import api.slack
Пример #14
0
from flask_oauthlib.contrib.oauth2 import bind_cache_grant, bind_sqlalchemy

from extranet import app, db
from extranet.models.user import User
from extranet.models.oauth import OauthApp, OauthToken

# configure oauth provider
provider = OAuth2Provider(app)

# configure stores
bind_sqlalchemy(provider,
                db.session,
                user=User,
                client=OauthApp,
                token=OauthToken)


def current_user_func():
    return User.query.get(current_user.id)


bind_cache_grant(app, provider, current_user_func)

scopes = {
    'user.read': {
        'description_brief':
        "Read your profile",
        'description':
        "This app will be able to read your basic profile information, such as names, promotion or city"
    }
}