예제 #1
0
    def post(self):
        print(request.form)
        grant_type = request.form.get('grant_type')
        username = request.form.get('username')
        password = request.form.get('password')

        if grant_type is None or grant_type.lower() != 'password':
            return api_abort(code=400,
                             message='The grant type must be password.')

        user = User.query.filter_by(username=username).first()
        if user is None or not user.validate_password(password):
            return api_abort(
                code=400,
                message='Either the username or password was invalid.')

        token, expiration = generate_token(user)

        response = jsonify({
            'access_token': token,
            'token_type': 'Bearer',
            'expires_in': expiration  #过期时间
        })
        response.headers['Cache-Control'] = 'no-store'
        response.headers['Pragma'] = 'no-cache'
        return response
예제 #2
0
 def delete(self, item_id):
     item = Item.query.get_or_404(item_id)
     if g.current_user != item.author:
         return api_abort(403)
     db.session.delete(item)
     db.session.commit()
     return '', 204
예제 #3
0
 def patch(self, item_id):
     item = Item.query.get_or_404(item_id)
     if g.current_user != item.author:
         return api_abort(403)
     item.done = True
     db.session.commit()
     return '', 204
예제 #4
0
 def get(self, item_id):
     """Get item."""
     item = Item.query.get_or_404(item_id)
     if g.current_user != item.author:
         return api_abort(403)
     # 使用模式函数获取资源字典,并传入对用的模型类实例作为参数,
     # 调用jsonify()方法将资源字典对象转换为标准的JSON数据,它会为响应报文设置正确的Content-Type字段(即'application/json')
     return jsonify(item_schema(item))
예제 #5
0
 def decorated(*args, **kwargs):
     token_type, token = get_token()
     if request.method != 'OPTIONS':
         if token_type is None or token_type.lower() != 'bearer':
             return api_abort(400, 'The token type must be bearer.')
         if token is None:
             return token_missing()
         if not validate_token(token):
             return invalid_token()
     return f(*args, **kwargs)
예제 #6
0
 def decorated(*args, **kwargs):
     token_type, token = get_token()
     # Flask normally handles OPTIONS requests on its own, but in the
     # case it is configured to forward those to the application, we
     # need to ignore authentication headers and let the request through
     # to avoid unwanted interactions with CORS.
     if request.method != 'OPTIONS':
         if token_type is None or token_type.lower() != 'bearer':
             return api_abort(400, 'The token type must be bearer!')
         if token is None:
             return token_missing()
         if not validate_token(token):
             return invalid_token()
     return f(*args, **kwargs)
예제 #7
0
 def get(self, category_id):
     category = Category.query.get_or_404(category_id)
     if g.current_user != category.author:
         return api_abort(403)
     return jsonify(category_schema(category))