如何使用wiremock模拟google oauth2身份验证的完整工作流程?

问题描述 投票:0回答:1

我有一种情况,我需要模拟整个谷歌驱动器工作流程进行内部测试。我们不想使用实际的服务帐户进行测试。我使用wiremock并模拟(或者我认为)以下api请求。

  1. 身份验证端点:
request": {
    "method": "GET",
    "urlPattern": ".*/o/oauth2/v2/auth.*",
  "headers": {
      "Host": {
        "equalTo": "accounts.google.com"
      }
    }
    },
  "response": {
    "status": 302,
    "headers": {
       "Location": "callbackurl"
    }
  1. 重定向端点:
request": {
    "method": "GET",
    "urlPattern": ".*/suite/oauth/callback.*" 
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "application/json"
    },
    "body": "callback is succesful"
}
}

  1. 令牌端点:
{
  "request": {
    "method": "POST",
    "url": "/token",
    "headers" : {
    "Host" : {
    "equalTo" : "oauth2.googleapis.com"
    }
    }
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "application/json"
    },
    "body": "{ \"access_token\": \"your_access_token\", \"token_type\": \"bearer\", \"expires_in\": 3600, \"refresh_token\": \"your_refresh_token\" }"
  }
}

但是,当我从邮递员或 python 脚本到达端点时,这会起作用。 但是当我尝试使用我的实际应用程序(我已经使用wiremock启用了代理)时,这不起作用,它在chrome中打开一个新选项卡并抛出错误。该网址与我使用邮递员和此应用程序测试的网址完全相同。问题是应用程序中的请求在新窗口中打开

You can’t sign in because this app sent an invalid request. You can try again later, or contact the developer about this issue. Learn more about this error
If you are a developer of this app, see error details.
Error 400: redirect_uri_mismatch

你有什么想法吗?

python json mocking google-oauth wiremock
1个回答
0
投票

我已经解决了。基本上,第二个模拟是不正确的。我不必使用 200 响应来模拟回调,而是必须使用从身份验证端点收到的“状态”值发起对回调 url 的请求,并且还需要发送虚拟身份验证令牌。

© www.soinside.com 2019 - 2024. All rights reserved.