Change schema to fit auth server

This commit is contained in:
kashiuno 2025-01-07 11:42:30 +03:00
parent 1a0319e0b4
commit c6fddca8ea
3 changed files with 53 additions and 41 deletions

View File

@ -9,8 +9,8 @@ import java.util.*
private const val GET_ACTUAL_QRS_BY_UNIT_ID = """ 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 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 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) JOIN oauth2_authorized_client c ON (c.id = d.unit_id)
WHERE c.client_registration_id = ? AND q.start_date_time <= CURRENT_TIMESTAMP AND q.end_date_time >= CURRENT_TIMESTAMP WHERE c.id = ? AND q.start_date_time <= CURRENT_TIMESTAMP AND q.end_date_time >= CURRENT_TIMESTAMP
""" """
@Repository @Repository

View File

@ -11,56 +11,68 @@ databaseChangeLog:
nullable: false nullable: false
primaryKey: true primaryKey: true
primaryKeyName: PK_oauth2_client primaryKeyName: PK_oauth2_client
name: client_registration_id name: id
type: VARCHAR(100) type: VARCHAR(100)
- column: - column:
constraints: constraints:
nullable: false nullable: false
primaryKey: true name: client_id
primaryKeyName: PK_oauth2_client
name: principal_name
type: VARCHAR(200)
- column:
name: access_token_type
type: VARCHAR(100) type: VARCHAR(100)
- column:
name: client_id_issued_at
type: TIMESTAMP
constraints: constraints:
nullable: false nullable: false
- column: - column:
name: access_token_value name: client_secret
type: TEXT type: TEXT
constraints: constraints:
nullable: false nullable: false
- column: - column:
name: access_token_issued_at name: client_secret_expires_at
type: TIMESTAMP type: TIMESTAMP
constraints: constraints:
nullable: false nullable: false
- column: - column:
name: access_token_expires_at name: client_name
type: TIMESTAMP type: TEXT
constraints: constraints:
nullable: false nullable: false
- column: - column:
name: access_token_scopes name: client_authentication_methods
type: TEXT
constraints:
nullable: false
- column:
name: authorization_grant_types
type: TEXT type: TEXT
constraints: constraints:
nullable: true nullable: true
- column: - column:
name: refresh_token_value name: redirect_uris
type: TEXT type: TEXT
constraints: constraints:
nullable: true nullable: true
- column: - column:
name: refresh_token_issued_at name: post_logout_redirect_uris
type: TIMESTAMP type: TEXT
constraints: constraints:
nullable: true nullable: false
- column: - column:
name: created_at name: scopes
type: TIMESTAMP type: TEXT
constraints:
nullable: false
- column:
name: client_settings
type: JSONB
constraints:
nullable: false
- column:
name: token_settings
type: JSONB
constraints: constraints:
nullable: false nullable: false
defaultValueComputed: CURRENT_TIMESTAMP
- createTable: - createTable:
tableName: doors tableName: doors
columns: columns:
@ -76,11 +88,6 @@ databaseChangeLog:
nullable: false nullable: false
name: unit_id name: unit_id
type: VARCHAR(100) type: VARCHAR(100)
- column:
constraints:
nullable: false
name: principal_name
type: VARCHAR(200)
- createTable: - createTable:
tableName: qrs tableName: qrs
columns: columns:
@ -109,8 +116,8 @@ databaseChangeLog:
name: end_date_time name: end_date_time
type: TIMESTAMP WITH TIME ZONE type: TIMESTAMP WITH TIME ZONE
- addForeignKeyConstraint: - addForeignKeyConstraint:
baseColumnNames: unit_id, principal_name baseColumnNames: unit_id
baseTableName: doors baseTableName: doors
constraintName: FK_unit_door constraintName: FK_unit_door
referencedColumnNames: client_registration_id, principal_name referencedColumnNames: id
referencedTableName: oauth2_authorized_client referencedTableName: oauth2_authorized_client

View File

@ -5,8 +5,8 @@ import java.time.LocalDateTime
import java.time.OffsetDateTime import java.time.OffsetDateTime
private const val INSERT_CLIENT_QUERY = 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) """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 (?, ?, ?, ?, ?, ?, ?)""" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, to_json(?::json), to_json(?::json))"""
class InsertDatabaseHelper(private val template: JdbcTemplate) { class InsertDatabaseHelper(private val template: JdbcTemplate) {
fun insertQr( fun insertQr(
@ -23,23 +23,28 @@ class InsertDatabaseHelper(private val template: JdbcTemplate) {
} }
} }
fun insertDoor(id: String, unitId: String, principalName: String = ""): Int { fun insertDoor(id: String, unitId: String): Int {
return template.update("INSERT INTO doors(id, unit_id, principal_name) VALUES (?, ?, ?)") { ps -> return template.update("INSERT INTO doors(id, unit_id) VALUES (?, ?)") { ps ->
ps.setString(1, id) ps.setString(1, id)
ps.setString(2, unitId) 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 -> return template.update(INSERT_CLIENT_QUERY) { ps ->
ps.setString(1, id) ps.setString(1, id)
ps.setString(2, principalName) ps.setString(2, id)
ps.setString(3, "Bearer") ps.setObject(3, LocalDateTime.now())
ps.setString(4, "Tokenasfgerseawvg") ps.setString(4, "secret")
ps.setObject(5, LocalDateTime.now()) ps.setObject(5, LocalDateTime.now())
ps.setObject(6, LocalDateTime.now()) ps.setString(6, id)
ps.setObject(7, LocalDateTime.now()) 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, "{}")
} }
} }
} }