2016-05-12 11 views
4

Flask REST APIにトークンベースの認証を実装しようとしています。私は第三者の認証サービスとしてStormpathを使用しています。Flask-Stormpathトークンベースの認証

の上に構築されたflask-stormpathを調べました。サーバー上でセッションを維持しようとしているときにパスワードベースの認証を使用するように見えます。また、ドキュメントは私に十分な情報を提供していません。

ストームパストークンベースの認証のためのフラスコインテグレーションはありますか? 「はい」の場合は、サンプルコードを指すことができます。

すでにサーバーにセッションを維持しているgithubのstormpath/flask-stormpath-sampleを使っています。

参考文献:

https://stormpath.com

https://github.com/stormpath/stormpath-flask

答えて

3

これは私が現在rdeggesまで使用している方法ですので、flask-stormpathにこの機能を組み込む必要があります。

stormpath python sdkの最新バージョンが必要で、funcツールからラップします。

from stormpath.api_auth import (PasswordGrantAuthenticator, RefreshGrantAuthenticator, JwtAuthenticator) 
from functools import wraps 

このようにアプリケーションを作成できます。

stormpathClient = Client(id=KEYS['STORMPATH_ID'], secret=KEYS['STORMPATH_SECRET']) 
stormpathApp = stormpathClient.applications.search('your-application')[0] 

このデコレータは、エンドポイントの保護に役立ちます。

def tokenRequired(func): 
    """ 
     Decorator to apply on all routes which require tokens. 
    """ 

    @wraps(func) 
    def wrappingFunc(): 
     #check the auth header of the request for a bearer token. 
     authHeader = request.headers.get('Authentication') 

     #make sure that the string is a bearer type. 
     if len(authHeader)<8 or (not authHeader[:7] == 'Bearer ') or (
       not authHeader): 
      return Response("401 Unauthorized",401) 
     authToken = authHeader[7:] 

     try: 
      authenticator = JwtAuthenticator(stormpathApp) 
      authResult = authenticator.authenticate(authToken) 
      request.vUser = authResult.account 
     except: 
      return Response("403 Forbidden",403) 

     return func() 

    return wrappingFunc 

#Use this decorator like below. 

@flaskApp.route('/secure-route',methods=['GET','POST']) 
@tokenRequired 
def secureEndpoint(): 

    # return JSON based response 
    return Response("This is secure Mr." + request.vUser.given_name ,200) 

誰かがトークンの発行と最新表示のエンドポイントも知りたいと思っているのかどうか、私に知らせてください。

2

私はフラスコ-Stormpathライブラリの作者です。答えはいいえだ。私は実際にこの機能をデフォルトで提供するライブラリーの新しいリリース(1ヶ月程度)に取り組んでいますが、今はセッションベースの認証のみをサポートしています。

+0

現在サポートを提供していますか? (4年後) –

+0

申し訳ありません私はちょうどそれが12日前だったと見ました... – WebQube

+0

@rdegges、私は現在(以下の答えで)使用してコメントをしている一時的な方法を見てくださいできますか? – Vasif

関連する問題