|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, VRAI Labs and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This software is licensed under the Apache License, Version 2.0 (the |
| 5 | + * "License") as published by the Apache Software Foundation. |
| 6 | + * |
| 7 | + * You may not use this file except in compliance with the License. You may |
| 8 | + * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.supertokens.storage.postgresql.queries; |
| 18 | + |
| 19 | + |
| 20 | +import io.supertokens.storage.postgresql.Start; |
| 21 | +import io.supertokens.storage.postgresql.config.Config; |
| 22 | +import io.supertokens.storage.postgresql.utils.Utils; |
| 23 | + |
| 24 | +public class WebAuthNQueries { |
| 25 | + |
| 26 | + static String getQueryToCreateWebAuthNUsersTable(Start start){ |
| 27 | + String schema = Config.getConfig(start).getTableSchema(); |
| 28 | + String webAuthNUsersTableName = Config.getConfig(start).getWebAuthNUsersTable(); |
| 29 | + return "CREATE TABLE IF NOT EXISTS " + webAuthNUsersTableName + "(" + |
| 30 | + " app_id VARCHAR(64) DEFAULT 'public' NOT NULL," + |
| 31 | + " user_id CHAR(36) NOT NULL," + |
| 32 | + " email VARCHAR(256) NOT NULL," + |
| 33 | + " rp_id VARCHAR(256) NOT NULL," + |
| 34 | + " time_joined BIGINT NOT NULL," + |
| 35 | + " CONSTRAINT " + Utils.getConstraintName(schema, webAuthNUsersTableName, null, "pkey") + |
| 36 | + " PRIMARY KEY (app_id, user_id)," + |
| 37 | + " CONSTRAINT " + Utils.getConstraintName(schema,webAuthNUsersTableName, "user_id", "fkey") + |
| 38 | + " FOREIGN KEY (app_id, user_id) REFERENCES " + Config.getConfig(start).getAppIdToUserIdTable() + |
| 39 | + " (app_id, user_id) ON DELETE CASCADE " + |
| 40 | + ");"; |
| 41 | + } |
| 42 | + |
| 43 | + static String getQueryToCreateWebAuthNUsersToTenantTable(Start start){ |
| 44 | + String schema = Config.getConfig(start).getTableSchema(); |
| 45 | + String webAuthNUserToTenantTableName = Config.getConfig(start).getWebAuthNUserToTenantTable(); |
| 46 | + return "CREATE TABLE IF NOT EXISTS " + webAuthNUserToTenantTableName +" (" + |
| 47 | + " app_id VARCHAR(64) DEFAULT 'public' NOT NULL," + |
| 48 | + " tenant_id VARCHAR(64) DEFAULT 'public' NOT NULL," + |
| 49 | + " user_id CHAR(36) NOT NULL," + |
| 50 | + " email VARCHAR(256) NOT NULL," + |
| 51 | + " CONSTRAINT "+ Utils.getConstraintName(schema, webAuthNUserToTenantTableName, "email", "key") + |
| 52 | + " UNIQUE (app_id, tenant_id, email)," + |
| 53 | + " CONSTRAINT "+ Utils.getConstraintName(schema, webAuthNUserToTenantTableName, null, "pkey") + |
| 54 | + " PRIMARY KEY (app_id, tenant_id, user_id)," + |
| 55 | + " CONSTRAINT "+ Utils.getConstraintName(schema, webAuthNUserToTenantTableName, "user_id", "fkey") + |
| 56 | + " FOREIGN KEY (app_id, tenant_id, user_id) " + |
| 57 | + " REFERENCES "+ Config.getConfig(start).getUsersTable()+" (app_id, tenant_id, user_id) ON DELETE CASCADE" + |
| 58 | + ");"; |
| 59 | + } |
| 60 | + |
| 61 | + static String getQueryToCreateWebAuthNGeneratedOptionsTable(Start start){ |
| 62 | + String schema = Config.getConfig(start).getTableSchema(); |
| 63 | + String webAuthNGeneratedOptionsTable = Config.getConfig(start).getWebAuthNGeneratedOptionsTable(); |
| 64 | + return "CREATE TABLE IF NOT EXISTS " + webAuthNGeneratedOptionsTable + "(" + |
| 65 | + " app_id VARCHAR(64) DEFAULT 'public' NOT NULL," + |
| 66 | + " tenant_id VARCHAR(64) DEFAULT 'public' NOT NULL," + |
| 67 | + " id CHAR(36) NOT NULL," + |
| 68 | + " challenge VARCHAR(256) NOT NULL," + |
| 69 | + " email VARCHAR(256)," + |
| 70 | + " rp_id VARCHAR(256) NOT NULL," + |
| 71 | + " origin VARCHAR(256) NOT NULL," + |
| 72 | + " expires_at BIGINT NOT NULL," + |
| 73 | + " created_at BIGINT NOT NULL," + |
| 74 | + " CONSTRAINT " + Utils.getConstraintName(schema, webAuthNGeneratedOptionsTable, null, "pkey") + |
| 75 | + " PRIMARY KEY (app_id, tenant_id, id)," + |
| 76 | + " CONSTRAINT "+ Utils.getConstraintName(schema, webAuthNGeneratedOptionsTable, "tenant_id", "fkey") + |
| 77 | + " FOREIGN KEY (app_id, tenant_id) " + |
| 78 | + " REFERENCES " + Config.getConfig(start).getTenantsTable() + " (app_id, tenant_id) ON DELETE CASCADE" + |
| 79 | + ");"; |
| 80 | + } |
| 81 | + |
| 82 | + static String getQueryToCreateWebAuthNChallengeExpiresIndex(Start start) { |
| 83 | + return "CREATE INDEX webauthn_user_challenges_expires_at_index ON " + |
| 84 | + Config.getConfig(start).getWebAuthNGeneratedOptionsTable() + |
| 85 | + " (app_id, tenant_id, expires_at);"; |
| 86 | + } |
| 87 | + |
| 88 | + static String getQueryToCreateWebAuthNCredentialsTable(Start start){ |
| 89 | + String schema = Config.getConfig(start).getTableSchema(); |
| 90 | + String webAuthNCredentialsTable = Config.getConfig(start).getWebAuthNCredentialsTable(); |
| 91 | + return "CREATE TABLE IF NOT EXISTS "+ webAuthNCredentialsTable + "(" + |
| 92 | + " id VARCHAR(256) NOT NULL," + |
| 93 | + " app_id VARCHAR(64) DEFAULT 'public'," + |
| 94 | + " rp_id VARCHAR(256)," + |
| 95 | + " user_id CHAR(36)," + |
| 96 | + " counter BIGINT NOT NULL," + |
| 97 | + " public_key BYTEA NOT NULL," + |
| 98 | + " transports TEXT NOT NULL," + // planned as TEXT[], which is not supported by sqlite |
| 99 | + " created_at BIGINT NOT NULL," + |
| 100 | + " updated_at BIGINT NOT NULL," + |
| 101 | + " CONSTRAINT " + Utils.getConstraintName(schema, webAuthNCredentialsTable, null, "pkey") + |
| 102 | + " PRIMARY KEY (app_id, rp_id, id)," + |
| 103 | + " CONSTRAINT "+ Utils.getConstraintName(schema, webAuthNCredentialsTable, "user_id", "fkey") + |
| 104 | + " FOREIGN KEY (app_id, user_id) REFERENCES " + |
| 105 | + Config.getConfig(start).getWebAuthNUsersTable() + " (app_id, user_id) ON DELETE CASCADE" + |
| 106 | + ");"; |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments