Example #1
0
def app(tmpdir):
    app = Flask(__name__)
    app.debug = True
    shutil.copy("tests/testdata/resource.json", str(tmpdir))
    shutil.copy("tests/testdata/permission.json", str(tmpdir))
    app.config["API_RESOURCE_JSON"] = join(str(tmpdir), "resource.json")
    app.config["API_PERMISSION_JSON"] = join(str(tmpdir), "permission.json")

    def fn_user_role(token):
        user_id = token["id"]
        user_roles = ["访客", "普通用户", "管理员"]
        return user_roles[user_id]

    api = Api(app)
    auth = Auth(api, fn_user_role=fn_user_role)

    class User(Resource):

        schema_inputs = {"post": {"id": "int(0,2)&required"}}

        def get(self):
            return "ok"

        def post(self, id):
            return "ok", auth.gen_header({"id": id})

    api.add_resource(User)
    api.add_resource(Permission, auth=auth)
    app.api = api
    app.auth = auth
    return app
Example #2
0
def create_app():
    app = Flask(__name__)
    app.config.update(
        DEBUG=True,
        SECRET_KEY='supersecret',
        DATABASE={
            'name': 'example.db',
            'engine': 'peewee.SqliteDatabase',
        },
    )
    app.db = Database(app)
    Feedloggr(app, app.db)

    # OPTIONALLY SETUP BEGINS
    # Simple authentication for the admin interface
    app.auth = Auth(app, app.db)
    app.auth.User.create_table(fail_silently=True)
    # Try to create a new admin user, but fail silently if it already exists
    try:
        user = app.auth.User.create(
            username='admin',
            email='.',
            password='',
            admin=True,
            active=True,
       )
    except PIE:
        pass
    else:
        user.set_password('admin')
        user.save()
    # Initialize the admin interface
    app.admin = Admin(app, app.auth)
    app.auth.register_admin(app.admin)
    # Register the feedloggr feeds model
    app.admin.register(feedloggr_Feeds, feedloggr_FeedsAdmin)
    app.admin.setup()
    # OPTIONALLY SETUP ENDS

    return app
Example #3
0
from flask import Flask, jsonify
from oauth1.authorize import Oauth1
from oauth1.errors.oauth import Oauth1Errors
from oauth1.store.sql import Oauth1StoreSQLAlchemy

BASE_URL = "http://localhost:5000/"
app = Flask(__name__)
app.debug = True
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://root:@127.0.0.1:3306/oauth"    # Change this to a valid URI
app.auth = None


class SQLProvider(Oauth1):

    def __init__(self):
        store = Oauth1StoreSQLAlchemy(app=app)
        super(SQLProvider, self).__init__(base_url=BASE_URL, store=store)

    def _verify_xauth_credentials(self, username, password):
        return username == 'username' and password == 'password'


@app.before_first_request
def after_run():
    global app
    app.auth = SQLProvider()
    oauth_app = app.auth.store.create_new_consumer_app(app_name='Test App %d' % Oauth1StoreSQLAlchemy.get_unix_time(),
                                                       app_desc='Just Testing', app_platform='CLI', app_url=BASE_URL)
    print "OAuth App:", oauth_app

    tokens = app.auth.store.create_new_consumer_tokens(app_id=oauth_app['app_id'])
Example #4
0
import config

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from finance.utils import Auth

app = Flask(__name__)
app.config.from_object(config)
app.auth = Auth()

db = SQLAlchemy(app)

import finance.views.base  # noqa
Example #5
0
from functools import wraps
from flask import (g, request, Response, Flask, jsonify)
from flask.ext.httpauth import HTTPBasicAuth
from models.base import db
from services.cyclopedia_service import CyclopediaService
from services.user_service import UserService
from services.entry_service import EntryService
from services.authentication_service import AuthenticationService
from presenters.user_presenter import UserPresenter
from presenters.cyclopedia_presenter import CyclopediaPresenter
from presenters.entry_presenter import EntryPresenter

app = Flask(__name__)
app.config.from_object('config.DevelopmentConfig')
app.auth = HTTPBasicAuth()
db.init_app(app)


def authenticate():
    """Sends a 401 response that enables basic auth"""
    return Response('Could not verify your access level for that URL.\n'
                    'You have to login with proper credentials', 401,
                    {'WWW-Authenticate': 'Basic realm="Login Required"'})


def require_apikey(fn):
    @wraps(fn)
    def _wrap(*args, **kwargs):
        auth = request.authorization

        if not auth: