处理对谷歌App Engine的会话与Android / IPhone [英] Handling Sessions on Google App Engine with Android/IPhone

查看:217
本文介绍了处理对谷歌App Engine的会话与Android / IPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始编写一个应用程序,从而在移动应用程序(安卓/ IPhone)将与GAE后台(蟒蛇)通过一系列使用JSON的Web API调用通信​​。

我无法使用谷歌帐户进行身份验证,所以我需要实现我自己的身份验证。我有一个如何做到这一点的想法,但我不知道是否有更好的方法。

谁能帮助与如何实现一些code例子/建议下面好吗?

方法

  1. 在移动应用程序调用该验证,并创建在店里一个会话密钥,并返回该到应用服务器上的登录方法 - 的不知道如何生成密钥/会话或者在请求/响应它应该可以。
  2. 在每次调用,应用程序通过这个密钥服务器进行身份验证,并允许行动,如果它通过。
  3. 在用户不应该登录在移动再次,除非他们明确注销或丢失了密钥。

登录方法 - 无密钥生成

 类登录(webapp.RequestHandler):
高清后期(个体经营):
    的args = json.loads(self.request.body)
    电子邮件=的args ['E']
    PWD =的args ['P']
    RET = {}
    用户= User.gql('WHERE电子邮件= 1',电子邮件)获得()
    如果用户和helpers.check_password(PWD,user.password的):
        RET ['ret_ code'] = 0
        保留['的dn'] = user.display_name
    其他:
        RET ['ret_ code'] = 1

    self.response.headers ['的Content-Type] ='应用程序/ JSON
    self.response.out.write(json.dumps(RET))
 

解决方案

我认为你应该使用webapp2提供来实现自定义的注册功能。

 从webapp2_extras进口身份验证
从google.appengine.api导入用户

类RegisterHandler(webapp2.RequestHandler):
    高清后期(个体经营):
        电子邮件= self.request.POST [电子邮件]
        密码= self.request.POST ['密码']

        #Let webapp2办理注册和管理会议
        用户= auth.get_auth()store.user_model.create_user。(自己的:+ STR(电子邮件),password_raw =密码,电子邮件=电子邮件)
        #user(诚然,用户(键=键(用户,80001),auth_ids = [u'own:useremail@mail.com'],电子邮件='USEREMAIL @ mail.com,密码= u'hashed_pa​​ssword', ...))

        如果没有用户的[0]:#user是一个元组
            self.response.write(用户[1])#错误信息
        其他:
            #你可以扩展你的用户模型,例如用户配置(用户):或有一个UserProperty在您的个人资料模型的例子。
            简介=用户配置(用户= users.User(用户[1] .email))。把()

            self.response.write(STR(profile.key()))
类LoginHandler(webapp2.RequestHandler):

    高清后期(个体经营):

        电子邮件= self.request.POST.get(电子邮件)
        电子邮件= self.request.POST.get('密码')
        #尝试登录用户密码
        #引发InvalidAuthIdError如果未找到用户
        #引发InvalidPasswordError如果提供的密码不符合规定的用户匹配
        尝试:
            auth.get_auth()get_user_by_password(自己的:+电子邮件,密码)。
            #Return USER_SESSION使用用户ID,
        除了InvalidPasswordError,InvalidAuthIdError:
            #错误
 

您可以检查用户通过登录:

 如果auth.get_user_by_session():
    #登录
其他:
    #未登录
 

在您的客户端应用程序(安卓,IOS)。你只需要存储响应cookie,并为每个子序列请求发送。

祝你好运:)

I'm starting to write an app whereby a mobile app (Android/IPhone) will communicate with the GAE backend (Python) through a series of Web API calls using JSON.

I can't use Google Accounts for authentication so I need to implement my own auth. I have an idea of how to do this, but I'm not sure if there is a better way.

Can anyone help with some code examples/suggestions of how to achieve the below please?

Method

  1. Mobile app calls a Login method on the server which authenticates and creates a session key in the store and returns this to the app - not sure how to generate the key/session or where on the request/response it should be.
  2. On every call, the app passes this key for the server to authenticate and allows the action if it passes.
  3. User should not have to login on mobile again unless they explicitly logout or lose the key.

Login Method - without key generation

class Login(webapp.RequestHandler):
def post(self):
    args = json.loads(self.request.body)
    email = args['e']
    pwd = args['p']
    ret = {}
    user = User.gql('WHERE email = :1', email).get()
    if user and helpers.check_password(pwd, user.password):
        ret['ret_code'] = 0
        ret['dn'] = user.display_name
    else:
        ret['ret_code'] = 1

    self.response.headers['Content-Type'] = 'application/json'
    self.response.out.write(json.dumps(ret))

解决方案

I think you should use features webapp2 providing to implement your custom registration.

from webapp2_extras import auth
from google.appengine.api import users

class RegisterHandler(webapp2.RequestHandler):
    def post(self):
        email=self.request.POST['email']
        password=self.request.POST['password']

        #Let webapp2 handle register and manage session
        user = auth.get_auth().store.user_model.create_user('own:'+str(email), password_raw=password,email=email)
        #user (True, User(key=Key('User', 80001), auth_ids=[u'own:useremail@mail.com'],email='useremail@mail.com',password=u'hashed_password',...))

        if not user[0]: #user is a tuple
            self.response.write(user[1]) # Error message
        else:
            #You can extend your User Model e.g UserProfile(User): or have a UserProperty in your profile model as the example.  
            profile=UserProfile(user=users.User(user[1].email)).put()

            self.response.write(str(profile.key()))
class LoginHandler(webapp2.RequestHandler): 

    def post(self):

        email = self.request.POST.get('email')
        email = self.request.POST.get('password')
        # Try to login user with password
        # Raises InvalidAuthIdError if user is not found
        # Raises InvalidPasswordError if provided password doesn't match with specified user
        try:
            auth.get_auth().get_user_by_password('own:'+email, password)
            #Return user_session with User id, 
        except InvalidPasswordError, InvalidAuthIdError:
            #Error

You can check user logged in by:

if auth.get_user_by_session():
    #Logged in      
else:
    #Not logged in

On your client application(Android, IOS). You only have to store the response cookie and send it for every sub sequence requests.

Good luck :)

这篇关于处理对谷歌App Engine的会话与Android / IPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