将 Salesforce 注册页面与 VanillaJS oidc-client-js 集成,出现错误 - 在存储中找不到匹配的状态

Integrate Salesforce registration page with VanillaJS oidc-client-js, getting the error - No matching state found in storage

将 Salesforce 注册页面与 VanillaJS 集成,出现错误 - 在存储中找不到匹配的状态

我们在创建“创建帐户”按钮时将用户重定向到 Salesforce 注册页面。 一旦用户在 Salesforce 中注册,用户将被重定向到我们的网站,但我们收到此错误。 ('No matching state found in storage').

我们尝试了以下解决方案,但仍然出现相同的错误。

As I stated in my answer, the oidc client maintains state information in the local storage so that it can verify that it got the response back from the intended server. You can mimic this by generating a secure random string and saving it in localStorage. Do this before sending a request to your auth server to register a new user.

参考-

有创建注册相关的功能吗?如何解决这个问题?

谢谢。 感谢您的帮助。

在这个问题上花了几天时间。终于找到了解决方法,因为注册不是 OIDC 的功能。

为了克服这个问题,需要遵循与注册过程相同的登录过程,创建与 startSigninMainWindow 相同的 startSignupMainWindow 方法并传递 signUpFlag:true,如下面的代码所示。

/* This function is written to mimic the oidc library sign in process flow */
function startSignupMainWindow() {
  var someState = {
    message: window.location.href,
    signUpFlag: true
  };
  mgr.signinRedirect({
    state: someState,
    useReplaceToNavigate: true
  }).then(function() {
    log("signinRedirect done");
  }).catch(function(err) {
    log(err);
  });
}

阅读 UserManager.js 中的 signUpFlag:true 并将 Salesforce 登录页面 Url 与注册页面 url 交换并在代码中调用注册函数。

UserManager.js(oidc - client - dev - js / src / UserManager.js)
//UserManager Customised Code :

return this.createSigninRequest(args).then(signinRequest => {
  Log.debug("UserManager._signinStart: got signin request");
  navigatorParams.url = signinRequest.url;
  navigatorParams.id = signinRequest.state.id;
  if (signinRequest.state._data.signUpFlag) {
    register(signinRequest.state._id, signinRequest.state._code_challenge);
  } else {
    return handle.navigate(navigatorParams);
  }
})

下面的代码是用代码写的Register函数

/* This function is written to send the code_challenge to salesforce server so that
salesforce server holds the code challenge and used to verify the further requests(token-request)
against the code_challenge it received initially.*/

//Customised register function written outside the library (Inside our App):

function register(_id, code_challenge) {
  var date = new Date();
  var baseUrl = "SALESFORCE_URL/login/SelfRegister?expid=id";
  var expId = "id";
  var userPage = encodeURIComponent(window.location.href);
  var appDetails = "response_type=code&" +
    "client_id=CLIENT_ID" +
    "client_secret=CLIENT_SECRET&" +
    "redirect_uri=CALLBACK_URL&" +
    "state=" + _id + "&code_challenge=" + code_challenge + "&code_challenge_method=S256&response_mode=query";
  var encodedapp = encodeURIComponent(appDetails);
  var startUrl = "/services/oauth2/authorize/expid?" + encodedapp;
  var signUpUrl = baseUrl + "&startURL=" + startUrl;
  window.open(signUpUrl, "_self");
};