예제 #1
0
def create_app():
    app = Flask(__name__)
    app.config.from_object('over_achiever.config')
    db = SQLAlchemy(app, metadata=models.metadata)
    db.create_all()
    resources.db = app.db = db

    oauth = OAuth(app)
    github = oauth.remote_app(
        'github',
        consumer_key='507e57ab372adeb8051b',
        consumer_secret='08a7dbaa06ac16daab00fac53724ee742c8081c5',
        request_token_params={'scope': 'user:email'},
        base_url='https://api.github.com/',
        request_token_url=None,
        access_token_method='POST',
        access_token_url='https://github.com/login/oauth/access_token',
        authorize_url='https://github.com/login/oauth/authorize')

    # set the token getter for the auth client
    github._tokengetter = lambda: session.get('github_token')
    resources.github = app.github = github

    api = Api(app)
    resource_map = (
        (User, '/v1.0/users'),
        (Goal, '/v1.0/goals'),
    )

    for resource, route in resource_map:
        api.add_resource(resource, route)

    return app
예제 #2
0
def create_app():
    app = Flask(__name__)
    app.config.from_object('over_achiever.config')
    db = SQLAlchemy(app, metadata=models.metadata)
    db.create_all()
    resources.db = app.db = db

    oauth = OAuth(app)
    google = oauth.remote_app(
        'google',
        consumer_key=os.getenv("GOOGLE_CLIENT_ID", ""),
        consumer_secret=os.getenv("GOOGLE_CLIENT_SECRET", ""),
        request_token_params={'scope': 'email'},
        base_url='https://www.googleapis.com/oauth2/v1/',
        request_token_url=None,
        access_token_method='POST',
        access_token_url='https://accounts.google.com/o/oauth2/token',
        authorize_url='https://accounts.google.com/o/oauth2/auth',
    )

    # set the token getter for the auth client
    google._tokengetter = lambda: session.get('google_token')
    resources.google = app.google = google

    api = Api(app)
    resource_map = (
        (User, '/v1.0/users'),
        (Goal, '/v1.0/goals'),
    )

    for resource, route in resource_map:
        api.add_resource(resource, route)

    return app
예제 #3
0
파일: oauth.py 프로젝트: jeyraof/inventory
# -*- coding: utf-8 -*-
__author__ = 'Jaeyoung'

from flask.ext.oauthlib.client import OAuth
from settings import FACEBOOK_OAUTH

facebook_oauth = OAuth()
facebook = facebook_oauth.remote_app(
    'facebook',
    base_url='https://graph.facebook.com/',
    request_token_url=None,
    access_token_url='/oauth/access_token',
    authorize_url='https://www.facebook.com/dialog/oauth',
    consumer_key=FACEBOOK_OAUTH.get('APP_ID', ''),
    consumer_secret=FACEBOOK_OAUTH.get('APP_SECRET', ''),
    request_token_params={'scope': 'email'})


class FacebookProfileFetcher(object):
    def __init__(self, access_token, fields):
        self.access_token = access_token
        self.fields = fields

    def fetch(self):
        fields_str = ','.join(self.fields)
        param = {
            'access_token': self.access_token,
            'locale': 'ko_KR',
            'fields': fields_str,
        }
        r = facebook.get('/me', data=param)
