-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.go
More file actions
86 lines (78 loc) · 2.43 KB
/
Copy pathhelpers.go
File metadata and controls
86 lines (78 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"strings"
authz "github.com/CHESSComputing/golib/authz"
srvConfig "github.com/CHESSComputing/golib/config"
utils "github.com/CHESSComputing/golib/utils"
"github.com/gin-gonic/gin"
)
// helper function to extract schema name from schema file name
func schemaName(fname string) string {
arr := strings.Split(fname, "/")
return strings.Split(arr[len(arr)-1], ".")[0]
}
// helper function to write data in NDJSON data format
func handleNDJSON(c *gin.Context, records []map[string]any) {
// Set the Content-Type header to NDJSON
c.Header("Content-Type", "application/x-ndjson")
c.Status(http.StatusOK)
// Use the Gin context's Writer to stream the response
for _, record := range records {
// Marshal each record to JSON
line, err := json.Marshal(record)
if err != nil {
// Handle JSON marshalling error
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to serialize record"})
return
}
// Write each JSON line followed by a newline
_, _ = c.Writer.Write(append(line, '\n'))
}
}
// helper function to adjust timestamp in spec
func adjustTimestamp(specPtr *map[string]any) {
spec := *specPtr
if val, ok := spec["date"]; ok {
switch ts := val.(type) {
case string:
epoch, err := utils.RFC3339ToEpoch(ts)
if err == nil {
spec["date"] = epoch
}
}
}
if Verbose > 2 {
log.Printf("adjusted spec: %+v", spec)
}
}
func specRecord(tmplRec map[string]any) map[string]any {
rec := make(map[string]any)
// TODO: implement logic of getting spec record from tmpl record
return rec
}
// checkUserBtrAffilication validates user's btr affiliation
func checkUserBtrAffilication(btr string, req *http.Request) error {
tokenStr := authz.RequestToken(req)
claims, err := authz.TokenClaims(tokenStr, srvConfig.Config.Authz.ClientID)
if err != nil {
return err
}
btrs := claims.CustomClaims.Btrs
user := claims.CustomClaims.User
groups := claims.CustomClaims.Groups
if utils.InList("foxdenadmins", groups) {
// FOXDEN admins are allowed to perform any actions on metadata record
return nil
}
if utils.InList(btr, btrs) {
// user btr is in btrs list and therefore this user is allowed to perform any actions on metadata record
return nil
}
msg := fmt.Sprintf("[MetaData.main.checkUserBtrAffilication] user '%s' with btrs %+v is not authorized to perform %s action on record with btr=%s", user, btrs, req.Method, btr)
return errors.New(msg)
}