22#if defined(HAVE_SYS_EPOLL_H)
24#include <sys/timerfd.h>
25#elif defined(HAVE_SYS_EVENT_H)
28#error libpq-oauth is not supported on this platform
35#ifdef USE_DYNAMIC_OAUTH
53#if defined(USE_DYNAMIC_OAUTH) && defined(LIBPQ_INT_H)
54#error do not rely on libpq-int.h in dynamic builds of libpq-oauth
69#define MAX_OAUTH_RESPONSE_SIZE (256 * 1024)
83#define MAX_OAUTH_NESTING_LEVEL 16
301 libpq_gettext(
"WARNING: libcurl easy handle removal failed: %s\n"),
321 libpq_gettext(
"WARNING: libcurl multi handle cleanup failed: %s\n"),
334 if (
actx->timerfd >= 0)
389 if (
actx->curl_err[0])
411#define actx_error(ACTX, FMT, ...) \
412 appendPQExpBuffer(&(ACTX)->errbuf, libpq_gettext(FMT), ##__VA_ARGS__)
414#define actx_error_internal(ACTX, FMT, ...) \
415 appendPQExpBuffer(&(ACTX)->errbuf, FMT, ##__VA_ARGS__)
417#define actx_error_str(ACTX, S) \
418 appendPQExpBufferStr(&(ACTX)->errbuf, S)
425#define CHECK_MSETOPT(ACTX, OPT, VAL, FAILACTION) \
427 struct async_ctx *_actx = (ACTX); \
428 CURLMcode _setopterr = curl_multi_setopt(_actx->curlm, OPT, VAL); \
430 actx_error(_actx, "failed to set %s on OAuth connection: %s",\
431 #OPT, curl_multi_strerror(_setopterr)); \
436#define CHECK_SETOPT(ACTX, OPT, VAL, FAILACTION) \
438 struct async_ctx *_actx = (ACTX); \
439 CURLcode _setopterr = curl_easy_setopt(_actx->curl, OPT, VAL); \
441 actx_error(_actx, "failed to set %s on OAuth connection: %s",\
442 #OPT, curl_easy_strerror(_setopterr)); \
447#define CHECK_GETINFO(ACTX, INFO, OUT, FAILACTION) \
449 struct async_ctx *_actx = (ACTX); \
450 CURLcode _getinfoerr = curl_easy_getinfo(_actx->curl, INFO, OUT); \
452 actx_error(_actx, "failed to get %s from OAuth response: %s",\
453 #INFO, curl_easy_strerror(_getinfoerr)); \
488#define PG_OAUTH_REQUIRED true
489#define PG_OAUTH_OPTIONAL false
501#define oauth_parse_set_error(ctx, fmt, ...) \
502 appendPQExpBuffer((ctx)->errbuf, libpq_gettext(fmt), ##__VA_ARGS__)
504#define oauth_parse_set_error_internal(ctx, fmt, ...) \
505 appendPQExpBuffer((ctx)->errbuf, fmt, ##__VA_ARGS__)
583 "internal error: started field \"%s\" before field \"%s\" was finished",
635 "internal error: field \"%s\" still active at end of object",
691 "internal error: found unexpected array end while parsing field \"%s\"",
746 "internal error: scalar target found at nesting level %d",
756 "internal error: scalar field \"%s\" would be assigned twice",
778 "internal error: array member found at nesting level %d",
836 switch (content_type[
i])
852 actx_error(
actx,
"unexpected content type: \"%s\"", content_type);
1024 return actx->debugging ? 0 : 1;
1145 if (
err->error_description)
1161 ?
gettext_noop(
"provider rejected the oauth_client_secret")
1162 :
gettext_noop(
"provider requires client authentication, and no oauth_client_secret is set"));
1219#if defined(HAVE_SYS_EPOLL_H)
1230 if (
actx->timerfd < 0)
1243#elif defined(HAVE_SYS_EVENT_H)
1258 if (
actx->timerfd < 0)
1266#error setup_multiplexer is not implemented on this platform
1280#if defined(HAVE_SYS_EPOLL_H)
1336#elif defined(HAVE_SYS_EVENT_H)
1398 for (
int i = 0;
i < res; ++
i)
1424#error register_socket is not implemented on this platform
1443#if defined(HAVE_SYS_EPOLL_H)
1446#elif defined(HAVE_SYS_EVENT_H)
1471#error comb_multiplexer is not implemented on this platform
1491#if defined(HAVE_SYS_EPOLL_H)
1505 spec.it_value.tv_nsec = 1;
1510 spec.it_value.tv_nsec = (
timeout % 1000) * 1000000;
1520#elif defined(HAVE_SYS_EVENT_H)
1576#error set_timer is not implemented on this platform
1588#if defined(HAVE_SYS_EPOLL_H) || defined(HAVE_SYS_EVENT_H)
1601#error timer_expired is not implemented on this platform
1699 for (
int i = 0;
i < size;
i++)
1709 if (
c >= 0x20 &&
c <= 0x7E)
1714 && (
c ==
'\r' ||
c ==
'\n'))
1798 if (
actx->debugging)
1820#if CURL_AT_LEAST_VERSION(7, 85, 0)
1822 const char *
protos =
"https";
1823 const char *
const unsafe =
"https,http";
1830 if (
actx->debugging)
1844 if (
actx->debugging)
1960#ifndef CURL_IGNORE_DEPRECATION
1961#define CURL_IGNORE_DEPRECATION(x) x
1968#define PG_CURL_IGNORE_DEPRECATION(x) CURL_IGNORE_DEPRECATION(x;)
2037 if (
actx->errbuf.len == 0)
2058 actx_error(
actx,
"no result was retrieved for the finished handle");
2205 if (!
actx->provider.grant_types_supported)
2224 actx->provider.grant_types_supported =
temp;
2238 const char *oauth_issuer_id =
actx->issuer_id;
2263 "the issuer identifier (%s) does not match oauth_issuer (%s)",
2271#define HTTPS_SCHEME "https://"
2272#define OAUTH_GRANT_TYPE_DEVICE_CODE "urn:ietf:params:oauth:grant-type:device_code"
2290 "issuer \"%s\" does not provide a device authorization endpoint",
2310 if (!
actx->debugging)
2316 "device authorization endpoint \"%s\" must use HTTPS",
2325 "token endpoint \"%s\" must use HTTPS",
2341 const char *oauth_client_id =
actx->client_id;
2342 const char *oauth_client_secret =
actx->client_secret;
2348 if (oauth_client_secret)
2389 actx->used_basic_auth =
true;
2400 actx->used_basic_auth =
false;
2425 const char *oauth_scope =
actx->scope;
2433 if (oauth_scope && oauth_scope[0])
2513 const char *device_code =
actx->authz.device_code;
2600 if (
tok.access_token)
2614 if (
strcmp(
err->error,
"authorization_pending") != 0 &&
2625 if (
strcmp(
err->error,
"slow_down") == 0)
2629 actx->authz.interval += 5;
2654 .user_code =
actx->authz.user_code,
2655 .verification_uri_complete =
actx->authz.verification_uri_complete,
2656 .expires_in =
actx->authz.expires_in,
2703#if HAVE_THREADSAFE_CURL_GLOBAL_INIT
2707#if !HAVE_THREADSAFE_CURL_GLOBAL_INIT
2728 req->error =
libpq_gettext(
"curl_global_init previously failed during OAuth setup");
2751#if HAVE_THREADSAFE_CURL_GLOBAL_INIT
2768 "\tCurl initialization was reported thread-safe when libpq\n"
2769 "\twas compiled, but the currently installed version of\n"
2770 "\tlibcurl reports that it is not. Recompile libpq against\n"
2771 "\tthe installed version of libcurl.");
2780#if !HAVE_THREADSAFE_CURL_GLOBAL_INIT
2805 char *oauth_token =
NULL;
2875 *altsock =
actx->timerfd;
2935 request->v1.token = oauth_token;
2937 if (!
actx->user_prompted)
2946 actx->user_prompted =
true;
2964 *altsock =
actx->timerfd;
2984 }
while (!oauth_token && !
actx->running);
3007 bool sigpipe_pending;
3040 if (
actx->debugging)
3042 actx->dbg_num_calls++;
3045 actx->dbg_num_calls);
3116 if (
strcmp(opt->keyword,
"oauth_client_id") == 0)
3119 if (!
actx->client_id)
3122 else if (
strcmp(opt->keyword,
"oauth_client_secret") == 0)
3125 if (!
actx->client_secret)
3133 actx->discovery_uri =
request->v1.openid_configuration;
static void cleanup(void)
#define Assert(condition)
#define fprintf(file, fmt, msg)
void err(int eval, const char *fmt,...)
PQauthDataHook_type PQgetAuthDataHook(void)
PQconninfoOption * PQconninfo(PGconn *conn)
void PQconninfoFree(PQconninfoOption *connOptions)
int PQsocketPoll(int sock, int forRead, int forWrite, pg_usec_time_t end_time)
JsonParseErrorType pg_parse_json(JsonLexContext *lex, const JsonSemAction *sem)
JsonLexContext * makeJsonLexContextCstringLen(JsonLexContext *lex, const char *json, size_t len, int encoding, bool need_escapes)
void setJsonLexContextOwnsTokens(JsonLexContext *lex, bool owned_by_context)
char * json_errdetail(JsonParseErrorType error, JsonLexContext *lex)
void freeJsonLexContext(JsonLexContext *lex)
int(* PQauthDataHook_type)(PGauthData type, PGconn *conn, void *data)
PostgresPollingStatusType
@ PQAUTHDATA_PROMPT_OAUTH_DEVICE
static bool drain_timer_events(struct async_ctx *actx, bool *was_expired)
static char * urlencode(const char *s)
static bool setup_multiplexer(struct async_ctx *actx)
static bool finish_token_request(struct async_ctx *actx, struct token *tok)
static JsonParseErrorType oauth_json_array_end(void *state)
static void append_urlencoded(PQExpBuffer buf, const char *s)
static bool start_token_request(struct async_ctx *actx, PGconn *conn)
#define MAX_OAUTH_RESPONSE_SIZE
static bool parse_token_error(struct async_ctx *actx, struct token_error *err)
int pg_start_oauthbearer(PGconn *conn, PGoauthBearerRequestV2 *request)
static bool add_client_identification(struct async_ctx *actx, PQExpBuffer reqbody, PGconn *conn)
static int parse_interval(struct async_ctx *actx, const char *interval_str)
static void free_provider(struct provider *provider)
static void build_urlencoded(PQExpBuffer buf, const char *key, const char *value)
#define PG_CURL_IGNORE_DEPRECATION(x)
#define oauth_parse_set_error_internal(ctx, fmt,...)
static void record_token_error(struct async_ctx *actx, const struct token_error *err)
static bool parse_device_authz(struct async_ctx *actx, struct device_authz *authz)
static void report_type_mismatch(struct oauth_parse *ctx)
static int register_socket(CURL *curl, curl_socket_t socket, int what, void *ctx, void *socketp)
#define PG_OAUTH_OPTIONAL
static bool set_timer(struct async_ctx *actx, long timeout)
#define actx_error_internal(ACTX, FMT,...)
static bool parse_access_token(struct async_ctx *actx, struct token *tok)
static int timer_expired(struct async_ctx *actx)
static PostgresPollingStatusType drive_request(struct async_ctx *actx)
static bool start_device_authz(struct async_ctx *actx, PGconn *conn)
static bool prompt_user(struct async_ctx *actx, PGconn *conn)
#define CHECK_MSETOPT(ACTX, OPT, VAL, FAILACTION)
static bool finish_discovery(struct async_ctx *actx)
static void append_actx_error(PGoauthBearerRequestV2 *req, struct async_ctx *actx)
static double parse_json_number(const char *s)
static bool start_discovery(struct async_ctx *actx, const char *discovery_uri)
static JsonParseErrorType oauth_json_object_field_start(void *state, char *name, bool isnull)
static JsonParseErrorType oauth_json_scalar(void *state, char *token, JsonTokenType type)
static void free_token_error(struct token_error *err)
#define actx_error_str(ACTX, S)
static bool finish_device_authz(struct async_ctx *actx)
static size_t append_data(char *buf, size_t size, size_t nmemb, void *userdata)
#define CHECK_SETOPT(ACTX, OPT, VAL, FAILACTION)
static bool parse_oauth_json(struct async_ctx *actx, const struct json_field *fields)
#define MAX_OAUTH_NESTING_LEVEL
#define OAUTH_GRANT_TYPE_DEVICE_CODE
static PostgresPollingStatusType pg_fe_run_oauth_flow(PGconn *conn, struct PGoauthBearerRequest *request, int *altsock)
static JsonParseErrorType oauth_json_array_start(void *state)
static JsonParseErrorType oauth_json_object_end(void *state)
static void pg_fe_cleanup_oauth_flow(PGconn *conn, PGoauthBearerRequest *request)
static bool initialize_curl(PGoauthBearerRequestV2 *req)
static int debug_callback(CURL *handle, curl_infotype type, char *data, size_t size, void *clientp)
static PostgresPollingStatusType pg_fe_run_oauth_flow_impl(PGconn *conn, PGoauthBearerRequestV2 *request, int *altsock)
static void free_token(struct token *tok)
#define oauth_parse_set_error(ctx, fmt,...)
static bool comb_multiplexer(struct async_ctx *actx)
@ OAUTH_STEP_DEVICE_AUTHORIZATION
@ OAUTH_STEP_WAIT_INTERVAL
@ OAUTH_STEP_TOKEN_REQUEST
static int register_timer(CURLM *curlm, long timeout, void *ctx)
static void free_async_ctx(struct async_ctx *actx)
#define CHECK_GETINFO(ACTX, INFO, OUT, FAILACTION)
static bool check_content_type(struct async_ctx *actx, const char *type)
static bool check_issuer(struct async_ctx *actx, PGconn *conn)
#define actx_error(ACTX, FMT,...)
static bool parse_provider(struct async_ctx *actx, struct provider *provider)
static bool start_request(struct async_ctx *actx)
static int parse_expires_in(struct async_ctx *actx, const char *expires_in_str)
static void free_device_authz(struct device_authz *authz)
static bool handle_token_response(struct async_ctx *actx, char **token)
static JsonParseErrorType oauth_json_object_start(void *state)
#define PG_OAUTH_REQUIRED
static bool check_for_device_flow(struct async_ctx *actx)
static bool setup_curl_handles(struct async_ctx *actx)
void pq_reset_sigpipe(sigset_t *osigset, bool sigpipe_pending, bool got_epipe)
int pq_block_sigpipe(sigset_t *osigset, bool *sigpipe_pending)
bool oauth_unsafe_debugging_enabled(void)
#define pgunlock_thread()
static char buf[DEFAULT_XLOG_SEG_SIZE]
void explicit_bzero(void *buf, size_t len)
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
void initPQExpBuffer(PQExpBuffer str)
void resetPQExpBuffer(PQExpBuffer str)
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
void appendBinaryPQExpBuffer(PQExpBuffer str, const char *data, size_t datalen)
void appendPQExpBufferChar(PQExpBuffer str, char ch)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
void termPQExpBuffer(PQExpBuffer str)
#define PQExpBufferBroken(str)
#define PQExpBufferDataBroken(buf)
json_struct_action array_end
json_struct_action object_start
json_ofield_action object_field_start
json_scalar_action scalar
json_struct_action array_start
json_struct_action object_end
const char * verification_uri
struct device_authz authz
PQExpBufferData work_data
const char * discovery_uri
char curl_err[CURL_ERROR_SIZE]
struct curl_slist * headers
char * verification_uri_complete
struct curl_slist ** array
const struct json_field * active
const struct json_field * fields
char * device_authorization_endpoint
struct curl_slist * grant_types_supported
int pg_encoding_verifymbstr(int encoding, const char *mbstr, int len)
#define socket(af, type, protocol)