在 RAML 中指定会话 cookie

Specify session cookie in RAML

我正在记录一个 API,它需要一些端点才能通过会话 cookie 对请求进行身份验证。此 cookie 是在用户使用向服务器发送用户名和密码的表单登录后设置的。

有没有办法使用 RAML 1.0 协议来指定它?

目前规范中没有针对 cookie 的任何内容。但是,您可以使用 headerssecuritySchemes.

对其进行建模

下面的示例创建了一个需要 Cookie header 的自定义 securityScheme 并描述了如何添加 'JSESSIONID' 值。这可以更改以适合您的 use-case.

第一个资源描述了一个 'login' 端点,该端点描述了一个 header 名为 'Set-Cookie' 的端点,该端点将返回一个示例值,并且必须在以后的请求中发送。

第二个资源是 securedBy 自定义 'cookie-auth' 安全方案。

#%RAML 1.0
title: Example cookie auth
version: 1.0

securitySchemes:
  cookie-auth:
    description: |
      custom authentication scheme for JSESSIONID cookie.
    type:
      x-custom
    describedBy:
      headers:
        Cookie:
          description: |
           JSESSIONID
          type: string
/login:
  post:
    body:
      application/json:
    description: |
      The session ID is returned in a cookie named `JSESSIONID`. You need to include this cookie in subsequent requests.
    headers:
      Set-Cookie:
        type: string
        example: JSESSIONID=abcde12345; Path=/; HttpOnly
    responses:
      201:
        body:
          application/json:
            # ...
/someprotectedresource:
  securedBy: cookie-auth
  get:
    # ...