【问题标题】:how to get new access token with auth0 and refresh token on bitbucket如何使用 auth0 获取新的访问令牌并在 bitbucket 上刷新令牌
【发布时间】:2020-03-09 11:48:54
【问题描述】:

我正在尝试使用提供程序 Bitbucket 上的 Auth0 使用刷新令牌获取新的访问令牌。根据 Bitbucket 文档,客户端 client_id 和 secret 的刷新令牌用于获取新的访问令牌。

curl -X POST -u "client_id:secret"
      https://bitbucket.org/site/oauth2/access_token \
      -d grant_type=refresh_token -d refresh_token={refresh_token}

但似乎我应该使用 Auth0 作为代理。它在 Auth0 文档中说要使用刷新令牌(特定于用户)以及我的 Auth0 域和 Auth0 客户端 ID。将我的 Auth0 凭据与 Bitbucket 用户的刷新令牌混合在一起似乎有点奇怪。

https://auth0.com/docs/tokens/concepts/idp-access-tokens?_ga=2.56840622.505323938.1583403242-2086121138.1583403242#renew-tokens

这是我的流程:

(1) 设置 AUTH0 客户端信息

  • 设置 auth0 客户端 ID
  • 设置 auth0 客户端密码
  • 设置 auth0 域
  • 检索 auth0 客户端令牌

(2) 获取初始用户信息

在端点获取用户信息:

https://<auth0_domain>.auth0.com/api/v2/users/<user_id>

 

  • 这将包含用户的访问令牌
  • 这还将包含“用户”而不是 AUTH0 客户端的刷新令牌(我假设?)

(3) 尝试从刷新令牌中获取访问令牌

在此处使用刷新令牌获取访问令牌:

https://<auth0_domain>.auth0.com/oauth/token

值得注意的是,刷新令牌是给用户的,但客户端信息来自 AUTH0 客户端 - 对我来说没有意义。失败并出现此错误。

{u'error_description': u'Unauthorized', u'error': u'access_denied'}

它基本上失败了:

     

payload = { "client_id": self.auth0_client_id,
                            "client_secret": self.auth0_client_secret,
                            "refresh_token": kwargs["refresh_token"].decode("utf-8"),
                            "grant_type": "refresh_token" }

        _endpt = 'https://{}.auth0.com/oauth/token'.format(self.auth0_domain)
        headers = {'content-type': 'application/json'}

        req = requests.post(_endpt,
                            data=json.dumps(payload),
                            headers=headers)

下面是我的python类的全部代码。

#!/usr/bin/env python

    import json
    import os
    import requests
    import logging

    class Auth0Client(object):

        def __init__(self,**kwargs):

            self.classname = "Auth0Client"
            self.logger.logging.basicConfig(level=logging.INFO)

            # (1) set AUTH0 client info

            self.auth0_client_id = os.environ["AUTH0_CLIENT_ID"]
            self.auth0_client_secret = os.environ["AUTH0_CLIENT_SECRET"]
            self.auth0_domain = os.environ["AUTH0_DOMAIN"]
            self._set_auth0_client_token()

        def _set_auth0_client_token(self):

            payload = { "client_id": self.auth0_client_id,
                        "client_secret": self.auth0_client_secret,
                        "audience": "https://{}.auth0.com/api/v2/".format(self.auth0_domain),
                        "grant_type": "client_credentials" }

            api_endpoint = 'https://{}.auth0.com/oauth/token'.format(self.auth0_domain)

            req = requests.post(api_endpoint,
                                data=json.dumps(payload),
                                headers={'content-type': 'application/json'})

            self.auth0_client_token = req.json()["access_token"].decode("utf-8")

        def _get_userinfo_frm_auth0(self,user):

            endpoint = "https://{}.auth0.com/api/v2/users/{}".format(self.auth0_domain,user)
            headers = {'Authorization': 'Bearer %s' % self.auth0_client_token}
            req = requests.get(endpoint,headers=headers)

            return req.json()

        def _get_user_access_token_with_refresh(self,**kwargs):

            payload = { "client_id": self.auth0_client_id,
                        "client_secret": self.auth0_client_secret,
                        "refresh_token": kwargs["refresh_token"].decode("utf-8"),
                        "grant_type": "refresh_token" }

            _endpt = 'https://{}.auth0.com/oauth/token'.format(self.auth0_domain)
            headers = {'content-type': 'application/json'}

            req = requests.post(_endpt,
                                data=json.dumps(payload),
                                headers=headers)

            return req.json()["access_token"].decode("utf-8")

        def get_user_access_token(self,user):

            # (2) get initial user info
            user_info = self._get_userinfo_frm_auth0(user)

            if str(user_info["identities"][0]["provider"]) == "bitbucket":
                # (3) try to get access token from refresh token
                user_token = self._get_user_access_token_with_refresh(user=user,**user_info["identities"][0])
            else:
                user_token = user_info["identities"][0]["access_token"].decode("utf-8")

            return user_token

【问题讨论】:

    标签: python bitbucket access-token auth0 refresh-token


    【解决方案1】:

    我在 auth0 社区的帮助下找到了答案。如果有什么需要,我在这里发布了解决方案和流程:

    https://github.com/gear2000/auth0-bitbucket

    【讨论】:

      猜你喜欢
      • 2020-02-26
      • 2015-07-19
      • 2019-02-25
      • 2020-07-12
      • 1970-01-01
      • 2015-01-10
      • 2022-01-23
      • 2016-09-28
      相关资源
      最近更新 更多