使用预设令牌创建模拟 OAuth 服务器

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

我使用存储令牌的外部 OAuth 服务器对 Flask API 上的用户进行身份验证,然后通过要求外部服务器内省我的 API 来内省令牌。我现在想测试我的功能,但不想在测试中使用外部服务器。因此我想在本地托管一个 OAuth 服务器进行测试。我在这个服务器上唯一想做的就是内省。是否可以以某种方式托管带有一些设置令牌、client_id、client_secret 的 OAuth 服务器,以便我可以完成此示例代码:

import requests

url = "http://localhost:8080/introspect"
payload = {
    "token": "tokentokentoken",
    'token_type_hint': 'access_token'
}

# Make a POST request to the specified URL with the payload
response = requests.post(url, data=payload, auth=("myClientID", "secret"))

# Check if the request was successful (status code 200)
if response.status_code == 200:
    # Print the token received in the response
    print("Token:", response.json().get("token"))
else:
    # Print an error message if the request was not successful
    print("Error:", response.status_code, response.text)

我已经尝试过这个docker镜像https://github.com/navikt/mock-oauth2-server但我似乎不知道如何拥有预设令牌、client_id和client_secret。

docker testing docker-compose oauth-2.0 mocking
1个回答
0
投票

您需要使用模拟 HTTP 服务器,该服务器为作为输入提供的特定模拟不透明令牌返回预设的内省响应。一种方法是使用 Wiremock 工具。

有一个端点可用于根据传入标头值注册预期响应。示例用法发布在这个答案

有关处理一些技术设置的示例,您可以查看我的 Node.js 示例代码。这些概念可以轻松移植到任何其他 API 编程语言。

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