From 5af5de77197412dff587cb1845c591ca81c0207f Mon Sep 17 00:00:00 2001 From: Caleb Gardner Date: Fri, 28 Jun 2024 07:04:10 -0500 Subject: [PATCH] More work on stuff --- go.mod | 5 ++--- go.sum | 4 ++-- internal/backend/header.go | 13 ++++++++++++- internal/backend/key.go | 12 ------------ internal/blog/README.md | 1 - internal/blog/author.go | 1 + internal/blog/blog.go | 18 +++++++++++++----- internal/blog/main.go | 4 ++++ 8 files changed, 34 insertions(+), 24 deletions(-) delete mode 100644 internal/backend/key.go diff --git a/go.mod b/go.mod index 43bc30a..9a709ca 100644 --- a/go.mod +++ b/go.mod @@ -9,11 +9,10 @@ require ( golang.org/x/crypto v0.24.0 ) -require github.com/google/go-cmp v0.6.0 // indirect - require ( + github.com/CalebQ42/bbConvert v1.0.0 // indirect github.com/golang/snappy v0.0.1 // indirect - github.com/joho/godotenv v1.5.1 // indirect + github.com/google/go-cmp v0.6.0 // indirect github.com/klauspost/compress v1.13.6 // indirect github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect github.com/xdg-go/pbkdf2 v1.0.0 // indirect diff --git a/go.sum b/go.sum index d4401a0..e6b8230 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/CalebQ42/bbConvert v1.0.0 h1:2WSAxVKhCCMReuU30r3ehLtL6m9aH8sY3wugp9yUdzg= +github.com/CalebQ42/bbConvert v1.0.0/go.mod h1:QJevnlhzUdL3EJB5Lgqoi7rdgtzt/UEamn/QGNoVgkM= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= @@ -8,8 +10,6 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= -github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc= github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0= diff --git a/internal/backend/header.go b/internal/backend/header.go index 02868d6..d72ce93 100644 --- a/internal/backend/header.go +++ b/internal/backend/header.go @@ -14,13 +14,24 @@ var ( ErrTokenUnauthorized = errors.New("token present but invalid") ) +type ApiKey struct { + Perm map[string]bool `json:"perm" bson:"perm"` + ID string `json:"id" bson:"_id" valkey:",key"` + AppID string `json:"appID" bson:"appID"` + Death int64 `json:"death" bson:"death"` +} + +func (k ApiKey) GetID() string { + return k.ID +} + type ParsedHeader struct { User *ReqestUser 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. -// If the Authorization header is present but invalid, ErrTokenUnauthorized is part of the returned error (check with errors.Is). +// If the Authorization header is present but invalid, ErrTokenUnauthorized is returned. // NOTE: An invalid apiKey will cause a nil return, but a invalid token will not. Token parsing is only func (b *Backend) ParseHeader(r *http.Request) (*ParsedHeader, error) { out := &ParsedHeader{} diff --git a/internal/backend/key.go b/internal/backend/key.go deleted file mode 100644 index 5d24684..0000000 --- a/internal/backend/key.go +++ /dev/null @@ -1,12 +0,0 @@ -package backend - -type ApiKey struct { - Perm map[string]bool `json:"perm" bson:"perm"` - ID string `json:"id" bson:"_id" valkey:",key"` - AppID string `json:"appID" bson:"appID"` - Death int64 `json:"death" bson:"death"` -} - -func (k ApiKey) GetID() string { - return k.ID -} diff --git a/internal/blog/README.md b/internal/blog/README.md index 28e1825..16a7161 100644 --- a/internal/blog/README.md +++ b/internal/blog/README.md @@ -75,7 +75,6 @@ Must have a auth token for a user with the `"blog": "admin"` permission. ```json { - author: "authorID", favicon: "favicon url", title: "blog title", blog: "blog", // blog will have been converted to HTML diff --git a/internal/blog/author.go b/internal/blog/author.go index 9aae509..21ba3ec 100644 --- a/internal/blog/author.go +++ b/internal/blog/author.go @@ -12,6 +12,7 @@ import ( type Author struct { ID string `json:"id" bson:"_id"` + Name string `json:"name" bson:"name"` About string `json:"about" bson:"about"` PicURL string `json:"picurl" bson:"picurl"` } diff --git a/internal/blog/blog.go b/internal/blog/blog.go index 1d4ff85..e278fd9 100644 --- a/internal/blog/blog.go +++ b/internal/blog/blog.go @@ -23,9 +23,9 @@ type Blog struct { UpdateTime int `json:"updateTime" bson:"updateTime"` } -func (b *Blog) ConvertBlog() { +func (b *BlogApp) ConvertBlog(blog *Blog) { //TODO: parse BBCode/Markdown from blog - //b.Blog = bbCodeConvert(b.Blog) + blog.Blog = b.conv.Convert(blog.Blog) } func (b *BlogApp) GetAuthor(blog *Blog) (*Author, error) { @@ -54,7 +54,7 @@ func (b *BlogApp) GetBlog(ID string) (*Blog, error) { if err != nil { return nil, err } - blog.ConvertBlog() + b.ConvertBlog(&blog) return &blog, nil } @@ -78,7 +78,15 @@ func (b *BlogApp) Blog(w http.ResponseWriter, r *http.Request) { } func (b *BlogApp) CreateBlog(w http.ResponseWriter, r *http.Request) { - //TODO + hdr, err := b.back.ParseHeader(r) + if err != nil{ + if err == backend.ErrApiKeyUnauthorized{ + backend.ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application unauthorized") + return + }else if err == backend.ErrTokenUnauthorized{ + backend.ReturnError(w, http.StatusUnauthorized, "") + } + } } func (b *BlogApp) UpdateBlog(w http.ResponseWriter, r *http.Request) { @@ -102,7 +110,7 @@ func (b *BlogApp) GetLatestBlogs(page int64) ([]Blog, error) { return nil, err } for i := range out { - out[i].ConvertBlog() + b.ConvertBlog(&out[i]) } return out, nil } diff --git a/internal/blog/main.go b/internal/blog/main.go index 75556dc..4da7fa6 100644 --- a/internal/blog/main.go +++ b/internal/blog/main.go @@ -3,6 +3,7 @@ package blog import ( "net/http" + "github.com/CalebQ42/bbConvert" "github.com/CalebQ42/darkstorm-server/internal/backend" "go.mongodb.org/mongo-driver/mongo" ) @@ -11,6 +12,7 @@ type BlogApp struct { back *backend.Backend blogCol *mongo.Collection authCol *mongo.Collection + conv *bbConvert.HTMLConverter } func NewBlogApp(b *backend.Backend, db *mongo.Database, mux *http.ServeMux) *BlogApp { @@ -18,7 +20,9 @@ func NewBlogApp(b *backend.Backend, db *mongo.Database, mux *http.ServeMux) *Blo back: b, blogCol: db.Collection("blog"), authCol: db.Collection("author"), + conv: &bbConvert.HTMLConverter{}, } + out.conv.ImplementDefaults() // setup mux mux.HandleFunc("GET /blog", out.LatestBlogs) mux.HandleFunc("GET /blog/list", out.BlogList)