{
  "version": 12,
  "name": "06_auth_types",
  "description": "Example: the auth types — flow-level auth inherited by requests, per-request Basic, Bearer, API key in a header and in the query, an explicit 'none', and a negative test with wrong credentials. OAuth 2.0 (client credentials) is included but disabled by its run condition: set $oauthTokenUrl, $oauthClientId, $oauthClientSecret (ideally as project secrets) and $runOAuth = true to try it.",
  "globals": [
    {
      "name": "$baseUrl",
      "value": "https://httpbin.org"
    },
    {
      "name": "$basicUser",
      "value": "clem"
    },
    {
      "name": "$basicPass",
      "value": "example-pass"
    },
    {
      "name": "$apiToken",
      "value": "example-token-123"
    },
    {
      "name": "$apiKey",
      "value": "example-key-456"
    },
    {
      "name": "$runOAuth",
      "value": "false"
    },
    {
      "name": "$oauthTokenUrl",
      "value": "https://example.com/oauth/token"
    },
    {
      "name": "$oauthClientId",
      "value": "client-id"
    },
    {
      "name": "$oauthClientSecret",
      "value": "client-secret"
    }
  ],
  "globalFunctions": "// Assertion helpers, built on check(label, pass, details).\n// Each records a pass / fail line in the log and the report.\n// A failed one fails its step when the step is done;\n// the code keeps running. Change them or add your own.\n// r defaults to the last response.\n\nfunction assertTrue(value, label = \"is true\") {\n  return check(label, Boolean(value), { actual: value, expected: true });\n}\n\nfunction assertEqual(actual, expected, label = \"equals \" + JSON.stringify(expected)) {\n  return check(label, isEqual(actual, expected), { actual, expected });\n}\n\nfunction assertNotEqual(actual, unexpected, label = \"does not equal \" + JSON.stringify(unexpected)) {\n  return check(label, !isEqual(actual, unexpected), { actual, expected: \"not \" + JSON.stringify(unexpected) });\n}\n\n// text contains a part, or a list contains an item\nfunction assertContains(haystack, needle, label = \"contains \" + JSON.stringify(needle)) {\n  const pass = Array.isArray(haystack) ? haystack.some((x) => isEqual(x, needle)) : String(haystack).includes(needle);\n  return check(label, pass, { actual: haystack, expected: needle });\n}\n\nfunction assertMatch(text, regex, label = \"matches \" + regex) {\n  return check(label, new RegExp(regex).test(String(text)), { actual: text, expected: String(regex) });\n}\n\nfunction assertLessThan(actual, limit, label = \"< \" + limit) {\n  return check(label, Number(actual) < Number(limit), { actual, expected: \"< \" + limit });\n}\n\nfunction assertGreaterThan(actual, limit, label = \"> \" + limit) {\n  return check(label, Number(actual) > Number(limit), { actual, expected: \"> \" + limit });\n}\n\n// expected: 200, \"2xx\", or a list such as [200, 201]\nfunction assertStatus(expected, r = response) {\n  const list = [].concat(expected).map(String);\n  const status = String(r && r.status);\n  const pass = list.some((e) => e === status || (/^[1-5]xx$/i.test(e) && e[0] === status[0]));\n  return check(\"status is \" + list.join(\" or \"), pass, { actual: r && r.status, expected });\n}\n\nfunction assertTimeBelow(ms, r = response) {\n  return check(\"time < \" + ms + \" ms\", Boolean(r) && r.timeMs < ms, { actual: r && r.timeMs, expected: \"< \" + ms });\n}\n\n// without expected, the header only has to exist\nfunction assertHeader(name, expected, r = response) {\n  const value = r ? r.headers[String(name).toLowerCase()] : undefined;\n  if (expected === undefined) return check(\"header \" + name + \" exists\", value !== undefined, { actual: value });\n  return check(\"header \" + name + \" equals \" + expected, String(value) === String(expected), { actual: value, expected });\n}\n\n// e.g. assertJsonPath(\"$.items[0].id\", 42); without expected, the value only has to exist\nfunction assertJsonPath(path, expected, r = response) {\n  const value = jsonPath(path, r);\n  if (expected === undefined) return check(path + \" exists\", value !== undefined, { actual: value });\n  return check(path + \" equals \" + JSON.stringify(expected), isEqual(value, expected), { actual: value, expected });\n}\n\n// schema: a JSON Schema object\nfunction assertSchema(schema, r = response) {\n  const result = validateSchema(r, schema);\n  return check(\"matches the JSON Schema\", result.valid, { error: result.errors || undefined });\n}\n",
  "libraries": [],
  "activeEnvironment": "",
  "auth": {
    "token": "$apiToken",
    "type": "bearer"
  },
  "settings": {},
  "data": {
    "file": ""
  },
  "cookies": [],
  "tags": [
    "example",
    "auth",
    "security"
  ],
  "steps": [
    {
      "auth": {
        "type": "inherit"
      },
      "checks": [
        {
          "op": "equals",
          "target": "status",
          "value": "200"
        },
        {
          "op": "equals",
          "path": "$.token",
          "target": "json",
          "value": "example-token-123"
        }
      ],
      "id": "inherit-bearer",
      "method": "GET",
      "name": "Inherited flow auth (Bearer)",
      "type": "request",
      "url": "$baseUrl/bearer"
    },
    {
      "auth": {
        "password": "$basicPass",
        "type": "basic",
        "username": "$basicUser"
      },
      "checks": [
        {
          "op": "equals",
          "target": "status",
          "value": "200"
        },
        {
          "op": "equals",
          "path": "$.authenticated",
          "target": "json",
          "value": "true"
        },
        {
          "op": "equals",
          "path": "$.user",
          "target": "json",
          "value": "clem"
        }
      ],
      "id": "basic",
      "method": "GET",
      "name": "Basic auth",
      "type": "request",
      "url": "$baseUrl/basic-auth/$basicUser/$basicPass"
    },
    {
      "auth": {
        "password": "wrong",
        "type": "basic",
        "username": "$basicUser"
      },
      "checks": [
        {
          "op": "equals",
          "target": "status",
          "value": "401"
        }
      ],
      "id": "basic-wrong",
      "method": "GET",
      "name": "Basic auth with a wrong password → 401",
      "type": "request",
      "url": "$baseUrl/basic-auth/$basicUser/$basicPass"
    },
    {
      "auth": {
        "prefix": "Token",
        "token": "$apiToken",
        "type": "bearer"
      },
      "checks": [
        {
          "op": "equals",
          "path": "$.headers.Authorization",
          "target": "json",
          "value": "Token example-token-123"
        }
      ],
      "id": "bearer-custom-prefix",
      "method": "GET",
      "name": "Bearer with a custom prefix",
      "type": "request",
      "url": "$baseUrl/headers"
    },
    {
      "auth": {
        "in": "header",
        "name": "X-Api-Key",
        "type": "apikey",
        "value": "$apiKey"
      },
      "checks": [
        {
          "op": "equals",
          "path": "$.headers.X-Api-Key",
          "target": "json",
          "value": "example-key-456"
        }
      ],
      "id": "apikey-header",
      "method": "GET",
      "name": "API key in a header",
      "type": "request",
      "url": "$baseUrl/headers"
    },
    {
      "auth": {
        "in": "query",
        "name": "api_key",
        "type": "apikey",
        "value": "$apiKey"
      },
      "checks": [
        {
          "op": "equals",
          "path": "$.args.api_key",
          "target": "json",
          "value": "example-key-456"
        }
      ],
      "id": "apikey-query",
      "method": "GET",
      "name": "API key in the query string",
      "type": "request",
      "url": "$baseUrl/get"
    },
    {
      "auth": {
        "type": "none"
      },
      "checks": [
        {
          "op": "equals",
          "target": "status",
          "value": "401"
        }
      ],
      "id": "no-auth",
      "method": "GET",
      "name": "Explicitly no auth → 401 on a protected endpoint",
      "type": "request",
      "url": "$baseUrl/bearer"
    },
    {
      "auth": {
        "addTo": "header",
        "clientAuth": "basic",
        "clientId": "$oauthClientId",
        "clientSecret": "$oauthClientSecret",
        "grant": "client_credentials",
        "headerPrefix": "Bearer",
        "scope": "",
        "tokenUrl": "$oauthTokenUrl",
        "type": "oauth2"
      },
      "checks": [
        {
          "op": "matches",
          "path": "$.headers.Authorization",
          "target": "json",
          "value": "^Bearer .+"
        }
      ],
      "conditional": "$runOAuth === true",
      "id": "oauth2-cc",
      "method": "GET",
      "name": "OAuth 2.0 client credentials (off by default)",
      "type": "request",
      "url": "$baseUrl/headers"
    }
  ]
}
