Skip to content

Commit 75745ae

Browse files
authored
Use short-lived tokens instead of user API keys (#340)
1 parent 14b2f7b commit 75745ae

File tree

29 files changed

+481
-420
lines changed

29 files changed

+481
-420
lines changed

build/go-generate/minifyStaticContent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,6 @@ func fileExists(filename string) bool {
137137
// Auto-generated content below, do not modify
138138
// Version codes can be changed in updateVersionNumbers.go
139139

140-
const jsAdminVersion = 13
140+
const jsAdminVersion = 14
141141
const jsE2EVersion = 8
142142
const cssMainVersion = 5

build/go-generate/updateProtectedUrls.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func writeDocumentationFile(urls []string) {
9797
for _, url := range urls {
9898
output = output + "- ``" + url + "``\n"
9999
}
100-
regex := regexp.MustCompile(`proxy:(?:\r?\n)+((?:- ` + "``" + `\/\w+` + "``" + `\r?\n)+)`)
100+
regex := regexp.MustCompile("proxy:(?:\\r?\\n)+(?:- ``\\/[^`]+``\\r?\\n)+")
101101
matches := regex.FindAllIndex(documentationContent, -1)
102102
if len(matches) != 1 {
103103
fmt.Println("ERROR: Not one match found exactly for documentation")

build/go-generate/updateVersionNumbers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"strings"
1212
)
1313

14-
const versionJsAdmin = 13
14+
const versionJsAdmin = 14
1515
const versionJsDropzone = 5
1616
const versionJsE2EAdmin = 8
1717
const versionCssMain = 5

docs/setup.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ This option disables Gokapis internal authentication completely, except for API
283283

284284
- ``/admin``
285285
- ``/apiKeys``
286+
- ``/auth/token``
286287
- ``/changePassword``
287288
- ``/e2eSetup``
288289
- ``/logs``

internal/configuration/database/Database.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,6 @@ func DeleteApiKey(id string) {
139139
db.DeleteApiKey(id)
140140
}
141141

142-
// GetSystemKey returns the latest UI API key
143-
func GetSystemKey(userId int) (models.ApiKey, bool) {
144-
return db.GetSystemKey(userId)
145-
}
146-
147142
// GetApiKeyByPublicKey returns an API key by using the public key
148143
func GetApiKeyByPublicKey(publicKey string) (string, bool) {
149144
return db.GetApiKeyByPublicKey(publicKey)

internal/configuration/database/Database_test.go

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package database
22

33
import (
4+
"log"
5+
"os"
6+
"testing"
7+
"time"
8+
49
"github.com/alicebob/miniredis/v2"
510
"github.com/forceu/gokapi/internal/configuration/database/dbabstraction"
611
"github.com/forceu/gokapi/internal/configuration/database/dbcache"
712
"github.com/forceu/gokapi/internal/models"
813
"github.com/forceu/gokapi/internal/test"
9-
"log"
10-
"os"
11-
"testing"
12-
"time"
1314
)
1415

1516
var configSqlite = models.DbConnection{
@@ -92,32 +93,6 @@ func TestApiKeys(t *testing.T) {
9293
runAllTypesCompareTwoOutputs(t, func() (any, any) {
9394
return GetApiKeyByPublicKey("publicId")
9495
}, "publicTest", true)
95-
96-
runAllTypesCompareOutput(t, func() any {
97-
_, ok := GetSystemKey(6)
98-
return ok
99-
}, false)
100-
101-
runAllTypesNoOutput(t, func() {
102-
SaveApiKey(models.ApiKey{
103-
Id: "sysKey1",
104-
PublicId: "sysKey1",
105-
IsSystemKey: true,
106-
Expiry: time.Now().Add(1 * time.Hour).Unix(),
107-
UserId: 6,
108-
})
109-
SaveApiKey(models.ApiKey{
110-
Id: "sysKey2",
111-
PublicId: "sysKey2",
112-
IsSystemKey: true,
113-
Expiry: time.Now().Add(2 * time.Hour).Unix(),
114-
UserId: 6,
115-
})
116-
})
117-
runAllTypesCompareTwoOutputs(t, func() (any, any) {
118-
key, ok := GetSystemKey(6)
119-
return key.Id, ok
120-
}, "sysKey2", true)
12196
}
12297

12398
func TestE2E(t *testing.T) {

internal/configuration/database/dbabstraction/DbAbstraction.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dbabstraction
22

33
import (
44
"fmt"
5+
56
"github.com/forceu/gokapi/internal/configuration/database/provider/redis"
67
"github.com/forceu/gokapi/internal/configuration/database/provider/sqlite"
78
"github.com/forceu/gokapi/internal/models"
@@ -43,8 +44,6 @@ type Database interface {
4344
UpdateTimeApiKey(apikey models.ApiKey)
4445
// DeleteApiKey deletes an API key with the given ID
4546
DeleteApiKey(id string)
46-
// GetSystemKey returns the latest UI API key
47-
GetSystemKey(userId int) (models.ApiKey, bool)
4847
// GetApiKeyByPublicKey returns an API key by using the public key
4948
GetApiKeyByPublicKey(publicKey string) (string, bool)
5049

internal/configuration/database/provider/redis/Redis_test.go

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package redis
22

33
import (
4-
"github.com/alicebob/miniredis/v2"
5-
"github.com/forceu/gokapi/internal/models"
6-
"github.com/forceu/gokapi/internal/test"
7-
redigo "github.com/gomodule/redigo/redis"
84
"log"
95
"os"
106
"slices"
117
"testing"
128
"time"
9+
10+
"github.com/alicebob/miniredis/v2"
11+
"github.com/forceu/gokapi/internal/models"
12+
"github.com/forceu/gokapi/internal/test"
13+
redigo "github.com/gomodule/redigo/redis"
1314
)
1415

1516
var config = models.DbConnection{
@@ -253,47 +254,6 @@ func TestApiKeys(t *testing.T) {
253254
keyName, ok := dbInstance.GetApiKeyByPublicKey("publicId")
254255
test.IsEqualBool(t, ok, true)
255256
test.IsEqualString(t, keyName, "publicTest")
256-
257-
_, ok = dbInstance.GetSystemKey(4)
258-
test.IsEqualBool(t, ok, false)
259-
dbInstance.SaveApiKey(models.ApiKey{
260-
Id: "sysKey1",
261-
PublicId: "publicSysKey1",
262-
IsSystemKey: true,
263-
UserId: 5,
264-
Expiry: time.Now().Add(time.Hour).Unix(),
265-
})
266-
_, ok = dbInstance.GetSystemKey(4)
267-
test.IsEqualBool(t, ok, false)
268-
dbInstance.SaveApiKey(models.ApiKey{
269-
Id: "sysKey2",
270-
PublicId: "publicSysKey2",
271-
IsSystemKey: true,
272-
UserId: 4,
273-
Expiry: time.Now().Add(-1 * time.Hour).Unix(),
274-
})
275-
_, ok = dbInstance.GetSystemKey(4)
276-
test.IsEqualBool(t, ok, false)
277-
_, ok = dbInstance.GetSystemKey(5)
278-
test.IsEqualBool(t, ok, true)
279-
dbInstance.SaveApiKey(models.ApiKey{
280-
Id: "sysKey3",
281-
PublicId: "publicSysKey2",
282-
IsSystemKey: true,
283-
UserId: 4,
284-
Expiry: time.Now().Add(2 * time.Hour).Unix(),
285-
})
286-
dbInstance.SaveApiKey(models.ApiKey{
287-
Id: "sysKey4",
288-
PublicId: "publicSysKey4",
289-
IsSystemKey: true,
290-
UserId: 4,
291-
Expiry: time.Now().Add(4 * time.Hour).Unix(),
292-
})
293-
key, ok = dbInstance.GetSystemKey(4)
294-
test.IsEqualBool(t, ok, true)
295-
test.IsEqualString(t, key.Id, "sysKey4")
296-
test.IsEqualBool(t, key.IsSystemKey, true)
297257
}
298258

299259
func TestDatabaseProvider_IncreaseDownloadCount(t *testing.T) {

internal/configuration/database/provider/redis/apikeys.go

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package redis
22

33
import (
4+
"strings"
5+
46
"github.com/forceu/gokapi/internal/helper"
57
"github.com/forceu/gokapi/internal/models"
68
redigo "github.com/gomodule/redigo/redis"
7-
"strings"
89
)
910

1011
const (
@@ -41,29 +42,6 @@ func (p DatabaseProvider) GetApiKey(id string) (models.ApiKey, bool) {
4142
return apikey, true
4243
}
4344

44-
// GetSystemKey returns the latest UI API key
45-
func (p DatabaseProvider) GetSystemKey(userId int) (models.ApiKey, bool) {
46-
keys := p.GetAllApiKeys()
47-
foundKey := ""
48-
var latestExpiry int64
49-
for _, key := range keys {
50-
if !key.IsSystemKey {
51-
continue
52-
}
53-
if key.UserId != userId {
54-
continue
55-
}
56-
if key.Expiry > latestExpiry {
57-
foundKey = key.Id
58-
latestExpiry = key.Expiry
59-
}
60-
}
61-
if foundKey == "" {
62-
return models.ApiKey{}, false
63-
}
64-
return keys[foundKey], true
65-
}
66-
6745
// GetApiKeyByPublicKey returns an API key by using the public key
6846
func (p DatabaseProvider) GetApiKeyByPublicKey(publicKey string) (string, bool) {
6947
keys := p.GetAllApiKeys()

internal/configuration/database/provider/sqlite/Sqlite_test.go

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
package sqlite
44

55
import (
6-
"github.com/forceu/gokapi/internal/helper"
7-
"github.com/forceu/gokapi/internal/models"
8-
"github.com/forceu/gokapi/internal/test"
96
"math"
107
"os"
118
"slices"
129
"sync"
1310
"testing"
1411
"time"
12+
13+
"github.com/forceu/gokapi/internal/helper"
14+
"github.com/forceu/gokapi/internal/models"
15+
"github.com/forceu/gokapi/internal/test"
1516
)
1617

1718
var config = models.DbConnection{
@@ -451,47 +452,6 @@ func TestUpdateTimeApiKey(t *testing.T) {
451452
keyName, ok := dbInstance.GetApiKeyByPublicKey("publicId")
452453
test.IsEqualBool(t, ok, true)
453454
test.IsEqualString(t, keyName, "publicTest")
454-
455-
_, ok = dbInstance.GetSystemKey(4)
456-
test.IsEqualBool(t, ok, false)
457-
dbInstance.SaveApiKey(models.ApiKey{
458-
Id: "sysKey1",
459-
PublicId: "publicSysKey1",
460-
IsSystemKey: true,
461-
UserId: 5,
462-
Expiry: time.Now().Add(time.Hour).Unix(),
463-
})
464-
_, ok = dbInstance.GetSystemKey(4)
465-
test.IsEqualBool(t, ok, false)
466-
dbInstance.SaveApiKey(models.ApiKey{
467-
Id: "sysKey2",
468-
PublicId: "publicSysKey2",
469-
IsSystemKey: true,
470-
UserId: 4,
471-
Expiry: time.Now().Add(-1 * time.Hour).Unix(),
472-
})
473-
_, ok = dbInstance.GetSystemKey(4)
474-
test.IsEqualBool(t, ok, true)
475-
_, ok = dbInstance.GetSystemKey(5)
476-
test.IsEqualBool(t, ok, true)
477-
dbInstance.SaveApiKey(models.ApiKey{
478-
Id: "sysKey3",
479-
PublicId: "publicSysKey2",
480-
IsSystemKey: true,
481-
UserId: 4,
482-
Expiry: time.Now().Add(2 * time.Hour).Unix(),
483-
})
484-
dbInstance.SaveApiKey(models.ApiKey{
485-
Id: "sysKey4",
486-
PublicId: "publicSysKey4",
487-
IsSystemKey: true,
488-
UserId: 4,
489-
Expiry: time.Now().Add(4 * time.Hour).Unix(),
490-
})
491-
key, ok = dbInstance.GetSystemKey(4)
492-
test.IsEqualBool(t, ok, true)
493-
test.IsEqualString(t, key.Id, "sysKey4")
494-
test.IsEqualBool(t, key.IsSystemKey, true)
495455
}
496456

497457
func TestParallelConnectionsWritingAndReading(t *testing.T) {

0 commit comments

Comments
 (0)