Small style fixes
This commit is contained in:
@@ -18,7 +18,7 @@ var robotEmbed embed.FS
|
|||||||
// A simple backend that handles user authentication, user count, and crash reports.
|
// A simple backend that handles user authentication, user count, and crash reports.
|
||||||
type Backend struct {
|
type Backend struct {
|
||||||
userTable Table[User]
|
userTable Table[User]
|
||||||
keyTable Table[ApiKey]
|
keyTable Table[APIKey]
|
||||||
m *http.ServeMux
|
m *http.ServeMux
|
||||||
apps map[string]App
|
apps map[string]App
|
||||||
managementKeyID string
|
managementKeyID string
|
||||||
@@ -29,7 +29,7 @@ type Backend struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create a new Backend with the given apps. keyTable must be specified.
|
// Create a new Backend with the given apps. keyTable must be specified.
|
||||||
func NewBackend(keyTable Table[ApiKey], apps ...App) (*Backend, error) {
|
func NewBackend(keyTable Table[APIKey], apps ...App) (*Backend, error) {
|
||||||
b := &Backend{
|
b := &Backend{
|
||||||
keyTable: keyTable,
|
keyTable: keyTable,
|
||||||
m: &http.ServeMux{},
|
m: &http.ServeMux{},
|
||||||
@@ -143,7 +143,7 @@ func (b *Backend) HandleFunc(pattern string, h http.HandlerFunc) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Try to get the App associated with the given ApiKey. Returns nil if not found.
|
// Try to get the App associated with the given ApiKey. Returns nil if not found.
|
||||||
func (b *Backend) GetApp(a *ApiKey) App {
|
func (b *Backend) GetApp(a *APIKey) App {
|
||||||
return b.apps[a.AppID]
|
return b.apps[a.AppID]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,11 +10,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrApiKeyUnauthorized = errors.New("api key present but invalid")
|
ErrAPIKeyUnauthorized = errors.New("api key present but invalid")
|
||||||
ErrTokenUnauthorized = errors.New("token present but invalid")
|
ErrTokenUnauthorized = errors.New("token present but invalid")
|
||||||
)
|
)
|
||||||
|
|
||||||
type ApiKey struct {
|
type APIKey struct {
|
||||||
Perm map[string]bool `json:"perm" bson:"perm"`
|
Perm map[string]bool `json:"perm" bson:"perm"`
|
||||||
ID string `json:"id" bson:"_id" valkey:",key"`
|
ID string `json:"id" bson:"_id" valkey:",key"`
|
||||||
AppID string `json:"appID" bson:"appID"`
|
AppID string `json:"appID" bson:"appID"`
|
||||||
@@ -22,13 +22,13 @@ type ApiKey struct {
|
|||||||
AllowedOrigins []string `json:"allowedOrigins" bson:"allowedOrigins"`
|
AllowedOrigins []string `json:"allowedOrigins" bson:"allowedOrigins"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k ApiKey) GetID() string {
|
func (k APIKey) GetID() string {
|
||||||
return k.ID
|
return k.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
type ParsedHeader struct {
|
type ParsedHeader struct {
|
||||||
User *ReqestUser
|
User *ReqestUser
|
||||||
Key *ApiKey
|
Key *APIKey
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parses the X-API-Key and Authorization headers. If the API Key provided but invalid (either due to expiring or isn't found), ErrApiKeyUnauthorized is returned.
|
// Parses the X-API-Key and Authorization headers. If the API Key provided but invalid (either due to expiring or isn't found), ErrApiKeyUnauthorized is returned.
|
||||||
@@ -41,24 +41,24 @@ func (b *Backend) ParseHeader(r *http.Request) (*ParsedHeader, error) {
|
|||||||
if key != "" {
|
if key != "" {
|
||||||
apiKey, err := b.keyTable.Get(r.Context(), key)
|
apiKey, err := b.keyTable.Get(r.Context(), key)
|
||||||
if err == ErrNotFound {
|
if err == ErrNotFound {
|
||||||
return nil, ErrApiKeyUnauthorized
|
return nil, ErrAPIKeyUnauthorized
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if apiKey.Death > 0 && time.Unix(apiKey.Death, 0).Before(time.Now()) {
|
if apiKey.Death > 0 && time.Unix(apiKey.Death, 0).Before(time.Now()) {
|
||||||
return nil, ErrApiKeyUnauthorized
|
return nil, ErrAPIKeyUnauthorized
|
||||||
}
|
}
|
||||||
out.Key = &apiKey
|
out.Key = &apiKey
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("origin:", r.Header.Get("origin"))
|
fmt.Println("origin:", r.Header.Get("origin"))
|
||||||
keys, err := b.keyTable.Find(r.Context(), map[string]any{"allowedOrigins": r.Header.Get("origin")})
|
keys, err := b.keyTable.Find(r.Context(), map[string]any{"allowedOrigins": r.Header.Get("origin")})
|
||||||
if err == ErrNotFound {
|
if err == ErrNotFound {
|
||||||
return nil, ErrApiKeyUnauthorized
|
return nil, ErrAPIKeyUnauthorized
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if keys[0].Death > 0 && time.Unix(keys[0].Death, 0).Before(time.Now()) {
|
if keys[0].Death > 0 && time.Unix(keys[0].Death, 0).Before(time.Now()) {
|
||||||
return nil, ErrApiKeyUnauthorized
|
return nil, ErrAPIKeyUnauthorized
|
||||||
}
|
}
|
||||||
out.Key = &keys[0]
|
out.Key = &keys[0]
|
||||||
}
|
}
|
||||||
@@ -87,7 +87,7 @@ func (b *Backend) ParseHeader(r *http.Request) (*ParsedHeader, error) {
|
|||||||
func (b *Backend) VerifyHeader(w http.ResponseWriter, r *http.Request, keyPerm string, allowManagementKey bool) (*ParsedHeader, error) {
|
func (b *Backend) VerifyHeader(w http.ResponseWriter, r *http.Request, keyPerm string, allowManagementKey bool) (*ParsedHeader, error) {
|
||||||
hdr, err := b.ParseHeader(r)
|
hdr, err := b.ParseHeader(r)
|
||||||
if hdr == nil || hdr.Key == nil {
|
if hdr == nil || hdr.Key == nil {
|
||||||
if err == ErrApiKeyUnauthorized {
|
if err == ErrAPIKeyUnauthorized {
|
||||||
ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized")
|
ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized")
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ func setupMongo(uri string) {
|
|||||||
func setupBackend(mux *http.ServeMux) {
|
func setupBackend(mux *http.ServeMux) {
|
||||||
blogApp = blog.NewBlogApp(mongoClient.Database("blog"))
|
blogApp = blog.NewBlogApp(mongoClient.Database("blog"))
|
||||||
var err error
|
var err error
|
||||||
back, err = backend.NewBackend(db.NewMongoTable[backend.ApiKey](
|
back, err = backend.NewBackend(db.NewMongoTable[backend.APIKey](
|
||||||
mongoClient.Database("darkstorm").Collection("keys")),
|
mongoClient.Database("darkstorm").Collection("keys")),
|
||||||
blogApp,
|
blogApp,
|
||||||
swassistant.NewSWBackend(mongoClient.Database("swassistant")),
|
swassistant.NewSWBackend(mongoClient.Database("swassistant")),
|
||||||
|
|||||||
Reference in New Issue
Block a user