Vinli平台的Tornado Web身份验证。

vinli_tornado_auth的Python项目详细描述


Vinli用于Tornado的平台身份验证包装器

安装

pip install vinli-tornado-auth

设置要求

https://dev.vin.li注册并创建应用程序

在应用程序中,创建一个webclient类型并注意 以下值:

  • vinli_client_id-应用程序客户端ID
  • vinli_client_secret-应用程序客户端机密
  • vinli_redirect_uri-要重定向到的有效url。例如:http://localhost:8000/auth/login

将这些值添加到应用程序settings

示例用法

import tornado.escape
import tornado.ioloop
import tornado.gen
import tornado.web

from vinli_tornado_auth.auth import VinliAuthLoginMixin

class LoginHandler(tornado.web.RequestHandler, VinliAuthLoginMixin):
    """
    Handle /auth/login
    """
    @tornado.gen.coroutine
    def get(self):
        code = self.get_argument('code', None)
        if not code:
            yield self.authorize_redirect(
                redirect_uri=self.settings['vinli_redirect_uri'],
                client_id=self.settings['vinli_client_id'],
                response_type='code'
            )
        else:
            access = yield self.get_authenticated_user(
                redirect_uri=self.settings['vinli_redirect_uri'],
                code=code
            )
            user = yield self.oauth2_request(
                self._OAUTH_USERINFO_URL,
                access_token=access['access_token']
            )
            self.set_secure_cookie(
                'user', tornado.escape.json_encode({
                    'user': user,
                    'token': access['access_token']
                })
            )
            self.redirect('/')


class IndexHandler(tornado.web.RequestHandler, VinliAuthLoginMixin):
    """
    Handle /
    """
    def get_current_user(self):
        user = self.get_secure_cookie('user')
        if not user:
            return None
        return tornado.escape.json_decode(user)

    @tornado.web.authenticated
    @tornado.gen.coroutine
    def get(self):
        devices = yield self.vinli_request(
            'platform', '/api/v1/devices',
            access_token=self.current_user.get('token')
        )
        self.write(devices)


class Application(tornado.web.Application):
    def __init__(self):
        settings = {
            'vinli_client_id': 'abc123',
            'vinli_client_secret': "shhhh it is secret",
            'vinli_redirect_uri': 'http://localhost:8000/auth/login',
            'cookie_secret': '12345',
        }
        urls = [
            (r'/', IndexHandler),
            (r'/auth/login', LoginHandler),
        ]
        super(Application, self).__init__(urls, **settings)


if __name__ == '__main__':
    app = Application()
    app.listen(8000)
    tornado.ioloop.IOLoop.instance().start()

发出经过身份验证的请求

使用vinli_request方法向 初始身份验证完成后的平台。

为设备获取行程

就像following example 从Vinli API文档中,设备ID的跳闸列表 fe4bbc20-cc90-11e3-8e05-f3abac5b6b58可以通过以下方式检索:

@tornado.web.authenticated
@tornado.gen.coroutine
def get(self):
    trips = yield self.vinli_request(
        'trips', '/api/v1/devices/fe4bbc20-cc90-11e3-8e05-f3abac5b6b58/trips',
        access_token=self.current_user.get('token')
    )
    self.write(trips)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
windows请帮助获取java。运行程序时发生lang.nullpointerexception   JPA标准中的错误:java。lang.IllegalArgumentException:此JPQLquery中没有名为“:inputMsgId_1_”的参数   java调度一个小程序,从ScheduledExecutorService开始   java点击AVD“创建设备”按钮抛出NullPointerException   缓存Java9WebStart会多次加载jar文件   使用java计算最小二乘   当代理关闭时,java kafka生产者不会抛出异常   我们什么时候以及为什么要在java中进行自定义序列化?   java使用GSON解析包含包含所需字符串的对象的对象数组   java如何使用BOBJ REST API实现分页?   java身份验证失败:用户的凭据已过期。CAS v4。2.   合并排序中的java无限循环?   java jackson xml将pojo子元素转换为字符串   tcp Java ObjectOutputStream重置错误   如何调用。bat文件,并使用java中的ProcessBuilder发送字符串   奇怪的Java数学结果   java如何在Android中禁用最近的活动按钮?   java刷下应用程序崩溃   使用Java收集所有VersionOne资产及其所有属性   Java打印:创建具有最小可接受边距的页面格式