简体   繁体   English

从现有的id_token获取Auth0 access_token

[英]Get Auth0 access_token from existing id_token

I'm using auth0 to authenticate my logins to my Single Page App (built on React). 我正在使用auth0对我的单页应用程序(基于React)构建的登录进行身份验证。 I'm mostly using the base API calls (listed here ). 我主要使用的是基本API调用(在此处列出)。

The process I'm using is: 我正在使用的过程是:

get username/email and password when the user enters them on my app's login page Send a POST request to /oauth/ro with those values - here is that code: 当用户在我的应用程序登录页面上输入用户名/电子邮件和密码时,获取用户名/电子邮件和密码,并使用这些值向/oauth/ro发送POST请求-这是该代码:

export const login = (params, err) => {
  if (err) return err
  const {email, password} = params
  const {AUTH0_CLIENT_ID, AUTH0_DOMAIN} = process.env
  return fetch(`${AUTH0_DOMAIN}/oauth/ro`, {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      'client_id': AUTH0_CLIENT_ID,
      'username': email,
      'password': password,
      'connection': 'Username-Password-Authentication',
      'grant_type': 'password',
      'scope': 'openid',
      'device': '',
      'id_token': ''
    })
  })
  .then(response => response.json())
  .then(json => {
    const {id_token, access_token} = json
    setCookieValue('id_token', id_token) // utility function I wrote
    return getProfile(access_token)
      .then(data => {
        const {user_id, email: emailAddress, picture, name} = data
        return {id_token, user_id, emailAddress, picture, name}
      })
  })
  .catch(error => console.log(`ERROR: ${error}`))
}

This is all sent through Redux and the user is logged in (assuming the username/password was correct). 这些都是通过Redux发送的,并且用户已登录(假设用户名/密码正确)。

However, I'm trying to figure out how to persist the login when refreshing the page/coming back to the app. 但是,我试图找出在刷新页面/返回应用程序时如何保留登录名。 I'm saving the id_token (which is a JWT ) in the browser's cookies and can fetch this when the app renders server-side. 我将id_token (这是一个JWT )保存在浏览器的cookie中,当应用程序呈现服务器端时可以获取它。 I can decode the JWT and get the payload ( sub is the user ID from auth0). 我可以解码JWT并获取有效负载( sub是auth0中的用户ID)。 However, to get the profile data I need the access_token which Auth0 provides when using the /oauth/ro POST request. 但是,要获取配置文件数据,我需要使用/oauth/ro POST请求时Auth0提供的access_token Obviously, if the JWT token has expired then it will just reject it and keep the user logged out. 显然,如果JWT令牌已过期,它将拒绝它并保持用户注销。

Here is my code to decode the JWT (happens on app render): 这是我的解码JWT的代码(发生在应用渲染上):

const ID_TOKEN = req.cookies.id_token || false
if (ID_TOKEN) {
  verifyJwt(ID_TOKEN, (err, decoded) => {
    if (err) { console.log(`JWT Verification error: ${err}`) }
    else {
      const {sub} = decoded
      getProfile(sub).then(data => store.dispatch(fetchUserDetails(data))) // fails as `sub` (the user id) is not the `access_token` which it requires
    }
  })
}

I have tried using the /oauth/ro call again, but this time specifying "grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer" and using the id_token retrieved from the cookies, and specifying a device . 我尝试再次使用/oauth/ro调用,但是这次指定"grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer"并使用从cookie中检索到的id_token并指定device However, when I do this call, I get this error from Auth0: 但是,当我执行此调用时,我从Auth0收到此错误:

{
  "error": "invalid_request",
  "error_description": "there is not an associated public key for specified client_id/user_id/device"
}

So my question is, what API call do I need to make to get the access_token from the id_token JWT? 所以我的问题是,从id_token JWT获取access_token我需要进行哪些API调用?

Also, as a bonus - when I do the POST request to login, the password is being transfered over plaintext. 另外,作为奖励,当我执行POST请求登录时, password将通过明文传输。 How would I encrypt this when sending to auth0 so they can decrypt it back? 发送到auth0时如何加密,以便他们可以解密回来? I assume it involves using the client_secret which auth0 provide but I'm not sure how to go about doing that. 我假设它涉及使用auth0提供的client_secret ,但是我不确定如何去做。

