From 675423c06ed695bc46904aa35525f3f3fa825997 Mon Sep 17 00:00:00 2001 From: MrMelon54 Date: Mon, 11 Sep 2023 17:45:37 +0100 Subject: [PATCH] Read token from json --- api/api.go | 23 ++++++++++++++++------- imap/client.go | 2 +- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/api/api.go b/api/api.go index e831193..916af47 100644 --- a/api/api.go +++ b/api/api.go @@ -1,6 +1,7 @@ package api import ( + "bytes" "encoding/json" "github.com/1f349/lotus/imap" "github.com/gorilla/websocket" @@ -49,17 +50,25 @@ func SetupApiServer(listen string, auth *AuthChecker, send Smtp, recv Imap) *htt return } - // get a "possible" auth token value - authToken := string(msg) + // parse token from message + 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 - // exit on empty reply - if authToken == "" { + // get a "possible" auth token value + // exit on empty token value + if tokenMsg.Token == "" { return } // check the token - authUser, err := auth.Check(authToken) + authUser, err := auth.Check(tokenMsg.Token) if err != nil { // exit on error return @@ -69,7 +78,7 @@ func SetupApiServer(listen string, auth *AuthChecker, send Smtp, recv Imap) *htt client, err := recv.MakeClient(authUser.Subject) if err != nil { - _ = c.WriteJSON(map[string]string{"Error": "Making client failed"}) + _ = c.WriteJSON(map[string]string{"error": "Making client failed"}) return } diff --git a/imap/client.go b/imap/client.go index c59e983..f85c162 100644 --- a/imap/client.go +++ b/imap/client.go @@ -42,7 +42,7 @@ func (c *Client) HandleWS(action string, args []string) (map[string]any, error) if err != nil { return nil, err } - return map[string]any{"Info": a}, nil + return map[string]any{"info": a}, nil case "move": // TODO: implementation case "rename":