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
This commit is contained in:
xdrm-brackets 2021-05-18 16:06:49 +02:00
parent 214e2348aa
commit 346cc4e557
No known key found for this signature in database
GPG Key ID: 99F952DD5DE2439E
1 changed files with 21 additions and 1 deletions

View File

@ -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{},
}