|
| 1 | +#include <stddef.h> |
| 2 | +#if defined(LWS_WITH_JOSE) |
| 3 | +/* |
| 4 | + * libwebsockets - small server side websockets and web server implementation |
| 5 | + * |
| 6 | + * Copyright (C) 2010 - 2025 Andy Green <andy@warmcat.com> |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to |
| 10 | + * deal in the Software without restriction, including without limitation the |
| 11 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 12 | + * sell copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 23 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 24 | + * IN THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +/** \defgroup captcha Captcha |
| 28 | + * ##Captcha API |
| 29 | + * |
| 30 | + * Lws provides a generic captcha "engine" that handles JWT sessions, |
| 31 | + * redirection flows, and asset serving. Captcha implementations (puzzles, |
| 32 | + * ratelimits, etc) provide a `struct lws_captcha_ops` to customize the |
| 33 | + * behavior. |
| 34 | + */ |
| 35 | +/**@{*/ |
| 36 | + |
| 37 | +typedef enum { |
| 38 | + LWS_INTERCEPTOR_RET_REJECT = 0, /* Failed the challenge */ |
| 39 | + LWS_INTERCEPTOR_RET_PASS = 1, /* Passed immediately */ |
| 40 | + LWS_INTERCEPTOR_RET_DELAYED = 2, /* Use a timer before passing (for ratelimiting) */ |
| 41 | +} lws_interceptor_result_t; |
| 42 | + |
| 43 | +struct lws_interceptor_ops { |
| 44 | + const char *name; /* e.g., "ratelimit" or "puzzle" */ |
| 45 | + |
| 46 | + /** |
| 47 | + * [Optional] Add implementation-specific dynamic JS variables to |
| 48 | + * the automatically served "captcha-config.js". |
| 49 | + */ |
| 50 | + int (*get_config_js)(struct lws *wsi, char *buf, size_t len); |
| 51 | + |
| 52 | + /** |
| 53 | + * [Optional] Customize the "visit" JWT claims. |
| 54 | + * e.g., Store a random math problem or puzzle seed here. |
| 55 | + */ |
| 56 | + int (*init_visit_cookie)(struct lws *wsi, char *buf, size_t len); |
| 57 | + |
| 58 | + /** |
| 59 | + * Verify the POSTed challenge submission. |
| 60 | + * Decides if the user passes, fails, or needs to wait. |
| 61 | + */ |
| 62 | + lws_interceptor_result_t (*verify)(struct lws *wsi, const void *data, size_t len); |
| 63 | + |
| 64 | + /** |
| 65 | + * [Optional] Extra logic to run if verify returned LWS_INTERCEPTOR_RET_DELAYED |
| 66 | + * and the timer expired. |
| 67 | + */ |
| 68 | + void (*on_delay_expired)(struct lws *wsi); |
| 69 | +}; |
| 70 | + |
| 71 | +/** |
| 72 | + * lws_interceptor_check() - Check if a valid interceptor session exists |
| 73 | + * |
| 74 | + * \param wsi: the connection to check |
| 75 | + * |
| 76 | + * Returns 0 if a valid interceptor session exists (JWT cookie present and valid for IP), |
| 77 | + * non-zero if a interceptor diversion is required. |
| 78 | + */ |
| 79 | +LWS_VISIBLE LWS_EXTERN int |
| 80 | +lws_interceptor_check(struct lws *wsi, const struct lws_protocols *prot); |
| 81 | + |
| 82 | +/** |
| 83 | + * lws_interceptor_handle_http() - Generic HTTP handler for interceptor plugins |
| 84 | + * |
| 85 | + * \param wsi: the connection |
| 86 | + * \param user: PSS |
| 87 | + * \param ops: the interceptor implementations ops |
| 88 | + * |
| 89 | + * This handles serving assets, config JS, and processing POST submissions |
| 90 | + * using the provided wheat. |
| 91 | + */ |
| 92 | +LWS_VISIBLE LWS_EXTERN int |
| 93 | +lws_interceptor_handle_http(struct lws *wsi, void *user, const struct lws_interceptor_ops *ops); |
| 94 | + |
| 95 | +/** |
| 96 | + * lws_callback_interceptor() - Generic protocol callback for interceptor plugins |
| 97 | + */ |
| 98 | +LWS_VISIBLE LWS_EXTERN int |
| 99 | +lws_callback_interceptor(struct lws *wsi, enum lws_callback_reasons reason, |
| 100 | + void *user, void *in, size_t len, |
| 101 | + const struct lws_interceptor_ops *ops); |
| 102 | + |
| 103 | +/**@}*/ |
| 104 | + |
| 105 | +#endif |
0 commit comments