More work on stuff
This commit is contained in:
@@ -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{}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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"`
|
||||
}
|
||||
|
||||
+13
-5
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user