From 346cc4e557ad760621302a5c03338e931cc7a32f Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 18 May 2021 16:06:49 +0200 Subject: [PATCH] feat: add dynamic scope from request's input - all occurences of '[abc]' where 'abc' is a valid input name ('name' field from json) is replaced with its value between square brackets --- handler.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/handler.go b/handler.go index 95c478c..f4b104b 100644 --- a/handler.go +++ b/handler.go @@ -1,7 +1,9 @@ package aicra import ( + "fmt" "net/http" + "strings" "git.xdrm.io/go/aicra/api" "git.xdrm.io/go/aicra/internal/config" @@ -50,8 +52,26 @@ func (s Handler) resolve(w http.ResponseWriter, r *http.Request) { return } + // replace format '[a]' in scope where 'a' is an existing input's name + scope := make([][]string, len(service.Scope)) + for a, list := range service.Scope { + scope[a] = make([]string, len(list)) + for b, perm := range list { + scope[a][b] = perm + for name, value := range input.Data { + if stringer, ok := value.(fmt.Stringer); ok { + scope[a][b] = strings.ReplaceAll( + scope[a][b], + fmt.Sprintf("[%s]", name), + fmt.Sprintf("[%s]", stringer), + ) + } + } + } + } + var auth = api.Auth{ - Required: service.Scope, + Required: scope, Active: []string{}, }