The ability to refresh a token programmatically without any type of user interaction is accomplished through the use of refresh tokens . 通过使用刷新令牌可以实现以编程方式刷新令牌而无需任何类型的用户交互的功能。 However, this is not applicable for browser-based applications because refresh tokens are long-lived credentials and the storage characteristics for browsers would place them at a too bigger risk of being leaked. 但是,这不适用于基于浏览器的应用程序,因为刷新令牌是长期存在的凭证,并且浏览器的存储特征会使它们面临太大的泄露风险。

If you want to continue to use the resource owner password credentials grant you can choose to ask the user to input the credentials again when the tokens expire. 如果要继续使用资源所有者密码凭据授予,则可以选择要求用户在令牌过期时再次输入凭据。 As an alternative, upon authentication you can obtain the required user information and initiate an application specific session. 或者,通过身份验证,您可以获得所需的用户信息并启动特定于应用程序的会话。 This could be achieved by having your server-side logic create an application specific session identifier or JWT. 这可以通过让您的服务器端逻辑创建应用程序特定的会话标识符或JWT来实现。

You can also stop using the resource owner password credentials grant and redirect the user to an Auth0 authentication page that besides returning the tokens to your application would also maintain an authenticated session for the user, meaning that when the tokens expired and your application redirected again to Auth0, the user might not need to manual reenter credentials because the Auth0 session is still valid. 您还可以停止使用资源所有者密码凭据授予,并将用户重定向到Auth0身份验证页面,该页面除了将令牌返回到您的应用程序之外还将维护用户的身份验证会话,这意味着当令牌过期并且您的应用程序再次重定向到Auth0,用户可能不需要手动重新输入凭据,因为Auth0会话仍然有效。

In relation to the password being sent in plaintext; 关于以明文形式发送的密码; the resource owner endpoint relies on HTTPS so the data is encrypted at the protocol level. 资源所有者端点依赖于HTTPS,因此在协议级别对数据进行加密。 You must also use HTTPS within your own application for any type of communication that includes user credentials of any kind. 您还必须在自己的应用程序中使用HTTPS进行任何通信,包括任何类型的用户凭据。

Also note that you can control what's returned within the ID token through the use of scopes, depending on the amount of information in question you might not even need to make additional calls to get the user profiles if you signal that you want that information to be contained within the ID token itself . 还要注意,您可以通过使用范围来控制在ID令牌中返回的内容,具体取决于所涉及的信息量,如果您表示希望将其作为信息,甚至可能不需要进行额外的调用来获取用户个人资料。包含在ID令牌本身中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Auth0中id_token和access_token有什么区别 - What is the difference between id_token and access_token in Auth0 如果您有id_token MSAL,则获取access_token - Get access_token if you have id_token MSAL Auth0.js:有没有办法将Google access_token静默替换为auth0 access_token? - Auth0.js: Is there a way to silently replace a google access_token into an auth0 access_token? 使用refresh_token使用Auth0 Lock 10 / Auth0.js 8获得新的id_token吗? - Use refresh_token to obtain new id_token with Auth0 Lock 10 / Auth0.js 8? 尝试使用 ADAL.js AuthenticationContext 获取访问令牌时,access_token 与 id_token 相同吗? - access_token same as id_token when trying to acquire access token with ADAL.js AuthenticationContext? 使用Azure AD身份验证库(角度5中的ADAL)获取Id_token,但是如何从Id_token获取访问令牌? - getting Id_token using Azure AD Authentication Library(ADAL in angular 5), but how to get access token from Id_token? 如何使用 'id_token'、'access_token'、expiry 等直接创建 userManager? - How can create userManager directly with 'id_token', 'access_token', expiry etc.? 从id_token获取Google OAuth访问令牌 - Getting a Google OAuth access token from an id_token Auth0-从服务器获取令牌 - Auth0 - get Token from Server 无法从 TokenProvider commercetools/sdk-auth 获取 access_token 和 token_type - Can not get the access_token and token_type from TokenProvider commercetools/sdk-auth
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM