/
models.go
107 lines (92 loc) · 2.47 KB
/
models.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
96
97
98
99
100
101
102
103
104
105
106
107
package client
import (
"fmt"
"strconv"
"time"
)
type Auth struct {
Username string
Password string
Enabled bool
}
type History []string
type StatusRes struct {
Status string `json:"status"`
}
type AuthYaml struct {
Username string `yaml:"otsUser"`
Password string `yaml:"otsToken"`
//Enabled bool `yaml:"OTS_AUTH_ENABLED"`
}
type ErrorResponse struct {
Message string `json:"message"`
}
type SecretBody struct {
Secret string
Passphrase string
Ttl int
Recipient string
}
type Secrets []Secret
type Secret struct {
Custid string `json:"custid"`
MetadataKey string `json:"metadata_key"`
SecretKey string `json:"secret_key"`
Ttl int `json:"ttl"`
MetadataTtl int `json:"metadata_ttl"`
SecretTtl int `json:"secret_ttl"`
Recipient []string `json:"recipient"`
CreatedAt int `json:"created"`
UpdatedAt int `json:"updated"`
PassphraseRequired bool `json:"passphrase_required"`
Value string `json:"value"`
ReceivedAt int `json:"received"`
State string `json:"state"`
}
type BurnSecretResponse struct {
State Secret `json:"state"`
}
func (sm Secret) secretUrl() string {
return fmt.Sprintf("%s/secret/%s\n", HOST, sm.SecretKey)
}
func (sm Secret) secretOtsCmd() string {
return fmt.Sprintf("Share via ots: `ots get secret %s`", sm.SecretKey)
}
func (sm Secret) toNewSecret() []string {
createdAt := time.Unix(int64(sm.CreatedAt), 0)
expireAt := createdAt.Add(time.Duration(sm.Ttl/2) * time.Second)
dateFmt := "2006-01-02 15:04:05"
expireAtFmt := fmt.Sprint(expireAt.Format(dateFmt))
return []string{
sm.Custid,
sm.secretUrl(),
expireAtFmt,
sm.MetadataKey,
fmt.Sprint(sm.Recipient),
}
}
func (sm Secret) toMetadata() []string {
timeNow := time.Now()
createdAt := time.Unix(int64(sm.CreatedAt), 0)
expireAt := createdAt.Add(time.Duration(sm.Ttl/2) * time.Second)
dateFmt := "2006-01-02 15:04:05"
expireAtFmt := fmt.Sprint(expireAt.Format(dateFmt))
createdAtFmt := fmt.Sprint(createdAt.Format(dateFmt))
user := sm.Custid
state := sm.State
expired := strconv.FormatBool(timeNow.After(expireAt))
metadataKey := sm.MetadataKey
passhphrase := strconv.FormatBool(sm.PassphraseRequired)
ttl := time.Duration(sm.SecretTtl) * time.Second
return []string{
user,
state,
expireAtFmt,
expired,
metadataKey,
passhphrase,
createdAtFmt,
fmt.Sprint(sm.Recipient),
ttl.String(),
}
}