Exemplo n.º 1
0
def render(template_name_or_list, **context):
    ui = ""
    if current_user and current_user.is_authenticated():
        ui = " for user '%s'" % current_user.name
    log.debug("rendering template '%s' %s" % (template_name_or_list, ui))
    return render_template(
        template_name_or_list, meta_content=__oauth_meta_content(), oauth_api_key=__oauth_api_key(), **context
    )
Exemplo n.º 2
0
def render(template_name_or_list, **context):
    ui = ""
    if current_user and current_user.is_authenticated():
        ui = " for user '%s'" % current_user.name
    log.debug("rendering template '%s' %s" % (template_name_or_list, ui))
    return render_template(template_name_or_list,
                           meta_content=__oauth_meta_content(),
                           oauth_api_key=__oauth_api_key(),
                           **context)
Exemplo n.º 3
0
    def login(self, args):
        code = args.get('code')
        if not code:
            return None

        log.info('login from alauda, code = ' + code)

        # basic auth header, content_type and post data
        client_id = get_config("login.alauda.client_id")
        client_secret = get_config("login.alauda.client_secret")
        basic_auth = HTTPBasicAuth(client_id, client_secret)

        headers = {'Content-type': 'application/x-www-form-urlencoded'}
        data = {
            'grant_type': 'authorization_code',
            'code': code,
            'redirect_uri': get_config('login.alauda.redirect_uri'),
            'client_id': client_id
        }

        # Post request
        url = get_config('login.alauda.access_token_url')
        r = requests.post(url, data=data, headers=headers, auth=basic_auth)
        resp = r.json()

        # {u'username': u'junbowang', u'realname': u'junbowang', u'success': True,
        # u'access_token': u'3MyZLF8RVo5X8lDLZQSj5s4OpIGQGn', u'token_type': u'Bearer',
        # u'logo_file': u'/static/images/user/default-logo.png', u'email': u'*****@*****.**'}
        if not resp.get("success"):
            log.debug("get access token failed from alauda: %r" % resp)
            return None

        # username will used as openid too since its unique. And also it's the 'namespace' for user's alauda resource
        username = resp["username"]
        email = resp["email"]
        email_list = [
            {
                'name': username,
                'email': email,
                'verified': 1,
                'primary': 1
            }
        ]

        user_with_token = user_manager.oauth_db_login(username,
                                                      provider=LOGIN_PROVIDER.ALAUDA,
                                                      name=username,
                                                      nickname=resp.get("realname", username),
                                                      access_token=resp["access_token"],
                                                      email_list=email_list,
                                                      avatar_url=resp.get("logo_file"))

        # for oxford only
        self.oxford(user_with_token["user"], resp.get("oxford_api"))

        return user_with_token
Exemplo n.º 4
0
    def login(self, args):
        code = args.get('code')
        if not code:
            return None

        log.info('login from alauda, code = ' + code)

        # basic auth header, content_type and post data
        client_id = get_config("login.alauda.client_id")
        client_secret = get_config("login.alauda.client_secret")
        basic_auth = HTTPBasicAuth(client_id, client_secret)

        headers = {'Content-type': 'application/x-www-form-urlencoded'}
        data = {
            'grant_type': 'authorization_code',
            'code': code,
            'redirect_uri': get_config('login.alauda.redirect_uri'),
            'client_id': client_id
        }

        # Post request
        url = get_config('login.alauda.access_token_url')
        r = requests.post(url, data=data, headers=headers, auth=basic_auth)
        resp = r.json()

        # {u'username': u'junbowang', u'realname': u'junbowang', u'success': True,
        # u'access_token': u'3MyZLF8RVo5X8lDLZQSj5s4OpIGQGn', u'token_type': u'Bearer',
        # u'logo_file': u'/static/images/user/default-logo.png', u'email': u'*****@*****.**'}
        if not resp.get("success"):
            log.debug("get access token failed from alauda: %r" % resp)
            return None

        # username will used as openid too since its unique. And also it's the 'namespace' for user's alauda resource
        username = resp["username"]
        email = resp["email"]
        email_list = [
            {
                'name': username,
                'email': email,
                'verified': 1,
                'primary': 1
            }
        ]

        required_info = {
            "openid": username,
            "provider": LOGIN_PROVIDER.ALAUDA,
            "name": username,
            "nickname": resp.get("realname", username),
            "access_token": resp["access_token"],
            "email_list": email_list,
            "avatar_url": resp.get("logo_file"),
            "oxford_api": resp.get("oxford_api")
        }

        return required_info
