PostgreSQL Source Code git master
Loading...
Searching...
No Matches
validator.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * validator.c
4 * Test module for serverside OAuth token validation callbacks
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 * src/test/modules/oauth_validator/validator.c
10 *
11 *-------------------------------------------------------------------------
12 */
13
14#include "postgres.h"
15
16#include "fmgr.h"
17#include "libpq/oauth.h"
18#include "miscadmin.h"
19#include "utils/guc.h"
20#include "utils/memutils.h"
21
23
27 const char *token,
28 const char *role,
30
31/* Callback implementations (exercise all three) */
39
40/* GUCs */
41static char *authn_id = NULL;
42static bool authorize_tokens = true;
43static char *error_detail = NULL;
44static bool internal_error = false;
45static bool invalid_hba = false;
46
47/* HBA options */
48static const char *hba_opts[] = {
49 "authn_id", /* overrides the default authn_id */
50 "log", /* logs an arbitrary string */
51};
52
53/*---
54 * Extension entry point. Sets up GUCs for use by tests:
55 *
56 * - oauth_validator.authn_id Sets the user identifier to return during token
57 * validation. Defaults to the username in the
58 * startup packet, or the validator.authn_id HBA
59 * option if it is set.
60 *
61 * - oauth_validator.authorize_tokens
62 * Sets whether to successfully validate incoming
63 * tokens. Defaults to true.
64 *
65 * - oauth_validator.error_detail
66 * Sets an error message to be included as a
67 * DETAIL on failure.
68 *
69 * - oauth_validator.internal_error
70 * Reports an internal error to the server.
71 */
72void
74{
75 DefineCustomStringVariable("oauth_validator.authn_id",
76 "Authenticated identity to use for future connections",
77 NULL,
78 &authn_id,
79 NULL,
81 0,
82 NULL, NULL, NULL);
83 DefineCustomBoolVariable("oauth_validator.authorize_tokens",
84 "Should tokens be marked valid?",
85 NULL,
87 true,
89 0,
90 NULL, NULL, NULL);
91 DefineCustomStringVariable("oauth_validator.error_detail",
92 "Error message to print during failures",
93 NULL,
95 NULL,
97 0,
98 NULL, NULL, NULL);
99 DefineCustomBoolVariable("oauth_validator.internal_error",
100 "Should the validator report an internal error?",
101 NULL,
103 false,
105 0,
106 NULL, NULL, NULL);
107 DefineCustomBoolVariable("oauth_validator.invalid_hba",
108 "Should the validator register an invalid option?",
109 NULL,
111 false,
113 0,
114 NULL, NULL, NULL);
115
116 MarkGUCPrefixReserved("oauth_validator");
117}
118
119/*
120 * Validator module entry point.
121 */
127
128#define PRIVATE_COOKIE ((void *) 13579)
129
130/*
131 * Startup callback, to set up private data for the validator.
132 */
133static void
135{
136 /*
137 * Make sure the server is correctly setting sversion. (Real modules
138 * should not do this; it would defeat upgrade compatibility.)
139 */
140 if (state->sversion != PG_VERSION_NUM)
141 elog(ERROR, "oauth_validator: sversion set to %d", state->sversion);
142
143 /*
144 * Test the behavior of custom HBA options. Registered options should not
145 * be retrievable during startup (we want to discourage modules from
146 * relying on the relative order of client connections and the
147 * startup_cb).
148 */
150 for (int i = 0; i < lengthof(hba_opts); i++)
151 {
153 elog(ERROR,
154 "oauth_validator: GetOAuthValidatorOption(\"%s\") was non-NULL during startup_cb",
155 hba_opts[i]);
156 }
157
158 if (invalid_hba)
159 {
160 /* Register a bad option, which should print a WARNING to the logs. */
161 const char *invalid = "bad option name";
162
164 }
165
166 state->private_data = PRIVATE_COOKIE;
167}
168
169/*
170 * Shutdown callback, to tear down the validator.
171 */
172static void
174{
175 /* Check to make sure our private state still exists. */
176 if (state->private_data != PRIVATE_COOKIE)
177 elog(PANIC, "oauth_validator: private state cookie changed to %p in shutdown",
178 state->private_data);
179}
180
181/*
182 * Validator implementation. Logs the incoming data and authorizes the token by
183 * default; the behavior can be modified via the module's GUC and HBA settings.
184 */
185static bool
187 const char *token, const char *role,
189{
190 /* Check to make sure our private state still exists. */
191 if (state->private_data != PRIVATE_COOKIE)
192 elog(ERROR, "oauth_validator: private state cookie changed to %p in validate",
193 state->private_data);
194
195 if (GetOAuthHBAOption(state, "log"))
196 elog(LOG, "%s", GetOAuthHBAOption(state, "log"));
197
198 elog(LOG, "oauth_validator: token=\"%s\", role=\"%s\"", token, role);
199 elog(LOG, "oauth_validator: issuer=\"%s\", scope=\"%s\"",
202
203 res->error_detail = error_detail; /* only relevant for failures */
204 if (internal_error)
205 return false;
206
208 if (authn_id)
209 res->authn_id = pstrdup(authn_id);
210 else if (GetOAuthHBAOption(state, "authn_id"))
211 res->authn_id = pstrdup(GetOAuthHBAOption(state, "authn_id"));
212 else
213 res->authn_id = pstrdup(role);
214
215 return true;
216}
const char * GetOAuthHBAOption(const ValidatorModuleState *state, const char *optname)
void RegisterOAuthHBAOptions(ValidatorModuleState *state, int num, const char *opts[])
Definition auth-oauth.c:949
#define lengthof(array)
Definition c.h:873
#define LOG
Definition elog.h:32
#define PANIC
Definition elog.h:44
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
struct Port * MyProcPort
Definition globals.c:53
void DefineCustomStringVariable(const char *name, const char *short_desc, const char *long_desc, char **valueAddr, const char *bootValue, GucContext context, int flags, GucStringCheckHook check_hook, GucStringAssignHook assign_hook, GucShowHook show_hook)
Definition guc.c:5129
void DefineCustomBoolVariable(const char *name, const char *short_desc, const char *long_desc, bool *valueAddr, bool bootValue, GucContext context, int flags, GucBoolCheckHook check_hook, GucBoolAssignHook assign_hook, GucShowHook show_hook)
Definition guc.c:5049
void MarkGUCPrefixReserved(const char *className)
Definition guc.c:5186
@ PGC_SIGHUP
Definition guc.h:75
invalidindex index d is invalid
Definition isn.c:138
int i
Definition isn.c:77
char * pstrdup(const char *in)
Definition mcxt.c:1781
#define PG_OAUTH_VALIDATOR_MAGIC
Definition oauth.h:88
static int fb(int x)
char * oauth_issuer
Definition hba.h:130
char * oauth_scope
Definition hba.h:131
ValidatorStartupCB startup_cb
Definition oauth.h:94
HbaLine * hba
Definition libpq-be.h:165
char * error_detail
Definition oauth.h:65
void _PG_init(void)
Definition validator.c:73
const OAuthValidatorCallbacks * _PG_oauth_validator_module_init(void)
Definition validator.c:123
#define PRIVATE_COOKIE
Definition validator.c:128
static bool internal_error
Definition validator.c:44
static char * error_detail
Definition validator.c:43
PG_MODULE_MAGIC
Definition validator.c:22
static bool invalid_hba
Definition validator.c:45
static bool authorize_tokens
Definition validator.c:42
static void validator_startup(ValidatorModuleState *state)
Definition validator.c:134
static void validator_shutdown(ValidatorModuleState *state)
Definition validator.c:173
static char * authn_id
Definition validator.c:41
static const OAuthValidatorCallbacks validator_callbacks
Definition validator.c:32
static bool validate_token(const ValidatorModuleState *state, const char *token, const char *role, ValidatorModuleResult *res)
Definition validator.c:186
static const char * hba_opts[]
Definition validator.c:48