From c6fddca8ea5f7b37105cb125bd3315f805aef408 Mon Sep 17 00:00:00 2001 From: kashiuno Date: Tue, 7 Jan 2025 11:42:30 +0300 Subject: [PATCH] Change schema to fit auth server --- .../qr_access_api/repository/QrRepository.kt | 4 +- .../db/changelog/1.0.0/changelog.yml | 63 ++++++++++--------- .../database/utils/InsertDatabaseHelper.kt | 27 ++++---- 3 files changed, 53 insertions(+), 41 deletions(-) diff --git a/src/main/kotlin/ru/vyatsu/qr_access_api/repository/QrRepository.kt b/src/main/kotlin/ru/vyatsu/qr_access_api/repository/QrRepository.kt index a18cbf1..5bf87ed 100644 --- a/src/main/kotlin/ru/vyatsu/qr_access_api/repository/QrRepository.kt +++ b/src/main/kotlin/ru/vyatsu/qr_access_api/repository/QrRepository.kt @@ -9,8 +9,8 @@ import java.util.* private const val GET_ACTUAL_QRS_BY_UNIT_ID = """ SELECT q.start_date_time, q.end_date_time, q.door_id, q.key_code FROM qrs q JOIN doors d ON (d.id = q.door_id) - JOIN oauth2_authorized_client c ON (c.client_registration_id = d.unit_id AND c.principal_name = d.principal_name) - WHERE c.client_registration_id = ? AND q.start_date_time <= CURRENT_TIMESTAMP AND q.end_date_time >= CURRENT_TIMESTAMP + JOIN oauth2_authorized_client c ON (c.id = d.unit_id) + WHERE c.id = ? AND q.start_date_time <= CURRENT_TIMESTAMP AND q.end_date_time >= CURRENT_TIMESTAMP """ @Repository diff --git a/src/main/resources/db/changelog/1.0.0/changelog.yml b/src/main/resources/db/changelog/1.0.0/changelog.yml index 29e039f..35490be 100644 --- a/src/main/resources/db/changelog/1.0.0/changelog.yml +++ b/src/main/resources/db/changelog/1.0.0/changelog.yml @@ -11,56 +11,68 @@ databaseChangeLog: nullable: false primaryKey: true primaryKeyName: PK_oauth2_client - name: client_registration_id + name: id type: VARCHAR(100) - column: constraints: nullable: false - primaryKey: true - primaryKeyName: PK_oauth2_client - name: principal_name - type: VARCHAR(200) - - column: - name: access_token_type + name: client_id type: VARCHAR(100) + - column: + name: client_id_issued_at + type: TIMESTAMP constraints: nullable: false - column: - name: access_token_value + name: client_secret type: TEXT constraints: nullable: false - column: - name: access_token_issued_at + name: client_secret_expires_at type: TIMESTAMP constraints: nullable: false - column: - name: access_token_expires_at - type: TIMESTAMP + name: client_name + type: TEXT constraints: nullable: false - column: - name: access_token_scopes + name: client_authentication_methods + type: TEXT + constraints: + nullable: false + - column: + name: authorization_grant_types type: TEXT constraints: nullable: true - column: - name: refresh_token_value + name: redirect_uris type: TEXT constraints: nullable: true - column: - name: refresh_token_issued_at - type: TIMESTAMP - constraints: - nullable: true - - column: - name: created_at - type: TIMESTAMP + name: post_logout_redirect_uris + type: TEXT + constraints: + nullable: false + - column: + name: scopes + type: TEXT + constraints: + nullable: false + - column: + name: client_settings + type: JSONB + constraints: + nullable: false + - column: + name: token_settings + type: JSONB constraints: nullable: false - defaultValueComputed: CURRENT_TIMESTAMP - createTable: tableName: doors columns: @@ -76,11 +88,6 @@ databaseChangeLog: nullable: false name: unit_id type: VARCHAR(100) - - column: - constraints: - nullable: false - name: principal_name - type: VARCHAR(200) - createTable: tableName: qrs columns: @@ -109,8 +116,8 @@ databaseChangeLog: name: end_date_time type: TIMESTAMP WITH TIME ZONE - addForeignKeyConstraint: - baseColumnNames: unit_id, principal_name + baseColumnNames: unit_id baseTableName: doors constraintName: FK_unit_door - referencedColumnNames: client_registration_id, principal_name + referencedColumnNames: id referencedTableName: oauth2_authorized_client \ No newline at end of file diff --git a/src/test/kotlin/ru/vyatsu/qr_access_api/database/utils/InsertDatabaseHelper.kt b/src/test/kotlin/ru/vyatsu/qr_access_api/database/utils/InsertDatabaseHelper.kt index 3ff1483..8961fae 100644 --- a/src/test/kotlin/ru/vyatsu/qr_access_api/database/utils/InsertDatabaseHelper.kt +++ b/src/test/kotlin/ru/vyatsu/qr_access_api/database/utils/InsertDatabaseHelper.kt @@ -5,8 +5,8 @@ import java.time.LocalDateTime import java.time.OffsetDateTime private const val INSERT_CLIENT_QUERY = - """INSERT INTO oauth2_authorized_client(client_registration_id, principal_name, access_token_type, access_token_value, access_token_issued_at, access_token_expires_at, created_at) - VALUES (?, ?, ?, ?, ?, ?, ?)""" + """INSERT INTO oauth2_authorized_client(id, client_id, client_id_issued_at, client_secret, client_secret_expires_at, client_name, client_authentication_methods, authorization_grant_types, redirect_uris, post_logout_redirect_uris, scopes, client_settings, token_settings) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, to_json(?::json), to_json(?::json))""" class InsertDatabaseHelper(private val template: JdbcTemplate) { fun insertQr( @@ -23,23 +23,28 @@ class InsertDatabaseHelper(private val template: JdbcTemplate) { } } - fun insertDoor(id: String, unitId: String, principalName: String = ""): Int { - return template.update("INSERT INTO doors(id, unit_id, principal_name) VALUES (?, ?, ?)") { ps -> + fun insertDoor(id: String, unitId: String): Int { + return template.update("INSERT INTO doors(id, unit_id) VALUES (?, ?)") { ps -> ps.setString(1, id) ps.setString(2, unitId) - ps.setString(3, principalName) } } - fun insertClient(id: String, principalName: String = ""): Int { + fun insertClient(id: String): Int { return template.update(INSERT_CLIENT_QUERY) { ps -> ps.setString(1, id) - ps.setString(2, principalName) - ps.setString(3, "Bearer") - ps.setString(4, "Tokenasfgerseawvg") + ps.setString(2, id) + ps.setObject(3, LocalDateTime.now()) + ps.setString(4, "secret") ps.setObject(5, LocalDateTime.now()) - ps.setObject(6, LocalDateTime.now()) - ps.setObject(7, LocalDateTime.now()) + ps.setString(6, id) + ps.setString(7, "client_secret_post") + ps.setString(8, "client_credentials") + ps.setString(9, "http://localhost:8080") + ps.setString(10, "http://localhost:8080") + ps.setString(11, "") + ps.setString(12, "{}") + ps.setString(13, "{}") } } } \ No newline at end of file