模块 Gem::GemcutterUtilities

用于使用 RubyGems API 的实用方法。

WebauthnListener 类在用户使用 Gem 主机成功进行 WebAuthn 认证后检索 OTP。实例使用给定的 TCPServer 实例打开一个套接字,并侦听来自 Gem 主机的请求。请求应为对根路径的 GET 请求,并在查询参数“code”中包含 OTP 代码。侦听器将返回该代码,该代码将用作 API 请求的 OTP。

侦听器在接收到请求后发送的响应类型

- 200 OK: OTP code was successfully retrieved
- 204 No Content: If the request was an OPTIONS request
- 400 Bad Request: If the request did not contain a query parameter `code`
- 404 Not Found: The request was not to the root path
- 405 Method Not Allowed: OTP code was not retrieved because the request was not a GET/OPTIONS request

用法示例

thread = Gem::WebauthnListener.listener_thread("https://rubygems.example", server)
thread.join
otp = thread[:otp]
error = thread[:error]

WebauthnListener 响应类由 WebauthnListener 使用,以创建要发送到 Gem 主机的响应。它在初始化时创建一个 Gem::Net::HTTPResponse 实例,并且可以使用“to_s”转换为适合通过套接字发送的格式。Gem::Net::HTTPResponse 实例无法直接通过套接字发送。

响应类类型

- OkResponse
- NoContentResponse
- BadRequestResponse
- NotFoundResponse
- MethodNotAllowedResponse

用法示例

server = TCPServer.new(0)
socket = server.accept

response = OkResponse.for("https://rubygems.example")
socket.print response.to_s
socket.close

WebauthnPoller 类在用户成功进行 WebAuthn 认证后检索 OTP。实例轮询 Gem 主机的 OTP 代码。轮询请求 (api/v1/webauthn_verification/<webauthn_token>/status.json) 每 5 秒发送到 Gem 主机,并在 5 分钟后超时。如果 json 响应中的 status 字段为“success”,则 code 字段将包含 OTP 代码。

用法示例

thread = Gem::WebauthnPoller.poll_thread(
  {},
  "RubyGems.org",
  "https://rubygems.org.cn/api/v1/webauthn_verification/odow34b93t6aPCdY",
  { email: "email@example.com", password: "password" }
)
thread.join
otp = thread[:otp]
error = thread[:error]