Skip to content

Commit 31c7c99

Browse files
committed
feat: creating webauthn related tables
1 parent 71a33d8 commit 31c7c99

File tree

3 files changed

+145
-1
lines changed

3 files changed

+145
-1
lines changed

src/main/java/io/supertokens/storage/postgresql/config/PostgreSQLConfig.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,14 @@ public String getOAuthLogoutChallengesTable() {
462462
return addSchemaAndPrefixToTableName("oauth_logout_challenges");
463463
}
464464

465+
public String getWebAuthNUsersTable(){ return addSchemaAndPrefixToTableName("webauthn_users");}
466+
467+
public String getWebAuthNUserToTenantTable(){ return addSchemaAndPrefixToTableName("webauthn_user_to_tenant"); }
468+
469+
public String getWebAuthNGeneratedOptionsTable() { return addSchemaAndPrefixToTableName("webauthn_generated_options"); }
470+
471+
public String getWebAuthNCredentialsTable() { return addSchemaAndPrefixToTableName("webauthn_credentials"); }
472+
465473
private String addSchemaAndPrefixToTableName(String tableName) {
466474
return addSchemaToTableName(postgresql_table_names_prefix + tableName);
467475
}

src/main/java/io/supertokens/storage/postgresql/queries/GeneralQueries.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,28 @@ public static void createTablesIfNotExists(Start start, Connection con) throws S
585585
update(con, OAuthQueries.getQueryToCreateOAuthLogoutChallengesTimeCreatedIndex(start), NO_OP_SETTER);
586586
}
587587

588+
if(!doesTableExists(start, con, Config.getConfig(start).getWebAuthNUsersTable())){
589+
getInstance(start).addState(CREATING_NEW_TABLE, null);
590+
update(con, WebAuthNQueries.getQueryToCreateWebAuthNUsersTable(start), NO_OP_SETTER);
591+
}
592+
593+
if(!doesTableExists(start, con, Config.getConfig(start).getWebAuthNUserToTenantTable())){
594+
getInstance(start).addState(CREATING_NEW_TABLE, null);
595+
update(con, WebAuthNQueries.getQueryToCreateWebAuthNUsersToTenantTable(start), NO_OP_SETTER);
596+
}
597+
598+
if(!doesTableExists(start, con, Config.getConfig(start).getWebAuthNGeneratedOptionsTable())){
599+
getInstance(start).addState(CREATING_NEW_TABLE, null);
600+
update(con, WebAuthNQueries.getQueryToCreateWebAuthNGeneratedOptionsTable(start), NO_OP_SETTER);
601+
//index
602+
update(con, WebAuthNQueries.getQueryToCreateWebAuthNChallengeExpiresIndex(start), NO_OP_SETTER);
603+
}
604+
605+
if(!doesTableExists(start, con, Config.getConfig(start).getWebAuthNCredentialsTable())){
606+
getInstance(start).addState(CREATING_NEW_TABLE, null);
607+
update(con, WebAuthNQueries.getQueryToCreateWebAuthNCredentialsTable(start), NO_OP_SETTER);
608+
}
609+
588610
} catch (Exception e) {
589611
if (e.getMessage().contains("schema") && e.getMessage().contains("does not exist")
590612
&& numberOfRetries < 1) {
@@ -664,7 +686,12 @@ public static void deleteAllTables(Start start) throws SQLException, StorageQuer
664686
+ getConfig(start).getOAuthClientsTable() + ","
665687
+ getConfig(start).getOAuthSessionsTable() + ","
666688
+ getConfig(start).getOAuthLogoutChallengesTable() + ","
667-
+ getConfig(start).getOAuthM2MTokensTable();
689+
+ getConfig(start).getOAuthM2MTokensTable() + ","
690+
+ getConfig(start).getWebAuthNCredentialsTable() + ","
691+
+ getConfig(start).getWebAuthNGeneratedOptionsTable() + ","
692+
+ getConfig(start).getWebAuthNUserToTenantTable() + ","
693+
+ getConfig(start).getWebAuthNUsersTable();
694+
668695
update(start, DROP_QUERY, NO_OP_SETTER);
669696
}
670697
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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

Comments
 (0)