@@ -16,6 +16,7 @@ type schemaApiKeys struct {
16
16
Expiry int64
17
17
IsSystemKey int
18
18
UserId int
19
+ PublicId string
19
20
}
20
21
21
22
// currentTime is used in order to modify the current time for testing purposes in unit tests
@@ -32,10 +33,11 @@ func (p DatabaseProvider) GetAllApiKeys() map[string]models.ApiKey {
32
33
defer rows .Close ()
33
34
for rows .Next () {
34
35
rowData := schemaApiKeys {}
35
- err = rows .Scan (& rowData .Id , & rowData .FriendlyName , & rowData .LastUsed , & rowData .Permissions , & rowData .Expiry , & rowData .IsSystemKey , & rowData .UserId )
36
+ err = rows .Scan (& rowData .Id , & rowData .FriendlyName , & rowData .LastUsed , & rowData .Permissions , & rowData .Expiry , & rowData .IsSystemKey , & rowData .UserId , & rowData . PublicId )
36
37
helper .Check (err )
37
38
result [rowData .Id ] = models.ApiKey {
38
39
Id : rowData .Id ,
40
+ PublicId : rowData .PublicId ,
39
41
FriendlyName : rowData .FriendlyName ,
40
42
LastUsed : rowData .LastUsed ,
41
43
Permissions : uint8 (rowData .Permissions ),
@@ -51,7 +53,7 @@ func (p DatabaseProvider) GetAllApiKeys() map[string]models.ApiKey {
51
53
func (p DatabaseProvider ) GetApiKey (id string ) (models.ApiKey , bool ) {
52
54
var rowResult schemaApiKeys
53
55
row := p .sqliteDb .QueryRow ("SELECT * FROM ApiKeys WHERE Id = ?" , id )
54
- err := row .Scan (& rowResult .Id , & rowResult .FriendlyName , & rowResult .LastUsed , & rowResult .Permissions , & rowResult .Expiry , & rowResult .IsSystemKey , & rowResult .UserId )
56
+ err := row .Scan (& rowResult .Id , & rowResult .FriendlyName , & rowResult .LastUsed , & rowResult .Permissions , & rowResult .Expiry , & rowResult .IsSystemKey , & rowResult .UserId , & rowResult . PublicId )
55
57
if err != nil {
56
58
if errors .Is (err , sql .ErrNoRows ) {
57
59
return models.ApiKey {}, false
@@ -62,6 +64,7 @@ func (p DatabaseProvider) GetApiKey(id string) (models.ApiKey, bool) {
62
64
63
65
result := models.ApiKey {
64
66
Id : rowResult .Id ,
67
+ PublicId : rowResult .PublicId ,
65
68
FriendlyName : rowResult .FriendlyName ,
66
69
LastUsed : rowResult .LastUsed ,
67
70
Permissions : uint8 (rowResult .Permissions ),
@@ -77,7 +80,7 @@ func (p DatabaseProvider) GetApiKey(id string) (models.ApiKey, bool) {
77
80
func (p DatabaseProvider ) GetSystemKey (userId int ) (models.ApiKey , bool ) {
78
81
var rowResult schemaApiKeys
79
82
row := p .sqliteDb .QueryRow ("SELECT * FROM ApiKeys WHERE IsSystemKey = 1 AND UserId = ? ORDER BY Expiry DESC LIMIT 1" , userId )
80
- err := row .Scan (& rowResult .Id , & rowResult .FriendlyName , & rowResult .LastUsed , & rowResult .Permissions , & rowResult .Expiry , & rowResult .IsSystemKey , & rowResult .UserId )
83
+ err := row .Scan (& rowResult .Id , & rowResult .FriendlyName , & rowResult .LastUsed , & rowResult .Permissions , & rowResult .Expiry , & rowResult .IsSystemKey , & rowResult .UserId , & rowResult . PublicId )
81
84
if err != nil {
82
85
if errors .Is (err , sql .ErrNoRows ) {
83
86
return models.ApiKey {}, false
@@ -88,6 +91,7 @@ func (p DatabaseProvider) GetSystemKey(userId int) (models.ApiKey, bool) {
88
91
89
92
result := models.ApiKey {
90
93
Id : rowResult .Id ,
94
+ PublicId : rowResult .PublicId ,
91
95
FriendlyName : rowResult .FriendlyName ,
92
96
LastUsed : rowResult .LastUsed ,
93
97
Permissions : uint8 (rowResult .Permissions ),
@@ -98,14 +102,29 @@ func (p DatabaseProvider) GetSystemKey(userId int) (models.ApiKey, bool) {
98
102
return result , true
99
103
}
100
104
105
+ // GetApiKeyByPublicKey returns an API key by using the public key
106
+ func (p DatabaseProvider ) GetApiKeyByPublicKey (publicKey string ) (string , bool ) {
107
+ var rowResult schemaApiKeys
108
+ row := p .sqliteDb .QueryRow ("SELECT Id FROM ApiKeys WHERE PublicId = ? LIMIT 1" , publicKey )
109
+ err := row .Scan (& rowResult .Id )
110
+ if err != nil {
111
+ if errors .Is (err , sql .ErrNoRows ) {
112
+ return "" , false
113
+ }
114
+ helper .Check (err )
115
+ return "" , false
116
+ }
117
+ return rowResult .Id , true
118
+ }
119
+
101
120
// SaveApiKey saves the API key to the database
102
121
func (p DatabaseProvider ) SaveApiKey (apikey models.ApiKey ) {
103
122
isSystemKey := 0
104
123
if apikey .IsSystemKey {
105
124
isSystemKey = 1
106
125
}
107
- _ , err := p .sqliteDb .Exec ("INSERT OR REPLACE INTO ApiKeys (Id, FriendlyName, LastUsed, Permissions, Expiry, IsSystemKey, UserId) VALUES (?, ?, ?, ?, ?, ?, ?)" ,
108
- apikey .Id , apikey .FriendlyName , apikey .LastUsed , apikey .Permissions , apikey .Expiry , isSystemKey , apikey .UserId )
126
+ _ , err := p .sqliteDb .Exec ("INSERT OR REPLACE INTO ApiKeys (Id, FriendlyName, LastUsed, Permissions, Expiry, IsSystemKey, UserId, PublicId ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)" ,
127
+ apikey .Id , apikey .FriendlyName , apikey .LastUsed , apikey .Permissions , apikey .Expiry , isSystemKey , apikey .UserId , apikey . PublicId )
109
128
helper .Check (err )
110
129
}
111
130
0 commit comments