PostgreSQL Source Code git master
Loading...
Searching...
No Matches
oauth-curl.h File Reference
#include "libpq-fe.h"
Include dependency graph for oauth-curl.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

PGDLLEXPORT int pg_start_oauthbearer (PGconn *conn, PGoauthBearerRequestV2 *request)
 

Function Documentation

◆ pg_start_oauthbearer()

PGDLLEXPORT int pg_start_oauthbearer ( PGconn conn,
PGoauthBearerRequestV2 request 
)
extern

Definition at line 3078 of file oauth-curl.c.

3079{
3080 struct async_ctx *actx;
3081 PQconninfoOption *conninfo = NULL;
3083
3084 /* Parse debug flags from the environment. */
3086
3088 return -1;
3089
3090 /*
3091 * Create our asynchronous state, and hook it into the upper-level OAuth
3092 * state immediately, so any failures below won't leak the context
3093 * allocation.
3094 */
3095 actx = calloc(1, sizeof(*actx));
3096 if (!actx)
3097 goto oom;
3098
3099 actx->mux = PGINVALID_SOCKET;
3100 actx->timerfd = -1;
3101
3102 /*
3103 * Now we have a valid (but still useless) actx, so we can fill in the
3104 * request object. From this point onward, failures will result in a call
3105 * to pg_fe_cleanup_oauth_flow(). Further cleanup logic belongs there.
3106 */
3107 request->v1.async = pg_fe_run_oauth_flow;
3108 request->v1.cleanup = pg_fe_cleanup_oauth_flow;
3109 request->v1.user = actx;
3110
3111 /*
3112 * Now finish filling in the actx.
3113 */
3114
3115 actx->debug_flags = debug_flags;
3116 initPQExpBuffer(&actx->work_data);
3117 initPQExpBuffer(&actx->errbuf);
3118
3119 /* Pull relevant connection options. */
3120 conninfo = PQconninfo(conn);
3121 if (!conninfo)
3122 goto oom;
3123
3124 for (PQconninfoOption *opt = conninfo; opt->keyword; opt++)
3125 {
3126 if (!opt->val)
3127 continue; /* simplifies the strdup logic below */
3128
3129 if (strcmp(opt->keyword, "oauth_client_id") == 0)
3130 {
3131 actx->client_id = strdup(opt->val);
3132 if (!actx->client_id)
3133 goto oom;
3134 }
3135 else if (strcmp(opt->keyword, "oauth_client_secret") == 0)
3136 {
3137 actx->client_secret = strdup(opt->val);
3138 if (!actx->client_secret)
3139 goto oom;
3140 }
3141 else if (strcmp(opt->keyword, "oauth_ca_file") == 0)
3142 {
3143 actx->ca_file = strdup(opt->val);
3144 if (!actx->ca_file)
3145 goto oom;
3146 }
3147 }
3148
3149 PQconninfoFree(conninfo);
3150 conninfo = NULL; /* keeps `goto oom` safe */
3151
3152 actx->discovery_uri = request->v1.openid_configuration;
3153 actx->issuer_id = request->issuer;
3154 actx->scope = request->v1.scope;
3155
3156 Assert(actx->client_id); /* ensured by setup_oauth_parameters() */
3157 Assert(actx->issuer_id); /* ensured by setup_oauth_parameters() */
3158 Assert(actx->discovery_uri); /* ensured by oauth_exchange() */
3159
3160 if (!setup_multiplexer(actx))
3161 {
3163 return -1;
3164 }
3165
3167 {
3169 return -1;
3170 }
3171
3172 return 0;
3173
3174oom:
3175 if (conninfo)
3176 PQconninfoFree(conninfo);
3177
3178 request->error = libpq_gettext("out of memory");
3179 return -1;
3180}
#define Assert(condition)
Definition c.h:943
uint32_t uint32
Definition c.h:624
PQconninfoOption * PQconninfo(PGconn *conn)
void PQconninfoFree(PQconninfoOption *connOptions)
static bool setup_multiplexer(struct async_ctx *actx)
static bool initialize_curl(PGoauthBearerRequestV2 *req, uint32 debug_flags)
static void append_actx_error(PGoauthBearerRequestV2 *req, struct async_ctx *actx)
Definition oauth-curl.c:379
static PostgresPollingStatusType pg_fe_run_oauth_flow(PGconn *conn, struct PGoauthBearerRequest *request, int *altsock)
static void pg_fe_cleanup_oauth_flow(PGconn *conn, PGoauthBearerRequest *request)
Definition oauth-curl.c:355
static bool setup_curl_handles(struct async_ctx *actx)
static uint32 oauth_parse_debug_flags(void)
Definition oauth-debug.h:79
#define libpq_gettext(x)
Definition oauth-utils.h:44
#define PGINVALID_SOCKET
Definition port.h:31
void initPQExpBuffer(PQExpBuffer str)
Definition pqexpbuffer.c:90
static int fb(int x)
#define calloc(a, b)
PGconn * conn
Definition streamutil.c:52
uint32 debug_flags
Definition oauth-curl.c:283

References append_actx_error(), Assert, calloc, conn, async_ctx::debug_flags, fb(), initialize_curl(), initPQExpBuffer(), _PQconninfoOption::keyword, libpq_gettext, oauth_parse_debug_flags(), pg_fe_cleanup_oauth_flow(), pg_fe_run_oauth_flow(), PGINVALID_SOCKET, PQconninfo(), PQconninfoFree(), setup_curl_handles(), and setup_multiplexer().