验证 oidc-client 中的状态和随机数

Ish*_*ain 5 csrf identityserver4 implicit-flow oidc-client-js angular

我的理解是 - oidc-client生成随机数和状态并将其发送到授权服务器(身份服务器 4)。这是用来防止CSRF攻击、重放攻击的。

状态和随机数通过下面的signinredirect()示例示例发送

https://auth.azurewebsites.net/Account/Login?
ReturnUrl=%2Fconnect%2Fauthorize%2Fcallback%3F
client_id%3DLocal%26
redirect_uri%3Dhttp%253A%252F%252Flocalhost%253A4200%252Fauth-callback%252F%26
response_type%3Did_token%2520token%26
scope%3Dopenid%2520profile%2520Api%26
state%3D212ee56661074896aea2b6043d2b8a3f%26
nonce%3D393838b342d543d5910f38cbcab22fa0%26
loginType%3DInternal // my extra params
Run Code Online (Sandbox Code Playgroud)

问题 1 - 回调后状态未定义

状态添加到回调 URL 中,如下所示

    http://localhost:4200/auth-callback#id_token=eyJhbG...
    token_type=Bearer
    &expires_in=300&
    scope=openid%20profile%20Api&
    state=155e3e4352814ca48e127547c134144e&
    session_state=DPXW-ijMR4ST9iTSxgMwhsLq7aoknEZOnq3aFDooCFg.ifImJurwkwU6M5lwZXCUuw
Run Code Online (Sandbox Code Playgroud)

状态必须存在于用户中。但就我而言,我在回调方法中看到状态未定义

  async completeAuthentication() {
    await this.manager
      .signinRedirectCallback()
      .then(x => {
        this.user = x;
        this.user.state = x.state; // undefined
        this.user.session_state = x.session_state;
      })
      .catch(errorData => {
        const expired = errorData;
      });
Run Code Online (Sandbox Code Playgroud)

状态未定义,但会话状态具有值

问题 -

  1. oidc 在生成后将状态存储在哪里?
  2. 为什么状态未定义?回调后如何获取状态?我想不是通过 URL(路径)!
  3. oidc 是否在内部验证状态?如何?在哪里?

问题 2 - 随机数

id_token 中收到随机数值

created: 1594171097
extraTokenParams: {}
id: "5cc732d3b7fe4a0abdb371be3bda69a6"
nonce: "17c3f171328b4542a282fcbdd43d6fe4"
Run Code Online (Sandbox Code Playgroud)

我还看到登录后有 2-4 个 oidc 用户存储在本地存储中。为什么这样?他们具有相同的用户信息,但 ID 和随机数不同。我用户clearstalestate()这些都是在每次新登录或刷新后生成的 在此输入图像描述

问题 -

  1. 为什么2-4个用户信息存储在本地存储中?哪种方法生成本地存储用户?
  2. 随机数值是每个会话还是每个用户请求?
  3. 生成后的随机数值存储在哪里?
  4. oidc 是否在内部验证随机数?在哪里?如果不是我该怎么办?

Soh*_*han 3

所以我调试了代码并找到了您的答案的问题,

  • 随机数值是每个会话还是每个用户请求?

    这不应该重复,因此它是根据请求生成的,以减轻重放攻击

  • 生成后的随机数值存储在哪里?

    存储在会话存储中

  • oidc 是否在内部验证随机数?在哪里?如果不是我该怎么办?

    是的,它在内部验证。您必须查看 oidc-client js。我从那里提取了一些代码以获得清晰的视图,

      _validateIdToken(state, response) {
         if (!state.nonce) {
             Log.error("ResponseValidator._validateIdToken: No nonce on state");
             return Promise.reject(new Error("No nonce on state"));
         }
    
         let jwt = this._joseUtil.parseJwt(response.id_token);
         if (!jwt || !jwt.header || !jwt.payload) {
             Log.error("ResponseValidator._validateIdToken: Failed to parse id_token", jwt);
             return Promise.reject(new Error("Failed to parse id_token"));
         }
    
         if (state.nonce !== jwt.payload.nonce) {
             Log.error("ResponseValidator._validateIdToken: Invalid nonce in id_token");
             return Promise.reject(new Error("Invalid nonce in id_token"));
         }
    
    Run Code Online (Sandbox Code Playgroud)

    }

现在回到状态参数验证。它在 User 对象中不再可用,而是预先在内部进行验证。这是 oidc-client js 的代码摘录

processSigninResponse(url, stateStore) {
    Log.debug("OidcClient.processSigninResponse");

    var response = new SigninResponse(url);

    if (!response.state) {
        Log.error("OidcClient.processSigninResponse: No state in response");
        return Promise.reject(new Error("No state in response"));
    }

    stateStore = stateStore || this._stateStore;

    return stateStore.remove(response.state).then(storedStateString => {
        if (!storedStateString) {
            Log.error("OidcClient.processSigninResponse: No matching state found in storage");
            throw new Error("No matching state found in storage");
        }

        let state = SigninState.fromStorageString(storedStateString);

        Log.debug("OidcClient.processSigninResponse: Received state from storage; validating response");
        return this._validator.validateSigninResponse(state, response);
    });
}
Run Code Online (Sandbox Code Playgroud)

状态和随机数均由 oidc-client 库管理。