예제 #4
0
def configure(app):
    """
    will build oauthlib remote apps from config vairable in form of

    OAUTH = {
        "google": {
            "consumer_key": 'xxxxxxxxxxxx',
            "consumer_secret": 'xxxxxxxxxxxxxxxxxxxx',
            "request_token_params": {
                'scope': ('https://www.googleapis.com/auth/userinfo.email '
                          'https://www.googleapis.com/auth/userinfo.profile')
            },
            "base_url": 'https://www.googleapis.com/oauth2/v1/',
            "request_token_url": None,
            "access_token_method": 'POST',
            "access_token_url": 'https://accounts.google.com/o/oauth2/token',
            "authorize_url": 'https://accounts.google.com/o/oauth2/auth',
            "_info_endpoint": "userinfo"
        },
        "facebook": {
            "consumer_key": "xxxxxxxxxxxxxxxxxxxxxxxx",
            "consumer_secret": "xxxxxxxxxxxxxxxxxxxxxxxxxx",
            "request_token_params": {'scope': 'email'},
            "base_url": 'https://graph.facebook.com',
            "request_token_url": None,
            "access_token_url": '/oauth/access_token',
            "authorize_url": 'https://www.facebook.com/dialog/oauth',
            "_info_endpoint": "/me"
        },
        "github": {},
        "linkedin": {},
        "dropbox": {},
        "twitter": {}
    }
    """

    app.add_quokka_url_rule('/accounts/oauth/login/<provider>/', 'oauth_login',
                            oauth_login)

    config = app.config.get("OAUTH")
    if not config:
        return

    oauth = OAuth(app)

    for provider, data in config.items():
        provider_name = "oauth_" + provider
        oauth_app = oauth.remote_app(
            provider,
            **{k: v
               for k, v in data.items() if not k.startswith("_")})

        token_var = "oauth_{}_token".format(provider)
        oauth_app.tokengetter(lambda: session.get(token_var))

        setattr(app, provider_name, oauth_app)

        app.add_quokka_url_rule(
            '/accounts/oauth/authorized/{0}/'.format(provider),
            '{0}_authorized'.format(provider),
            oauth_app.authorized_handler(make_oauth_handler(provider)))

        if provider == 'linkedin':

            def change_linkedin_query(uri, headers, body):
                auth = headers.pop('Authorization')
                headers['x-li-format'] = 'json'
                if auth:
                    auth = auth.replace('Bearer', '').strip()
                    if '?' in uri:
                        uri += '&oauth2_access_token=' + auth
                    else:
                        uri += '?oauth2_access_token=' + auth
                return uri, headers, body

            oauth_app.pre_request = change_linkedin_query
예제 #5
0
# -*- coding: utf-8 -*-
from flask import Flask

# flask-SQLAlchemy
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy()
# flask-oauth
from flask.ext.oauthlib.client import OAuth
oauth = OAuth()
# flask-login
from flask.ext.login import LoginManager
login_manager = LoginManager()
login_manager.session_protection = 'strong'
login_manager.login_view = 'user.login'
# flask-mail
from flask.ext.mail import Mail
mail = Mail()


# app factory class
class App(object):
    def __init__(self, **kwargs):
        self.app = Flask(__name__, instance_relative_config=False)
        # config -- public config
        # from phzblog import config
        # self.app.config.from_object(config)
        self.app.config.from_object('config')
        # instance/config.py -- private config
        self.app.config.from_pyfile('../instance/config.py')
        # config/xxx.py -- scence config
        # self.app.config.from_envvar('APP_CONFIG_FILE') # APP_CONFIG_FILE defined in start.sh
예제 #6
0
# coding: utf-8

import os
from flask import Flask
from flask import session

from flask.ext.oauthlib.client import OAuth

from piko import App
app = App('piko')

oauth = OAuth(app)


def twitter():
    if app.config.get('TWITTER_API_KEY', False):
        twitter = oauth.remote_app(
            'twitter',
            register=False,
            consumer_key=app.config.get('TWITTER_API_KEY', None),
            consumer_secret=app.config.get('TWITTER_API_SECRET', None),
            base_url='https://api.twitter.com/1.1/',
            request_token_url='https://api.twitter.com/oauth/request_token',
            access_token_url='https://api.twitter.com/oauth/access_token',
            authorize_url='https://api.twitter.com/oauth/authenticate',
        )

        @twitter.tokengetter
        def get_twitter_token():
            if 'twitter_oauth' in session:
                resp = session['twitter_oauth_token']
예제 #7
0
from flask import json

import midauth.models.user
from midauth.utils import importlib
from . import defaults
from . import dispatch
from .ext import OAuth2Provider


login_manager = LoginManager()
login_manager.anonymous_user = midauth.models.user.AnonymousUser

current_user = flask.ext.login.current_user

oauth2 = OAuth2Provider()
oauth_client = OAuth()


def create_app(config):
    app = flask.Flask('midauth.web')
    app.config.from_object(defaults)
    if isinstance(config, collections.Mapping):
        app.config.update(config)
    else:
        app.config.from_pyfile(config)
    login_manager.init_app(app)
    oauth2.init_app(app)
    init_oauth_client(app)
    init_sqla_session(app, app.config['DATABASE_URL'])
    init_error_handlers(app)
    init_blueprints(app, app.config['BLUEPRINTS'])