-
Notifications
You must be signed in to change notification settings - Fork 70
/
sharedrecords_db.go
95 lines (89 loc) · 2.35 KB
/
sharedrecords_db.go
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
87
88
89
90
91
92
93
94
95
package main
import (
"errors"
"fmt"
"strings"
"time"
uuid "github.com/hashicorp/go-uuid"
"github.com/securitybunker/databunker/src/storage"
"go.mongodb.org/mongo-driver/bson"
)
func (dbobj dbcon) saveSharedRecord(userTOKEN string, fields string, expiration string, session string, appName string, partner string, conf Config) (string, error) {
if isValidUUID(userTOKEN) == false {
return "", errors.New("bad uuid")
}
if len(expiration) == 0 {
return "", errors.New("failed to parse expiration")
}
if len(appName) > 0 {
apps, _ := dbobj.listAllApps(conf)
if strings.Contains(string(apps), appName) == false {
return "", errors.New("app not found")
}
}
fmt.Printf("Expiration is : %s\n", expiration)
start, err := parseExpiration(expiration)
if err != nil {
return "", err
}
// check if user record exists
record, err := dbobj.lookupUserRecord(userTOKEN)
if record == nil || err != nil {
// not found
return "", errors.New("not found")
}
recordUUID, err := uuid.GenerateUUID()
if err != nil {
return "", err
}
now := int32(time.Now().Unix())
bdoc := bson.M{}
bdoc["token"] = userTOKEN
bdoc["record"] = recordUUID
bdoc["when"] = now
bdoc["endtime"] = start
if len(fields) > 0 {
bdoc["fields"] = fields
}
if len(appName) > 0 {
bdoc["app"] = appName
}
if len(partner) > 0 {
bdoc["partner"] = partner
}
if len(session) > 0 {
bdoc["session"] = session
}
_, err = dbobj.store.CreateRecord(storage.TblName.Sharedrecords, &bdoc)
if err != nil {
return "", err
}
return recordUUID, nil
}
func (dbobj dbcon) getSharedRecord(recordUUID string) (checkRecordResult, error) {
var result checkRecordResult
if isValidUUID(recordUUID) == false {
return result, errors.New("failed to authenticate")
}
record, err := dbobj.store.GetRecord(storage.TblName.Sharedrecords, "record", recordUUID)
if record == nil || err != nil {
return result, errors.New("failed to authenticate")
}
result.name = recordUUID
// tokenType = temp
now := int32(time.Now().Unix())
if now > record["endtime"].(int32) {
return result, errors.New("xtoken expired")
}
result.token = record["token"].(string)
if value, ok := record["fields"]; ok {
result.fields = value.(string)
}
if value, ok := record["session"]; ok {
result.session = value.(string)
}
if value, ok := record["app"]; ok {
result.appName = value.(string)
}
return result, nil
}