Read token from json

This commit is contained in:
Melon 2023-09-11 17:45:37 +01:00
parent 29b1694840
commit 675423c06e
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
2 changed files with 17 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package api package api
import ( import (
"bytes"
"encoding/json" "encoding/json"
"github.com/1f349/lotus/imap" "github.com/1f349/lotus/imap"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
@ -49,17 +50,25 @@ func SetupApiServer(listen string, auth *AuthChecker, send Smtp, recv Imap) *htt
return return
} }
// get a "possible" auth token value // parse token from message
authToken := string(msg) var tokenMsg struct {
Token string `json:"token"`
}
dec := json.NewDecoder(bytes.NewReader(msg))
dec.DisallowUnknownFields()
err = dec.Decode(&tokenMsg)
if err != nil {
return
}
// wait for authToken or error // get a "possible" auth token value
// exit on empty reply // exit on empty token value
if authToken == "" { if tokenMsg.Token == "" {
return return
} }
// check the token // check the token
authUser, err := auth.Check(authToken) authUser, err := auth.Check(tokenMsg.Token)
if err != nil { if err != nil {
// exit on error // exit on error
return return
@ -69,7 +78,7 @@ func SetupApiServer(listen string, auth *AuthChecker, send Smtp, recv Imap) *htt
client, err := recv.MakeClient(authUser.Subject) client, err := recv.MakeClient(authUser.Subject)
if err != nil { if err != nil {
_ = c.WriteJSON(map[string]string{"Error": "Making client failed"}) _ = c.WriteJSON(map[string]string{"error": "Making client failed"})
return return
} }

View File

@ -42,7 +42,7 @@ func (c *Client) HandleWS(action string, args []string) (map[string]any, error)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return map[string]any{"Info": a}, nil return map[string]any{"info": a}, nil
case "move": case "move":
// TODO: implementation // TODO: implementation
case "rename": case "rename":