示例#1
0
def user1():
    # return new user named one
    one = User(
        name="test_one",
        nickname="test_one",
        avatar_url="/static/pic/monkey-32-32px.png",
        is_super=False)
    one.set_password("test_password")
    one.save()
    return one
示例#2
0
def admin1():
    # return new admin named one
    admin_one = User(
        name="admin_one",
        nickname="admin_one",
        avatar_url="/static/pic/monkey-32-32px.png",
        is_super=True)
    admin_one.set_password("test_password")
    admin_one.save()
    return admin_one
示例#3
0
def user2():
    # return new user named two
    two = User(
        name="test_two",
        nickname="test_two",
        avatar_url="/static/pic/monkey-32-32px.png",
        is_super=False)
    two.set_password("test_password")
    two.save()
    return two
示例#4
0
    def __oauth_login(self, provider, context):
        # update db
        email_list = context.get("email_list", [])
        openid = context.openid

        user = self.__get_existing_user(openid, provider)
        if user is not None:
            user.update(
                provider=provider,
                name=context.get("name", user.name),
                nickname=context.get("nickname", user.nickname),
                access_token=context.get("access_token", user.access_token),
                avatar_url=context.get("avatar_url", user.avatar_url),
                last_login_time=self.util.get_now(),
                login_times=user.login_times + 1,
                online=True,
            )
            map(lambda x: self.__create_or_update_email(user, x), email_list)
        else:
            user = User(
                openid=openid,
                name=context.name,
                provider=provider,
                nickname=context.nickname,
                access_token=context.access_token,
                avatar_url=context.get("avatar_url", ""),
                login_times=1,
                online=True,
            )

            try:
                user.save()
            except ValidationError as e:
                self.log.error(e)
                return internal_server_error("create user fail.")

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

        # oxford only
        if provider == "alauda":
            self.__oxford(user, context.get("oxford_api"))

        # generate API token
        token = self.__generate_api_token(user)
        return {"token": token.dic(), "user": user.dic()}
示例#5
0
    def __oauth_login(self, provider, context):
        # update db
        email_list = context.get('email_list', [])
        openid = context.openid

        user = self.__get_existing_user(openid, provider)
        if user is not None:
            user.update(
                provider=provider,
                name=context.get("name", user.name),
                nickname=context.get("nickname", user.nickname),
                access_token=context.get("access_token", user.access_token),
                avatar_url=context.get("avatar_url", user.avatar_url),
                last_login_time=self.util.get_now(),
                login_times=user.login_times + 1,
                online=True)
            map(lambda x: self.__create_or_update_email(user, x), email_list)
        else:
            user = User(openid=openid,
                        name=context.name,
                        provider=provider,
                        nickname=context.nickname,
                        access_token=context.access_token,
                        avatar_url=context.get("avatar_url", ""),
                        login_times=1,
                        online=True)

            try:
                user.save()
            except ValidationError as e:
                self.log.error(e)
                return internal_server_error("create user fail.")

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

        # oxford only
        if provider == "alauda":
            self.__oxford(user, context.get("oxford_api"))

        # generate API token
        token = self.__generate_api_token(user)
        return {
            "token": token.dic(),
            "user": user.dic()}
示例#6
0
    def authing(self, context):
        token = context.token
        username = context.username

        if not token or not username:
            self.log.info(
                "Unable to handle authing login request. Either token or username is empty. username: "******"Unable to handle authing login request. Either token or username is empty"
            )

        # validate access token
        self.log.info("Validate authing token for user %s" % username)
        validate_url = get_config("login.authing.validate_token_url") + token
        validate_raw_resp = get_remote(validate_url)
        validate_resp = json.loads(validate_raw_resp)

        if int(validate_resp["code"]) != 200 or not bool(
                validate_resp["status"]):
            self.log.info("Token invalid: %s" % validate_raw_resp)
            return unauthorized("Token invalid: %s" % validate_raw_resp)

        authing_id = context._id
        open_id = context.unionid
        provider = context.registerMethod
        if "oauth" in provider:
            # OAuth like github. registerMethod example: "oauth:github"
            provider = provider[6:]
        else:
            # Authing user: using authing_id as open_id
            open_id = authing_id

        email_list = [{
            "email": context.get("email", ""),
            "primary": True,
            "verified": bool(context.get("emailVerified", False))
        }]

        user = self.__get_existing_user(open_id, provider)
        if user is not None:
            nickname = context.get("nickname", user.nickname)
            if not nickname:
                nickname = user.name
            user.update(name=context.get("username", user.name),
                        nickname=nickname,
                        access_token=context.get("token", user.access_token),
                        avatar_url=context.get("photo", user.avatar_url),
                        authing_id=authing_id,
                        last_login_time=self.util.get_now(),
                        login_times=user.login_times + 1,
                        online=True)
            list(
                map(lambda x: self.__create_or_update_email(user, x),
                    email_list))
        else:
            user = User(openid=open_id,
                        name=username,
                        provider=provider,
                        authing_id=authing_id,
                        nickname=context.nickname,
                        access_token=token,
                        avatar_url=context.get("photo", ""),
                        login_times=int(context.get("loginsCount", "1")),
                        online=True)

            try:
                user.save()
            except ValidationError as e:
                self.log.error(e)
                return internal_server_error("create user fail.")

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

        # save API token
        token_expire_date = self.util.get_now() + timedelta(hours=1)
        if "tokenExpiredAt" in context:
            try:
                token_expire_date = datetime.strptime(
                    context.tokenExpiredAt, '%a %b %d %Y %H:%M:%S GMT%z (CST)')
            except Exception as e:
                self.log.warn(
                    "Unable to parse tokenExpiredAt: %s. Will use 1 hour as expiry."
                    % context.tokenExpiredAt)
        else:
            self.log.info(
                "tokenExpiredAt not included in authing response. Will use 1 hour as expiry."
            )

        user_token = UserToken(token=token,
                               user=user,
                               expire_date=token_expire_date)
        user_token.save()
        # resp = {
        #     "token": user_token.dic(),
        #     "user": user.dic()
        # }
        resp = context.to_dict()
        resp.update(user.dic())
        return resp