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

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