remove token auth method

This commit is contained in:
2022-06-12 17:08:02 +08:00
parent 4199caa5ef
commit c7382a1561
6 changed files with 4 additions and 38 deletions

View File

@@ -3,7 +3,6 @@
"database_name": "music.sqlite3", "database_name": "music.sqlite3",
"single_thread": true, "single_thread": true,
"addr": ":8080", "addr": ":8080",
"token": "!! config your very strong token here !!",
"ffmpeg_threads": 1, "ffmpeg_threads": 1,
"ffmpeg_config_list": [ "ffmpeg_config_list": [
{ {

View File

@@ -37,7 +37,6 @@ func main() {
log.Println("Starting", log.Println("Starting",
config.APIConfig.DatabaseName, config.APIConfig.DatabaseName,
config.APIConfig.Addr, config.APIConfig.Addr,
config.APIConfig.Token,
) )
log.Fatal(api.Server.ListenAndServe()) log.Fatal(api.Server.ListenAndServe())
} }

View File

@@ -12,7 +12,6 @@ import (
type API struct { type API struct {
Db *database.Database Db *database.Database
Server http.Server Server http.Server
token string
APIConfig commonconfig.APIConfig APIConfig commonconfig.APIConfig
Tmpfs *tmpfs.Tmpfs Tmpfs *tmpfs.Tmpfs
store *sessions.CookieStore store *sessions.CookieStore
@@ -96,14 +95,12 @@ func NewAPI(config commonconfig.Config) (*API, error) {
apiMux.HandleFunc("/update_review", api.HandleUpdateReview) apiMux.HandleFunc("/update_review", api.HandleUpdateReview)
apiMux.HandleFunc("/delete_review", api.HandleDeleteReview) apiMux.HandleFunc("/delete_review", api.HandleDeleteReview)
apiMux.HandleFunc("/get_reviews_by_user", api.HandleGetReviewsByUser) apiMux.HandleFunc("/get_reviews_by_user", api.HandleGetReviewsByUser)
// below needs token // below needs admin
apiMux.HandleFunc("/walk", api.HandleWalk) apiMux.HandleFunc("/walk", api.HandleWalk)
apiMux.HandleFunc("/reset", api.HandleReset) apiMux.HandleFunc("/reset", api.HandleReset)
mux.Handle("/api/v1/", http.StripPrefix("/api/v1", apiMux)) mux.Handle("/api/v1/", http.StripPrefix("/api/v1", apiMux))
mux.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("web/build")))) mux.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("web/build"))))
api.token = apiConfig.Token
return api, nil return api, nil
} }

View File

@@ -11,19 +11,9 @@ type WalkRequest struct {
TagIDs []int64 `json:"tag_ids"` TagIDs []int64 `json:"tag_ids"`
} }
type ResetRequest struct {
Token string `json:"token"`
}
func (api *API) HandleReset(w http.ResponseWriter, r *http.Request) { func (api *API) HandleReset(w http.ResponseWriter, r *http.Request) {
resetRequest := &ResetRequest{} var err error
err := json.NewDecoder(r.Body).Decode(resetRequest) // check admin
if err != nil {
api.HandleError(w, r, err)
return
}
// check token
err = api.CheckAdmin(w, r) err = api.CheckAdmin(w, r)
if err != nil { if err != nil {
api.HandleError(w, r, err) api.HandleError(w, r, err)
@@ -53,7 +43,7 @@ func (api *API) HandleWalk(w http.ResponseWriter, r *http.Request) {
return return
} }
// check token match // check admin
err = api.CheckAdmin(w, r) err = api.CheckAdmin(w, r)
if err != nil { if err != nil {
api.HandleError(w, r, err) api.HandleError(w, r, err)

View File

@@ -1,18 +0,0 @@
package api
import (
"errors"
"log"
"net/http"
)
func (api *API) CheckToken(w http.ResponseWriter, r *http.Request, token string) error {
if token != api.token {
err := errors.New("token not matched")
log.Println("[api] [Warning] Token not matched", token)
api.HandleErrorCode(w, r, err, 403)
return err
}
log.Println("[api] Token passed")
return nil
}

View File

@@ -9,7 +9,6 @@ type APIConfig struct {
DatabaseName string `json:"database_name"` DatabaseName string `json:"database_name"`
SingleThread bool `json:"single_thread,default=true"` SingleThread bool `json:"single_thread,default=true"`
Addr string `json:"addr"` Addr string `json:"addr"`
Token string `json:"token"`
FfmpegThreads int64 `json:"ffmpeg_threads"` FfmpegThreads int64 `json:"ffmpeg_threads"`
FfmpegConfigList []FfmpegConfig `json:"ffmpeg_config_list"` FfmpegConfigList []FfmpegConfig `json:"ffmpeg_config_list"`
} }