Skip to content

Commit 4e52840

Browse files
committed
Removed system key functions and replace with tokens. Fixed doc generation. TODO: test e2e
1 parent 8e48389 commit 4e52840

File tree

15 files changed

+206
-147
lines changed

15 files changed

+206
-147
lines changed

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")

docs/setup.rst

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -290,20 +290,6 @@ This option disables Gokapis internal authentication completely, except for API
290290
- ``/uploadChunk``
291291
- ``/uploadStatus``
292292
- ``/users``
293-
- ``/auth/token``
294-
- ``/changePassword``
295-
- ``/e2eSetup``
296-
- ``/logs``
297-
- ``/uploadChunk``
298-
- ``/uploadStatus``
299-
- ``/users``
300-
- ``/auth/token``
301-
- ``/changePassword``
302-
- ``/e2eSetup``
303-
- ``/logs``
304-
- ``/uploadChunk``
305-
- ``/uploadStatus``
306-
- ``/users``
307293

308294
.. warning::
309295
This option has potential to be *very* dangerous, only proceed if you know what you are doing!

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/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/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/apikeys.go

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package sqlite
33
import (
44
"database/sql"
55
"errors"
6+
"time"
7+
68
"github.com/forceu/gokapi/internal/helper"
79
"github.com/forceu/gokapi/internal/models"
8-
"time"
910
)
1011

1112
type schemaApiKeys struct {
@@ -76,32 +77,6 @@ func (p DatabaseProvider) GetApiKey(id string) (models.ApiKey, bool) {
7677
return result, true
7778
}
7879

79-
// GetSystemKey returns the latest UI API key
80-
func (p DatabaseProvider) GetSystemKey(userId int) (models.ApiKey, bool) {
81-
var rowResult schemaApiKeys
82-
row := p.sqliteDb.QueryRow("SELECT * FROM ApiKeys WHERE IsSystemKey = 1 AND UserId = ? ORDER BY Expiry DESC LIMIT 1", userId)
83-
err := row.Scan(&rowResult.Id, &rowResult.FriendlyName, &rowResult.LastUsed, &rowResult.Permissions, &rowResult.Expiry, &rowResult.IsSystemKey, &rowResult.UserId, &rowResult.PublicId)
84-
if err != nil {
85-
if errors.Is(err, sql.ErrNoRows) {
86-
return models.ApiKey{}, false
87-
}
88-
helper.Check(err)
89-
return models.ApiKey{}, false
90-
}
91-
92-
result := models.ApiKey{
93-
Id: rowResult.Id,
94-
PublicId: rowResult.PublicId,
95-
FriendlyName: rowResult.FriendlyName,
96-
LastUsed: rowResult.LastUsed,
97-
Permissions: models.ApiPermission(rowResult.Permissions),
98-
Expiry: rowResult.Expiry,
99-
IsSystemKey: rowResult.IsSystemKey == 1,
100-
UserId: rowResult.UserId,
101-
}
102-
return result, true
103-
}
104-
10580
// GetApiKeyByPublicKey returns an API key by using the public key
10681
func (p DatabaseProvider) GetApiKeyByPublicKey(publicKey string) (string, bool) {
10782
var rowResult schemaApiKeys

internal/webserver/Webserver.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,6 @@ func showE2ESetup(w http.ResponseWriter, r *http.Request) {
653653
err = templateFolder.ExecuteTemplate(w, "e2esetup", e2ESetupView{
654654
HasBeenSetup: e2einfo.HasBeenSetUp(),
655655
PublicName: configuration.Get().PublicName,
656-
SystemKey: api.GetSystemKey(user.Id),
657656
CustomContent: customStaticInfo})
658657
helper.CheckIgnoreTimeout(err)
659658
}
@@ -681,7 +680,6 @@ type e2ESetupView struct {
681680
IsDownloadView bool
682681
HasBeenSetup bool
683682
PublicName string
684-
SystemKey string
685683
CustomContent customStatic
686684
}
687685

@@ -695,7 +693,6 @@ type AdminView struct {
695693
ServerUrl string
696694
Logs string
697695
PublicName string
698-
SystemKey string
699696
IsAdminView bool
700697
IsDownloadView bool
701698
IsApiView bool
@@ -807,7 +804,6 @@ func (u *AdminView) convertGlobalConfig(view int, user models.User) *AdminView {
807804
u.MinLengthPassword = config.MinLengthPassword
808805
u.ChunkSize = config.ChunkSize
809806
u.IncludeFilename = config.IncludeFilename
810-
u.SystemKey = api.GetSystemKey(user.Id)
811807
return u
812808
}
813809

internal/webserver/api/Api.go

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -124,49 +124,6 @@ func generateNewKey(defaultPermissions bool, userId int, friendlyName string) mo
124124
return newKey
125125
}
126126

127-
// newSystemKey generates a new API key that is only used internally for the GUI
128-
// and will be valid for 48 hours
129-
func newSystemKey(userId int) string {
130-
user, ok := database.GetUser(userId)
131-
if !ok {
132-
panic("user not found")
133-
}
134-
tempKey := models.ApiKey{
135-
Permissions: models.ApiPermAll,
136-
}
137-
if !user.HasPermissionReplace() {
138-
tempKey.RemovePermission(models.ApiPermReplace)
139-
}
140-
if !user.HasPermissionManageUsers() {
141-
tempKey.RemovePermission(models.ApiPermManageUsers)
142-
}
143-
if !user.HasPermissionManageLogs() {
144-
tempKey.RemovePermission(models.ApiPermManageLogs)
145-
}
146-
147-
newKey := models.ApiKey{
148-
Id: helper.GenerateRandomString(LengthApiKey),
149-
PublicId: helper.GenerateRandomString(LengthPublicId),
150-
FriendlyName: "Internal System Key",
151-
Permissions: tempKey.Permissions,
152-
Expiry: time.Now().Add(time.Hour * 48).Unix(),
153-
IsSystemKey: true,
154-
UserId: userId,
155-
}
156-
database.SaveApiKey(newKey)
157-
return newKey.Id
158-
}
159-
160-
// GetSystemKey returns the latest System API key or generates a new one, if none exists or the current one expires
161-
// within the next 24 hours
162-
func GetSystemKey(userId int) string {
163-
key, ok := database.GetSystemKey(userId)
164-
if !ok || key.Expiry < time.Now().Add(time.Hour*24).Unix() {
165-
return newSystemKey(userId)
166-
}
167-
return key.Id
168-
}
169-
170127
func apiDeleteKey(w http.ResponseWriter, r requestParser, user models.User) {
171128
request, ok := r.(*paramAuthDelete)
172129
if !ok {

0 commit comments

Comments
 (0)