Exemplo n.º 5
0
    def login(self, args):
        code = args.get("code")
        if not code:
            return None

        log.info("login from alauda, code = " + code)

        # basic auth header, content_type and post data
        client_id = get_config("login.alauda.client_id")
        client_secret = get_config("login.alauda.client_secret")
        basic_auth = HTTPBasicAuth(client_id, client_secret)

        headers = {"Content-type": "application/x-www-form-urlencoded"}
        data = {
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": get_config("login.alauda.redirect_uri"),
            "client_id": client_id,
        }

        # Post request
        url = get_config("login.alauda.access_token_url")
        r = requests.post(url, data=data, headers=headers, auth=basic_auth)
        resp = r.json()

        # {u'username': u'junbowang', u'realname': u'junbowang', u'success': True,
        # u'access_token': u'3MyZLF8RVo5X8lDLZQSj5s4OpIGQGn', u'token_type': u'Bearer',
        # u'logo_file': u'/static/images/user/default-logo.png', u'email': u'*****@*****.**'}
        if not resp.get("success"):
            log.debug("get access token failed from alauda: %r" % resp)
            return None

        # username will used as openid too since its unique. And also it's the 'namespace' for user's alauda resource
        username = resp["username"]
        email = resp["email"]
        email_list = [{"name": username, "email": email, "verified": 1, "primary": 1}]

        required_info = {
            "openid": username,
            "provider": LOGIN_PROVIDER.ALAUDA,
            "name": username,
            "nickname": resp.get("realname", username),
            "access_token": resp["access_token"],
            "email_list": email_list,
            "avatar_url": resp.get("logo_file"),
            "oxford_api": resp.get("oxford_api"),
        }

        return required_info
Exemplo n.º 6
0
 def __remote_login(self, data):
     try:
         req = requests.post(self.login_url,
                             json=data,
                             headers=self.headers)
         resp = req.json()
         if "error" in resp:
             log.debug("login failed: %r" % resp)
             return None
         else:
             login_user = User(resp["user"])
             token = resp["token"]
             return {"user": login_user, "token": token["token"]}
     except Exception as e:
         log.error(e)
         return None
Exemplo n.º 7
0
 def __remote_login(self, data):
     try:
         req = requests.post(self.login_url, json=data, headers=self.headers)
         resp = req.json()
         if "error" in resp:
             log.debug("login failed: %r" % resp)
             return None
         else:
             login_user = User(resp["user"])
             token = resp["token"]
             return {
                 "user": login_user,
                 "token": token["token"]
             }
     except Exception as e:
         log.error(e)
         return None
Exemplo n.º 8
0
 def authing(self, user_info):
     try:
         req = requests.post(self.authing_url,
                             json=user_info,
                             headers=self.headers)
         resp = req.json()
         if resp:
             # if login isn't successful, it will return None
             login_user = User(resp)
             log.debug("Login successfully %s" % login_user.get_user_id())
             return login_user
         else:
             log.debug("login failed: %r" % resp)
             return None
     except Exception as e:
         log.error(e)
         return None
Exemplo n.º 9
0
    def get_info(self, token):
        """ Get qq open id

        :type token: str
        :param token:

        :rtype: dict
        :return: info
        """

        openid_resp = get_remote(get_config("login.qq.openid_url") + token)
        log.debug("get access_token from qq:" + token)
        info = json.loads(openid_resp[10:-4])

        if info.get("error") is not None:
            raise Exception(info)

        return info
Exemplo n.º 10
0
    def get_info(self, token):
        """ Get qq open id

        :type token: str
        :param token:

        :rtype: dict
        :return: info
        """

        openid_resp = get_remote(get_config("login.qq.openid_url") + token)
        log.debug("get access_token from qq:" + token)
        info = json.loads(openid_resp[10:-4])

        if info.get("error") is not None:
            raise Exception(info)

        return info
Exemplo n.º 11
0
 def __remote_login(self, data):
     try:
         req = requests.post(self.login_url,
                             json=data,
                             headers=self.headers)
         resp = req.json()
         if resp:
             # if login isn't successful, it will return None
             login_user = User(resp["user"])
             token = resp["token"]
             log.debug("Login successfully %s" % login_user.get_user_id())
             return {"user": login_user, "token": token["token"]}
         else:
             log.debug("login failed: %r" % resp)
             return None
     except Exception as e:
         log.error(e)
         return None
Exemplo n.º 12
0
def render(template_name_or_list, **context):
    log.debug("rendering template '%s'" % template_name_or_list)
    return render_template(template_name_or_list,
                           meta_content=__oauth_meta_content(),
                           oauth_api_key=__oauth_api_key(),
                           **context)