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 3063 of file oauth-curl.c.

3064{
3065 struct async_ctx *actx;
3066 PQconninfoOption *conninfo = NULL;
3067
3069 return -1;
3070
3071 /*
3072 * Create our asynchronous state, and hook it into the upper-level OAuth
3073 * state immediately, so any failures below won't leak the context
3074 * allocation.
3075 */
3076 actx = calloc(1, sizeof(*actx));
3077 if (!actx)
3078 goto oom;
3079
3080 actx->mux = PGINVALID_SOCKET;
3081 actx->timerfd = -1;
3082
3083 /*
3084 * Now we have a valid (but still useless) actx, so we can fill in the
3085 * request object. From this point onward, failures will result in a call
3086 * to pg_fe_cleanup_oauth_flow(). Further cleanup logic belongs there.
3087 */
3088 request->v1.async = pg_fe_run_oauth_flow;
3089 request->v1.cleanup = pg_fe_cleanup_oauth_flow;
3090 request->v1.user = actx;
3091
3092 /*
3093 * Now finish filling in the actx.
3094 */
3095
3096 /* Parse debug flags from the environment. */
3097 actx->debug_flags = oauth_parse_debug_flags();
3098
3099 initPQExpBuffer(&actx->work_data);
3100 initPQExpBuffer(&actx->errbuf);
3101
3102 /* Pull relevant connection options. */
3103 conninfo = PQconninfo(conn);
3104 if (!conninfo)
3105 goto oom;
3106
3107 for (PQconninfoOption *opt = conninfo; opt->keyword; opt++)
3108 {
3109 if (!opt->val)
3110 continue; /* simplifies the strdup logic below */
3111
3112 if (strcmp(opt->keyword, "oauth_client_id") == 0)
3113 {
3114 actx->client_id = strdup(opt->val);
3115 if (!actx->client_id)
3116 goto oom;
3117 }
3118 else if (strcmp(opt->keyword, "oauth_client_secret") == 0)
3119 {
3120 actx->client_secret = strdup(opt->val);
3121 if (!actx->client_secret)
3122 goto oom;
3123 }
3124 else if (strcmp(opt->keyword, "oauth_ca_file") == 0)
3125 {
3126 actx->ca_file = strdup(opt->val);
3127 if (!actx->ca_file)
3128 goto oom;
3129 }
3130 }
3131
3132 PQconninfoFree(conninfo);
3133 conninfo = NULL; /* keeps `goto oom` safe */
3134
3135 actx->discovery_uri = request->v1.openid_configuration;
3136 actx->issuer_id = request->issuer;
3137 actx->scope = request->v1.scope;
3138
3139 Assert(actx->client_id); /* ensured by setup_oauth_parameters() */
3140 Assert(actx->issuer_id); /* ensured by setup_oauth_parameters() */
3141 Assert(actx->discovery_uri); /* ensured by oauth_exchange() */
3142
3143 if (!setup_multiplexer(actx))
3144 {
3146 return -1;
3147 }
3148
3150 {
3152 return -1;
3153 }
3154
3155 return 0;
3156
3157oom:
3158 if (conninfo)
3159 PQconninfoFree(conninfo);
3160
3161 request->error = libpq_gettext("out of memory");
3162 return -1;
3163}
#define Assert(condition)
Definition c.h:943
PQconninfoOption * PQconninfo(PGconn *conn)
void PQconninfoFree(PQconninfoOption *connOptions)
static bool setup_multiplexer(struct async_ctx *actx)
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 initialize_curl(PGoauthBearerRequestV2 *req)
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

References append_actx_error(), Assert, calloc, conn, 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().