Exemplo n.º 1
0
    def oauth_db_login(self, openid, **kwargs):
        # update db
        email_list = kwargs['email_list']
        provider = kwargs["provider"]
        user = self.__get_existing_user(openid, provider)
        if user is not None:
            self.db.update_object(user,
                                  provider=provider,
                                  name=kwargs["name"],
                                  nickname=kwargs["nickname"],
                                  access_token=kwargs["access_token"],
                                  avatar_url=kwargs["avatar_url"],
                                  last_login_time=get_now(),
                                  login_times=user.login_times + 1,
                                  online=1)
            map(lambda x: self.__create_or_update_email(user, x), email_list)
        else:
            user = User(openid=openid,
                        name=kwargs["name"],
                        provider=provider,
                        nickname=kwargs["nickname"],
                        access_token=kwargs["access_token"],
                        avatar_url=kwargs["avatar_url"],
                        login_times=1,
                        online=1)

            self.db.add_object(user)
            map(lambda x: self.__create_or_update_email(user, x), email_list)

        # generate API token
        token = self.__generate_api_token(user)
        return {
            "token": token,
            "user": user
        }
Exemplo n.º 2
0
    def oauth_db_login(self, openid, **kwargs):
        # update db
        email_list = kwargs["email_list"]
        admin = self.__get_existing_user(openid, email_list)
        if admin is not None:
            self.db.update_object(
                admin,
                provider=kwargs["provider"],
                name=kwargs["name"],
                nickname=kwargs["nickname"],
                access_token=kwargs["access_token"],
                avatar_url=kwargs["avatar_url"],
                last_login_time=get_now(),
                online=1,
            )
            map(lambda x: self.__create_or_update_email(admin, x), email_list)
        else:
            admin = User(
                openid=openid,
                name=kwargs["name"],
                provider=kwargs["provider"],
                nickname=kwargs["nickname"],
                access_token=kwargs["access_token"],
                avatar_url=kwargs["avatar_url"],
                online=1,
            )

            self.db.add_object(admin)
            map(lambda x: self.__create_or_update_email(admin, x), email_list)

        # generate API token
        token = self.__generate_api_token(admin)
        return {"token": token, "admin": admin}
Exemplo n.º 3
0
 def __generate_api_token(self, admin):
     token_issue_date = get_now()
     token_expire_date = token_issue_date + timedelta(
         minutes=safe_get_config("login/token_expiration_minutes", 1440))
     user_token = UserToken(token=str(uuid.uuid1()),
                            user=admin,
                            expire_date=token_expire_date,
                            issue_date=token_issue_date)
     self.db.add_object(user_token)
     return user_token
Exemplo n.º 4
0
 def get_now_serialized():
     return __date_serializer(get_now())
Exemplo n.º 5
0
 def __validate_token(self, token):
     t = self.db.find_first_object_by(UserToken, token=token)
     if t is not None and t.expire_date >= get_now():
         return t.user
     return None