Esempio n. 1
0
    def __init__(self,site,**args):
        """
        根据site名称和accesstoken相关的参数来初始化这个Service
        """

        #检验site名是否合法
        safe_site(site)

        self.site = site
        
        #这2个参数必须有
        if ('access_token' in args) and ('expires_in' in args):
            self.access_token = args['access_token']
            self.expires_in = args['expires_in']
        else:
            raise Exception("没有给定access_token和expires_in参数")
        #以下参数可能不一定有
        if 'refresh_token' in args:
            self.refresh_token = args['refresh_token']
        else:
            self.refresh_token = ""
        if 'openid' in args:
            self.openid = args['openid']
        else:
            self.openid = ""
        if 'name' in args:
            self.name = args['name']
        else:
            self.name = ""
        if 'nick' in args:
            self.nick = args['nick']
        else:
            self.nick = ""
Esempio n. 2
0
    def addToken(self, site, **args):
        """
        添加Token,即绑定一个AccessToken,如果该site已经被绑定,
        则更新accesstoken信息
        """

        #检查site名是否合法,如果不合法,将抛出异常
        safe_site(site)

        #这2个参数必须有
        if ('access_token' in args) and ('expires_in' in args):
            access_token = args['access_token']
            expires_in = args['expires_in']
        else:
            raise Exception("没有给定access_token和expires_in参数")
        #以下参数可能不一定有
        if 'refresh_token' in args:
            refresh_token = args['refresh_token']
        else:
            refresh_token = ""
        if 'openid' in args:
            openid = args['openid']
        else:
            openid = ""

        #这段代码是为了防止同一个token被多个账号绑定
        try:
            #尝试获取这个token
            t = Token.objects.get(access_token=access_token,site=site)
        except:
            pass
        else:
            #如果该token已经被其他账号绑定,先解除其他账号上的绑定,再绑定到该账号上
            if t.user.username != self.user.username:
                t.delete()

        oauth = self.get_open_auth(site=site)
        #如果已经存在该openAuth,则更新
        if oauth:
            oauth.access_token = access_token
            oauth.refresh_token = refresh_token
            oauth.expires_in = expires_in
            oauth.openid = openid
            oauth.save()
        #如果不存在这个openAuth,则继续
        else:
            user = self.user
            from accounts.platform.config import site_config
            user.token_set.create(
                site=site,
                site_name=[n['site_name'] for n in site_config if n['site'] == site][0],
                access_token=access_token,
                refresh_token=refresh_token,
                expires_in=expires_in,
                openid=openid,
            )
            user.save()
Esempio n. 3
0
    def refreshToken(self, site):
        """
        通过refreshToken,刷新一个accessToken,因为accessToken是会过期的.
        refreshToken在OpenAuthModel里面有
        """

        #检查site名是否合法,如果不合法,将抛出异常
        safe_site(site)

        #TODO 有些平台可能没有refreshToken这个概念,所以这个以后再做打算

        pass
Esempio n. 4
0
    def deleteToken(self, site):
        """
        删除一个accessToken,直接从数据库中删除
        """

        #检查site名是否合法,如果不合法,将抛出异常
        safe_site(site)

        oauth = self.get_open_auth(site)
        if oauth:
            oauth.delete()
        else:
            pass
Esempio n. 5
0
    def setTokenEnable(self, site, enable=True):
        """
        设置对应site的accessToken是否有效,
        传入enable默认为True,即为有效
        """

        #检查site名是否合法,如果不合法,将抛出异常
        safe_site(site)

        oauth = self.get_open_auth(site)
        if oauth:
            oauth.enable = enable
            oauth.save()
        else:
            pass
Esempio n. 6
0
    def get_open_auth(self, site, username=None):
        """
        获取用户在平台上的token信息
        如果是,返回openauth对象,否则返回None
        """

        #检查site名是否合法,如果不合法,将抛出异常
        safe_site(site)

        if username:
            try:
                user = User.objects.get(username)
            except ObjectDoesNotExist:
                raise Exception("用户名为%s的账户不存在" % username)
        else:
            user = self.user
        try:
            oauth = Token.objects.get(user=user, site=site)
        except ObjectDoesNotExist:
            return None
        else:
            return oauth