【问题标题】:Xero SDK - OAuth implementation of state parameterXero SDK - 状态参数的 OAuth 实现
【发布时间】:2020-01-30 20:30:59
【问题描述】:

我目前已经使用xero-node sdk 包实现了一个 NodeJs,express api,我遇到了一个问题,似乎没有使用 OAuth 状态参数(尽管我看到它已定义作为 XeroClient 构造函数的可选参数:

export interface IXeroClientConfig {
  clientId: string,
  clientSecret: string,
  redirectUris: string[],
  scopes: string[],
  state?: string
}

谁能确认这是否已经实施?

我假设它会像这样工作:

const xero = new XeroClient({
  clientId: xeroParams.clientId,
  clientSecret: xeroParams.clientSecret,
  redirectUris: [xeroParams.redirectUrl],
  scopes: xeroParams.scopes.split(' '),
  state: this.callback_state,
});

//then when building the consent url like this, the state param would be included
const consentUrl = await xero.buildConsentUrl();

然后当回调被触发时,我期望能够访问state code 作为查询参数之一。类似的东西解释了here

我看到返回了 session_state 参数,但这与我提供的状态代码不匹配。

【问题讨论】:

    标签: node.js xero-api


    【解决方案1】:

    以下是使用 xero-node SDK 通过 OAuth 流传递状态的方法:

    https://github.com/SerKnight/xero-node-basic-app/blob/master/index.js#L37

    例子:

    • 首先生成consentUrl,然后附加您的自定义参数。
    app.get('/connect', async function(req, res) {
      try {
        let consentUrl = await xero.buildConsentUrl();
        res.redirect(consentUrl + "&state=THIS_IS_A_STANDARD_OAUTH_2_STATE_PARAMETER"); // Append any type of state/params you would like
      } catch (err) {
        res.send("Sorry, something went wrong");
      }
    })
    
    ...
    
    app.get('/callback', async function(req, res) {
      let url = redirectUri + req.originalUrl;
    
      console.log('req.query: ', req.query) // this will the the same state/params you passed to the API
    
      // ...do api stuff..
      // ref: https://github.com/XeroAPI/xero-node-oauth2-app
    
      res.send(req.query);
    })
    

    https://github.com/SerKnight/xero-node-basic-app/blob/master/index.js#L37

    值得注意的是,XeroClient 的可选state: 参数是为openidclient 保留的。不要使用它。只需将其附加到同意网址即可。

    【讨论】:

    • 对不起,我应该澄清一下,是的,我正在尝试通过回调来获取状态代码以识别用户
    • 爆炸了吗?我相信我在这个提交中修复了这种方式:github.com/XeroAPI/xero-node/commit/…你使用的是什么版本?
    • 我正在使用xero-node的v4.0.7
    • 它没有爆炸,只是我没有在回调中获得相同的状态代码。有一个session_state 参数,但它返回的代码与我提供的代码完全不同
    • 因此,与其将状态传递给 xero 客户端,不如将其附加到身份验证 url!感谢您的问题-希望其他人也可以使用它:)
    猜你喜欢
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    • 2010-12-30
    • 2016-08-13
    • 2014-08-18
    • 2020-12-05
    • 1970-01-01
    • 2023-04-10
    相关资源
    最近更新 更多