add callback to validate tokens

This commit is contained in:
2026-03-13 17:26:50 +01:00
parent cfcbaee5f1
commit 2628f89154
3 changed files with 12 additions and 6 deletions

View File

@@ -1,4 +1,4 @@
package gotsclient package rpc
import ( import (
_ "embed" _ "embed"

2
go.mod
View File

@@ -1,4 +1,4 @@
module clientGen module RpcTsClient
go 1.26.1 go 1.26.1

View File

@@ -1,4 +1,4 @@
package gotsclient package rpc
import ( import (
"context" "context"
@@ -10,9 +10,9 @@ import (
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
) )
func WsHandler(api any) func(*websocket.Conn) { func WsHandler(apiStruct any, isTokenValid func(token string, method string, params []any) bool) func(*websocket.Conn) {
return func(ws *websocket.Conn) { return func(ws *websocket.Conn) {
apiVal := reflect.ValueOf(api) apiVal := reflect.ValueOf(apiStruct)
for { for {
var msg []byte var msg []byte
@@ -34,7 +34,13 @@ func WsHandler(api any) func(*websocket.Conn) {
continue continue
} }
log.Printf("%s, %v", req.Method, req.Params) if !isTokenValid(req.Token, req.Method, req.Params) {
_ = websocket.JSON.Send(ws, map[string]any{
"id": req.ID,
"error": "token not valid",
})
return
}
method := apiVal.MethodByName(req.Method) method := apiVal.MethodByName(req.Method)
if !method.IsValid() { if !method.IsValid() {