-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathPostgreSQLConfig.java
More file actions
813 lines (670 loc) · 31.2 KB
/
PostgreSQLConfig.java
File metadata and controls
813 lines (670 loc) · 31.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
/*
* Copyright (c) 2020, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* You may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/
package io.supertokens.storage.postgresql.config;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.supertokens.pluginInterface.ConfigFieldInfo;
import io.supertokens.pluginInterface.exceptions.InvalidConfigException;
import io.supertokens.storage.postgresql.Start;
import io.supertokens.storage.postgresql.annotations.*;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.*;
@JsonIgnoreProperties(ignoreUnknown = true)
public class PostgreSQLConfig {
@JsonProperty
@IgnoreForAnnotationCheck
private int postgresql_config_version = -1;
@EnvName("POSTGRESQL_CONNECTION_POOL_SIZE")
@JsonProperty
@ConnectionPoolProperty
@DashboardInfo(
description = "Defines the connection pool size to PostgreSQL. Please see https://github" +
".com/brettwooldridge/HikariCP/wiki/About-Pool-Sizing",
defaultValue = "10", isOptional = true, isEditable = true)
private int postgresql_connection_pool_size = 10;
@EnvName("POSTGRESQL_HOST")
@JsonProperty
@UserPoolProperty
@DashboardInfo(
description = "Specify the postgresql host url here. For example: - \"localhost\" - \"192.168.0.1\" - " +
"\"<IP to cloud instance>\" - \"example.com\"",
defaultValue = "\"localhost\"", isOptional = true)
private String postgresql_host = null;
@EnvName("POSTGRESQL_PORT")
@JsonProperty
@UserPoolProperty
@DashboardInfo(description = "Specify the port to use when connecting to PostgreSQL instance.",
defaultValue = "5432", isOptional = true)
private int postgresql_port = -1;
@EnvName("POSTGRESQL_USER")
@JsonProperty
@ConnectionPoolProperty
@DashboardInfo(
description = "The PostgreSQL user to use to query the database. If the relevant tables are not already " +
"created by you, this user should have the ability to create new tables. To see the tables " +
"needed, visit: https://supertokens.com/docs/thirdpartyemailpassword/pre-built-ui/setup/database" +
"-setup/postgresql",
defaultValue = "null")
private String postgresql_user = null;
@EnvName("POSTGRESQL_PASSWORD")
@JsonProperty
@ConnectionPoolProperty
@DashboardInfo(
description = "Password for the PostgreSQL user. If you have not set a password make this an empty string.",
defaultValue = "null")
private String postgresql_password = null;
@EnvName("POSTGRESQL_DATABASE_NAME")
@JsonProperty
@UserPoolProperty
@DashboardInfo(description = "The database name to store SuperTokens related data.",
defaultValue = "\"supertokens\"", isOptional = true)
private String postgresql_database_name = null;
@EnvName("POSTGRESQL_TABLE_NAMES_PREFIX")
@JsonProperty
@NotConflictingWithinUserPool
@DashboardInfo(
description = "A prefix to add to all table names managed by SuperTokens. An \"_\" will be added between " +
"this prefix and the actual table name if the prefix is defined.",
defaultValue = "\"\"", isOptional = true)
private String postgresql_table_names_prefix = "";
@EnvName("POSTGRESQL_KEY_VALUE_TABLE_NAME")
@JsonProperty
@NotConflictingWithinUserPool
@DashboardInfo(
description = "Specify the name of the table that will store secret keys and app info necessary for the " +
"functioning sessions.",
defaultValue = "\"key_value\"", isOptional = true)
private String postgresql_key_value_table_name = null;
@EnvName("POSTGRESQL_SESSION_INFO_TABLE_NAME")
@JsonProperty
@NotConflictingWithinUserPool
@DashboardInfo(description = "Specify the name of the table that will store the session info for users.",
defaultValue = "\"session_info\"", isOptional = true)
private String postgresql_session_info_table_name = null;
@EnvName("POSTGRESQL_EMAILPASSWORD_USERS_TABLE_NAME")
@JsonProperty
@NotConflictingWithinUserPool
@DashboardInfo(
description = "Specify the name of the table that will store the user information, along with their email" +
" and hashed password.",
defaultValue = "\"emailpassword_users\"", isOptional = true)
private String postgresql_emailpassword_users_table_name = null;
@EnvName("POSTGRESQL_EMAILPASSWORD_PSWD_RESET_TOKENS_TABLE_NAME")
@JsonProperty
@NotConflictingWithinUserPool
@DashboardInfo(description = "Specify the name of the table that will store the password reset tokens for users.",
defaultValue = "\"emailpassword_pswd_reset_tokens\"", isOptional = true)
private String postgresql_emailpassword_pswd_reset_tokens_table_name = null;
@EnvName("POSTGRESQL_EMAILVERIFICATION_TOKENS_TABLE_NAME")
@JsonProperty
@NotConflictingWithinUserPool
@DashboardInfo(
description = "Specify the name of the table that will store the email verification tokens for users.",
defaultValue = "\"emailverification_tokens\"", isOptional = true)
private String postgresql_emailverification_tokens_table_name = null;
@EnvName("POSTGRESQL_EMAILVERIFICATION_VERIFIED_EMAILS_TABLE_NAME")
@JsonProperty
@NotConflictingWithinUserPool
@DashboardInfo(description = "Specify the name of the table that will store the verified email addresses.",
defaultValue = "\"emailverification_verified_emails\"", isOptional = true)
private String postgresql_emailverification_verified_emails_table_name = null;
@EnvName("POSTGRESQL_THIRDPARTY_USERS_TABLE_NAME")
@JsonProperty
@NotConflictingWithinUserPool
@DashboardInfo(description = "Specify the name of the table that will store the thirdparty recipe users.",
defaultValue = "\"thirdparty_users\"", isOptional = true)
private String postgresql_thirdparty_users_table_name = null;
@EnvName("POSTGRESQL_TABLE_SCHEMA")
@JsonProperty
@UserPoolProperty
@DashboardInfo(description = "The schema for tables.", defaultValue = "\"public\"", isOptional = true)
private String postgresql_table_schema = "public";
@EnvName("POSTGRESQL_CONNECTION_URI")
@JsonProperty
@IgnoreForAnnotationCheck
@DashboardInfo(
description = "Specify the PostgreSQL connection URI in the following format: " +
"postgresql://[user[:[password]]@]host[:port][/dbname][?attr1=val1&attr2=val2... Values provided " +
"via other configs will override values provided by this config.",
defaultValue = "null", isOptional = true)
private String postgresql_connection_uri = null;
@EnvName("POSTGRESQL_CONNECTION_ATTRIBUTES")
@ConnectionPoolProperty
@DashboardInfo(description = "The connection attributes of the PostgreSQL database.",
defaultValue = "\"allowPublicKeyRetrieval=true\"", isOptional = true)
private String postgresql_connection_attributes = "allowPublicKeyRetrieval=true";
@EnvName("POSTGRESQL_CONNECTION_SCHEME")
@ConnectionPoolProperty
@DashboardInfo(description = "The scheme of the PostgreSQL database.", defaultValue = "\"postgresql\"",
isOptional = true)
private String postgresql_connection_scheme = "postgresql";
@EnvName("POSTGRESQL_IDLE_CONNECTION_TIMEOUT")
@JsonProperty
@ConnectionPoolProperty
@DashboardInfo(description = "Timeout in milliseconds for the idle connections to be closed.",
defaultValue = "60000", isOptional = true, isEditable = true)
private long postgresql_idle_connection_timeout = 60000;
@EnvName("POSTGRESQL_MINIMUM_IDLE_CONNECTIONS")
@JsonProperty
@ConnectionPoolProperty
@DashboardInfo(
description = "Minimum number of idle connections to be kept active. If not set, minimum idle connections" +
" will be same as the connection pool size.",
defaultValue = "null", isOptional = true, isEditable = true)
private Integer postgresql_minimum_idle_connections = null;
@IgnoreForAnnotationCheck
boolean isValidAndNormalised = false;
public PostgreSQLConfig() {
}
public static Set<String> getValidFields() {
PostgreSQLConfig config = new PostgreSQLConfig();
JsonObject configObj = new GsonBuilder().serializeNulls().create().toJsonTree(config).getAsJsonObject();
Set<String> validFields = new HashSet<>();
for (Map.Entry<String, JsonElement> entry : configObj.entrySet()) {
validFields.add(entry.getKey());
}
return validFields;
}
public static ArrayList<ConfigFieldInfo> getConfigFieldsInfoForDashboard(Start start) {
ArrayList<ConfigFieldInfo> result = new ArrayList<ConfigFieldInfo>();
JsonObject tenantConfig = new Gson().toJsonTree(Config.getConfig(start)).getAsJsonObject();
PostgreSQLConfig defaultConfigObj = new PostgreSQLConfig();
try {
defaultConfigObj.validateAndNormalise(true); // skip validation and just populate defaults
} catch (InvalidConfigException e) {
throw new IllegalStateException(e); // should never happen
}
JsonObject defaultConfig = new Gson().toJsonTree(defaultConfigObj).getAsJsonObject();
for (String fieldId : PostgreSQLConfig.getValidFields()) {
try {
Field field = PostgreSQLConfig.class.getDeclaredField(fieldId);
if (!field.isAnnotationPresent(DashboardInfo.class)) {
continue;
}
if (field.getName().endsWith("_table_name")) {
continue; // do not show
}
String key = field.getName();
String description = field.isAnnotationPresent(DashboardInfo.class)
? field.getAnnotation(DashboardInfo.class).description()
: "";
boolean isDifferentAcrossTenants = true;
String valueType = null;
Class<?> fieldType = field.getType();
if (fieldType == String.class) {
valueType = "string";
} else if (fieldType == boolean.class) {
valueType = "boolean";
} else if (fieldType == int.class || fieldType == long.class || fieldType == Integer.class) {
valueType = "number";
} else {
throw new RuntimeException("Unknown field type " + fieldType.getName());
}
JsonElement value = tenantConfig.get(field.getName());
JsonElement defaultValue = defaultConfig.get(field.getName());
boolean isNullable = defaultValue == null;
boolean isEditable = field.getAnnotation(DashboardInfo.class).isEditable();
result.add(new ConfigFieldInfo(
key, valueType, value, description, isDifferentAcrossTenants,
null, isNullable, defaultValue, true, isEditable));
} catch (NoSuchFieldException e) {
continue;
}
}
return result;
}
public String getTableSchema() {
return postgresql_table_schema;
}
public int getConnectionPoolSize() {
return postgresql_connection_pool_size;
}
public String getConnectionScheme() {
return postgresql_connection_scheme;
}
public String getConnectionAttributes() {
return postgresql_connection_attributes;
}
public String getHostName() {
return postgresql_host;
}
public int getPort() {
return postgresql_port;
}
public String getUser() {
return postgresql_user;
}
public String getPassword() {
return postgresql_password;
}
public String getDatabaseName() {
return postgresql_database_name;
}
public String getConnectionURI() {
return postgresql_connection_uri;
}
public String getUsersTable() {
return addSchemaAndPrefixToTableName("all_auth_recipe_users");
}
public String getAppsTable() {
return addSchemaAndPrefixToTableName("apps");
}
public String getTenantsTable() {
return addSchemaAndPrefixToTableName("tenants");
}
public String getTenantConfigsTable() {
return addSchemaAndPrefixToTableName("tenant_configs");
}
public String getTenantFirstFactorsTable() {
return addSchemaAndPrefixToTableName("tenant_first_factors");
}
public String getTenantRequiredSecondaryFactorsTable() {
return addSchemaAndPrefixToTableName("tenant_required_secondary_factors");
}
public String getTenantThirdPartyProvidersTable() {
return addSchemaAndPrefixToTableName("tenant_thirdparty_providers");
}
public String getTenantThirdPartyProviderClientsTable() {
return addSchemaAndPrefixToTableName("tenant_thirdparty_provider_clients");
}
public String getKeyValueTable() {
return postgresql_key_value_table_name;
}
public String getAppIdToUserIdTable() {
return addSchemaAndPrefixToTableName("app_id_to_user_id");
}
public String getUserLastActiveTable() {
return addSchemaAndPrefixToTableName("user_last_active");
}
public String getAccessTokenSigningKeysTable() {
return addSchemaAndPrefixToTableName("session_access_token_signing_keys");
}
public String getSessionInfoTable() {
return postgresql_session_info_table_name;
}
public String getEmailPasswordUserToTenantTable() {
return addSchemaAndPrefixToTableName("emailpassword_user_to_tenant");
}
public String getEmailPasswordUsersTable() {
return postgresql_emailpassword_users_table_name;
}
public String getPasswordResetTokensTable() {
return postgresql_emailpassword_pswd_reset_tokens_table_name;
}
public String getEmailVerificationTokensTable() {
return postgresql_emailverification_tokens_table_name;
}
public String getEmailVerificationTable() {
return postgresql_emailverification_verified_emails_table_name;
}
public String getThirdPartyUsersTable() {
return postgresql_thirdparty_users_table_name;
}
public long getIdleConnectionTimeout() {
return postgresql_idle_connection_timeout;
}
public Integer getMinimumIdleConnections() {
return postgresql_minimum_idle_connections;
}
public String getThirdPartyUserToTenantTable() {
return addSchemaAndPrefixToTableName("thirdparty_user_to_tenant");
}
public String getPasswordlessUsersTable() {
return addSchemaAndPrefixToTableName("passwordless_users");
}
public String getPasswordlessUserToTenantTable() {
return addSchemaAndPrefixToTableName("passwordless_user_to_tenant");
}
public String getPasswordlessDevicesTable() {
return addSchemaAndPrefixToTableName("passwordless_devices");
}
public String getPasswordlessCodesTable() {
return addSchemaAndPrefixToTableName("passwordless_codes");
}
public String getJWTSigningKeysTable() {
return addSchemaAndPrefixToTableName("jwt_signing_keys");
}
public String getUserMetadataTable() {
return addSchemaAndPrefixToTableName("user_metadata");
}
public String getRolesTable() {
return addSchemaAndPrefixToTableName("roles");
}
public String getUserRolesPermissionsTable() {
return addSchemaAndPrefixToTableName("role_permissions");
}
public String getUserRolesTable() {
return addSchemaAndPrefixToTableName("user_roles");
}
public String getUserIdMappingTable() {
return addSchemaAndPrefixToTableName("userid_mapping");
}
public String getTablePrefix() {
return postgresql_table_names_prefix;
}
public String getDashboardUsersTable() {
return addSchemaAndPrefixToTableName("dashboard_users");
}
public String getDashboardSessionsTable() {
return addSchemaAndPrefixToTableName("dashboard_user_sessions");
}
public String getTotpUsersTable() {
return addSchemaAndPrefixToTableName("totp_users");
}
public String getTotpUserDevicesTable() {
return addSchemaAndPrefixToTableName("totp_user_devices");
}
public String getTotpUsedCodesTable() {
return addSchemaAndPrefixToTableName("totp_used_codes");
}
public String getOAuthClientsTable() {
return addSchemaAndPrefixToTableName("oauth_clients");
}
public String getOAuthM2MTokensTable() {
return addSchemaAndPrefixToTableName("oauth_m2m_tokens");
}
public String getOAuthSessionsTable() {
return addSchemaAndPrefixToTableName("oauth_sessions");
}
public String getOAuthLogoutChallengesTable() {
return addSchemaAndPrefixToTableName("oauth_logout_challenges");
}
public String getWebAuthNUsersTable(){ return addSchemaAndPrefixToTableName("webauthn_users");}
public String getWebAuthNUserToTenantTable(){ return addSchemaAndPrefixToTableName("webauthn_user_to_tenant"); }
public String getWebAuthNGeneratedOptionsTable() { return addSchemaAndPrefixToTableName("webauthn_generated_options"); }
public String getWebAuthNCredentialsTable() { return addSchemaAndPrefixToTableName("webauthn_credentials"); }
public String getWebAuthNAccountRecoveryTokenTable() { return addSchemaAndPrefixToTableName("webauthn_account_recovery_tokens"); }
public String getSAMLClientsTable() { return addSchemaAndPrefixToTableName("saml_clients"); }
public String getSAMLRelayStateTable() { return addSchemaAndPrefixToTableName("saml_relay_state"); }
public String getSAMLClaimsTable() { return addSchemaAndPrefixToTableName("saml_claims"); }
public String getBulkImportUsersTable() {
return addSchemaAndPrefixToTableName("bulk_import_users");
}
public String getRecipeUserAccountInfosTable() {
return addSchemaAndPrefixToTableName("recipe_user_account_infos");
}
public String getRecipeUserTenantsTable() {
return addSchemaAndPrefixToTableName("recipe_user_tenants");
}
public String getPrimaryUserTenantsTable() {
return addSchemaAndPrefixToTableName("primary_user_tenants");
}
private String addSchemaAndPrefixToTableName(String tableName) {
return addSchemaToTableName(postgresql_table_names_prefix + tableName);
}
private String addSchemaToTableName(String tableName) {
String name = tableName;
if (!getTableSchema().equals("public")) {
name = getTableSchema() + "." + name;
}
return name;
}
public void validateAndNormalise() throws InvalidConfigException {
validateAndNormalise(false);
}
private void validateAndNormalise(boolean skipValidation) throws InvalidConfigException {
if (isValidAndNormalised) {
return;
}
if (!skipValidation) {
if (postgresql_connection_uri != null) {
try {
URI ignored = URI.create(postgresql_connection_uri);
} catch (Exception e) {
throw new InvalidConfigException(
"The provided postgresql connection URI has an incorrect format. Please use a format like "
+
"postgresql://[user[:[password]]@]host[:port][/dbname][?attr1=val1&attr2=val2...");
}
} else {
if (this.getUser() == null) {
throw new InvalidConfigException(
"'postgresql_user' and 'postgresql_connection_uri' are not set. Please set at least one of "
+ "these values");
}
}
if (postgresql_connection_pool_size <= 0) {
throw new InvalidConfigException(
"'postgresql_connection_pool_size' in the config.yaml file must be > 0");
}
if (postgresql_minimum_idle_connections != null) {
if (postgresql_minimum_idle_connections < 0) {
throw new InvalidConfigException(
"'postgresql_minimum_idle_connections' must be >= 0");
}
if (postgresql_minimum_idle_connections > postgresql_connection_pool_size) {
throw new InvalidConfigException(
"'postgresql_minimum_idle_connections' must be less than or equal to "
+ "'postgresql_connection_pool_size'");
}
}
}
// Normalisation
if (postgresql_connection_uri != null) {
{ // postgresql_connection_attributes
URI uri = URI.create(postgresql_connection_uri);
String query = uri.getQuery();
if (query != null) {
if (query.contains("allowPublicKeyRetrieval=")) {
postgresql_connection_attributes = query;
} else {
postgresql_connection_attributes = query + "&allowPublicKeyRetrieval=true";
}
}
}
{ // postgresql_table_schema
String connectionAttributes = postgresql_connection_attributes;
if (connectionAttributes.contains("currentSchema=")) {
String[] splitted = connectionAttributes.split("currentSchema=");
String valueStr = splitted[1];
if (valueStr.contains("&")) {
postgresql_table_schema = valueStr.split("&")[0];
} else {
postgresql_table_schema = valueStr;
}
postgresql_table_schema = postgresql_table_schema.trim();
}
}
{ // postgresql_host
URI uri = URI.create(postgresql_connection_uri);
if (uri.getHost() != null) {
postgresql_host = uri.getHost();
}
}
{ // postgresql_port
URI uri = URI.create(postgresql_connection_uri);
if (uri.getPort() > 0) {
postgresql_port = uri.getPort();
}
}
{ // postgresql_connection_scheme
URI uri = URI.create(postgresql_connection_uri);
// sometimes if the scheme is missing, the host is returned as the scheme. To
// prevent that,
// we have a check
String host = postgresql_host;
if (uri.getScheme() != null && !uri.getScheme().equals(host)) {
postgresql_connection_scheme = uri.getScheme();
}
}
{
URI uri = URI.create(postgresql_connection_uri);
String userInfo = uri.getUserInfo();
if (userInfo != null) {
String[] userInfoArray = userInfo.split(":");
if (userInfoArray.length > 0 && !userInfoArray[0].equals("")) {
postgresql_user = userInfoArray[0];
}
}
}
{
URI uri = URI.create(postgresql_connection_uri);
String userInfo = uri.getUserInfo();
if (userInfo != null) {
String[] userInfoArray = userInfo.split(":");
if (userInfoArray.length > 1 && !userInfoArray[1].equals("")) {
postgresql_password = userInfoArray[1];
}
}
}
{
URI uri = URI.create(postgresql_connection_uri);
String path = uri.getPath();
if (path != null && !path.equals("") && !path.equals("/")) {
if (path.startsWith("/")) {
postgresql_database_name = path.substring(1);
} else {
postgresql_database_name = path;
}
}
}
}
{ // postgresql_table_names_prefix
if (postgresql_table_names_prefix == null) {
postgresql_table_names_prefix = "";
}
postgresql_table_names_prefix = postgresql_table_names_prefix.trim();
if (!postgresql_table_names_prefix.isEmpty()) {
postgresql_table_names_prefix = postgresql_table_names_prefix + "_";
}
}
{ // postgresql_table_schema
postgresql_table_schema = postgresql_table_schema.trim();
}
{ // postgresql_connection_scheme
postgresql_connection_scheme = postgresql_connection_scheme.trim();
}
{ // postgresql_host
if (postgresql_host == null) {
postgresql_host = "localhost";
}
}
{ // postgresql_port
if (postgresql_port < 0) {
postgresql_port = Integer.parseInt(System.getProperty("ST_POSTGRESQL_PLUGIN_SERVER_PORT", "5432"));
}
}
{ // postgresql_database_name
if (postgresql_database_name == null) {
postgresql_database_name = "supertokens";
}
postgresql_database_name = postgresql_database_name.trim();
}
if (postgresql_key_value_table_name != null) {
postgresql_key_value_table_name = addSchemaToTableName(postgresql_key_value_table_name);
} else {
postgresql_key_value_table_name = addSchemaAndPrefixToTableName("key_value");
}
if (postgresql_session_info_table_name != null) {
postgresql_session_info_table_name = addSchemaToTableName(postgresql_session_info_table_name);
} else {
postgresql_session_info_table_name = addSchemaAndPrefixToTableName("session_info");
}
if (postgresql_emailpassword_users_table_name != null) {
postgresql_emailpassword_users_table_name = addSchemaToTableName(postgresql_emailpassword_users_table_name);
} else {
postgresql_emailpassword_users_table_name = addSchemaAndPrefixToTableName("emailpassword_users");
}
if (postgresql_emailpassword_pswd_reset_tokens_table_name != null) {
postgresql_emailpassword_pswd_reset_tokens_table_name = addSchemaToTableName(
postgresql_emailpassword_pswd_reset_tokens_table_name);
} else {
postgresql_emailpassword_pswd_reset_tokens_table_name = addSchemaAndPrefixToTableName(
"emailpassword_pswd_reset_tokens");
}
if (postgresql_emailverification_tokens_table_name != null) {
postgresql_emailverification_tokens_table_name = addSchemaToTableName(
postgresql_emailverification_tokens_table_name);
} else {
postgresql_emailverification_tokens_table_name = addSchemaAndPrefixToTableName("emailverification_tokens");
}
if (postgresql_emailverification_verified_emails_table_name != null) {
postgresql_emailverification_verified_emails_table_name = addSchemaToTableName(
postgresql_emailverification_verified_emails_table_name);
} else {
postgresql_emailverification_verified_emails_table_name = addSchemaAndPrefixToTableName(
"emailverification_verified_emails");
}
if (postgresql_thirdparty_users_table_name != null) {
postgresql_thirdparty_users_table_name = addSchemaToTableName(postgresql_thirdparty_users_table_name);
} else {
postgresql_thirdparty_users_table_name = addSchemaAndPrefixToTableName("thirdparty_users");
}
isValidAndNormalised = true;
}
void assertThatConfigFromSameUserPoolIsNotConflicting(PostgreSQLConfig otherConfig) throws InvalidConfigException {
for (Field field : PostgreSQLConfig.class.getDeclaredFields()) {
if (field.isAnnotationPresent(NotConflictingWithinUserPool.class)) {
try {
if (!Objects.equals(field.get(this), field.get(otherConfig))) {
throw new InvalidConfigException(
"You cannot set different values for " + field.getName() +
" for the same user pool");
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
}
public String getUserPoolId() {
StringBuilder userPoolId = new StringBuilder();
for (Field field : PostgreSQLConfig.class.getDeclaredFields()) {
if (field.isAnnotationPresent(UserPoolProperty.class)) {
userPoolId.append("|");
try {
if (field.get(this) != null) {
userPoolId.append(field.get(this).toString());
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
return userPoolId.toString();
}
public String getConnectionPoolId() {
StringBuilder connectionPoolId = new StringBuilder();
for (Field field : PostgreSQLConfig.class.getDeclaredFields()) {
if (field.isAnnotationPresent(ConnectionPoolProperty.class)) {
try {
String fieldName = field.getName();
String fieldValue = field.get(this) != null ? field.get(this).toString() : null;
if (fieldValue == null) {
continue;
}
// To ensure a unique connectionPoolId we include the database password and use the "|db_pass|"
// identifier.
// This facilitates easy removal of the password from logs when necessary.
if (fieldName.equals("postgresql_password")) {
connectionPoolId.append("|db_pass|" + fieldValue + "|db_pass");
} else {
connectionPoolId.append("|" + fieldValue);
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
return connectionPoolId.toString();
}
}