如何从不同的蓝图访问flask auth令牌

时间:2018-01-01 18:23:09

标签: python jwt flask-restful flask-login

我有一个带有2个蓝图的烧瓶应用程序。身份验证蓝图和事件蓝图。这是他们的结构

protected void onStart() {
        super.onStart();
        GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
        databaseUsers = FirebaseDatabase.getInstance().getReference("users");
        if (account == null) {
            username.setText("Need to sign in");
            email.setText("");
            avatar.setImageResource(android.R.color.transparent);
        }
        else {
            personName = account.getDisplayName();
            personGivenName = account.getGivenName();
            personFamilyName = account.getFamilyName();
            personEmail = account.getEmail();
            personId = account.getId();
            personPhoto = account.getPhotoUrl();
            username.setText(account.getDisplayName());
            email.setText(account.getEmail());
            final String cleanEmail = email.getText().toString().replaceAll("\\.", ",");

            databaseUsers.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot snapshot) {
                        if (!snapshot.child(cleanEmail).exists()) {
                            mProgress.setMessage("LUL");
                            mProgress.show();
                            mStorage = FirebaseStorage.getInstance().getReference();


                            InputStream stream = null;
                            try {
                                stream = new FileInputStream("some path here");
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }

                            StorageReference filePath = mStorage.child("Avatar").child(personPhoto.getPath());
                            UploadTask uploadTask = filePath.putStream(stream);
                            uploadTask.addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception exception) {
                                    // Handle unsuccessful uploads
                                }
                            }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                                @Override
                                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                    Uri downloadUrl = taskSnapshot.getDownloadUrl();
                                    mProgress.dismiss();
                                }
                            });
                    }
                }
                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
            if(account.getPhotoUrl() != null){
                Picasso.with(this).load(account.getPhotoUrl()).into(avatar);
            }
            else {
                avatar.setImageResource(R.drawable.userdefault);
            }
        }
    }

auth_blueprint处理用户事件。登录,注册等我的问题是,如何在登录时访问auth_blueprint生成的令牌,并在events_blueprint中使用它来保护其他端点?

在auth_blueprint下的视图中,验证码为

`app/
    auth_blueprint/
            init.py
            views.py
            model.py
    events_blueprint/
            init.py
            model.py
            views.py 
`

当我尝试在events_blueprint下的视图中访问g时,我得到一个错误g没有属性用户

@authenticate.verify_password
def verify_password(username_or_token, password):
    # first try to authenticate by token
    user = models.User.verify_auth_token(username_or_token)
    if not user:
        # try to authenticate with username/password
        user = models.User.query.filter_by(username = username_or_token).first()
        if not user or not user.verify_password(password):
            return False
    g.user = user

    import pdb; pdb.set_trace() #debugger
    return True

#login user
@auth.route('/api/v1/auth/login')
@authenticate.login_required
def login():    
    token = g.user.generate_auth_token()
    return jsonify({ 'token': token.decode('ascii') })
    #return jsonify({'Welcome':'Login Okay'}),200 

PS:用于测试目的。我只对将用户对象存储在g中以及稍后从那里检索用户ID感兴趣。

我正在使用jwt进行基于令牌的身份验证。如果有任何需要澄清,请告诉我。谢谢

0 个答案:

没有答案