/
types.go
128 lines (112 loc) · 3.25 KB
/
types.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
Copyright AppsCode Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package server
import (
"fmt"
"strconv"
"github.com/avct/uasurfer"
"github.com/google/uuid"
. "gomodules.xyz/email-providers"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type ProductLicense struct {
Domain string `json:"domain"`
Product string `json:"product"` // This is now called plan in a parsed LicenseInfo
TTL *metav1.Duration `json:"ttl,omitempty"`
Agreement *LicenseAgreement `json:"agreement,omitempty"`
}
type LicenseAgreement struct {
NumClusters int `json:"num_clusters"`
ExpiryDate metav1.Time `json:"expiry_date"`
}
type RegisterRequest struct {
Email string `form:"email" binding:"Required;Email" json:"email"`
}
type LicenseForm struct {
Name string `form:"name" binding:"Required" json:"name"`
Email string `form:"email" binding:"Required;Email" json:"email"`
CC string `form:"cc" json:"cc"`
Product string `form:"product" binding:"Required" json:"product"` // This is now called plan in a parsed LicenseInfo
Cluster string `form:"cluster" binding:"Required" json:"cluster"`
Tos string `form:"tos" binding:"Required" json:"tos"`
Token string `form:"token" json:"token"`
}
type LicenseMailData struct {
LicenseForm `json:",inline,omitempty"`
License string
}
func (form LicenseForm) Validate() error {
_, err := uuid.Parse(form.Cluster)
if err != nil {
return err
}
if _, found := SupportedProducts[form.Product]; !found {
return fmt.Errorf("unknown product: %s", form.Product)
}
if agree, _ := strconv.ParseBool(form.Tos); !agree {
return fmt.Errorf("user must agree to terms and services")
}
return nil
}
type LogEntry struct {
LicenseForm `json:",inline,omitempty"`
GeoLocation `json:",inline,omitempty"`
Timestamp string `json:"timestamp,omitempty"`
UA *uasurfer.UserAgent `json:"-"`
}
type GeoLocation struct {
IP string `json:"ip,omitempty"`
Timezone string `json:"timezone,omitempty"`
City string `json:"city,omitempty"`
Country string `json:"country,omitempty"`
Coordinates string `json:"coordinates,omitempty"`
}
func (_ LogEntry) Headers() []string {
return []string{
"Domain",
"Name",
"Email",
"Product",
"Cluster",
"Timestamp",
"IP",
"Timezone",
"City",
"Country",
"Coordinates",
"Client OS",
"Client Device",
}
}
func (info LogEntry) Data() []string {
var clientOS, clientDevice string
if info.UA != nil {
clientOS = info.UA.OS.Name.StringTrimPrefix()
clientDevice = info.UA.DeviceType.StringTrimPrefix()
}
return []string{
Domain(info.Email),
info.Name,
info.Email,
info.Product,
info.Cluster,
info.Timestamp,
info.IP,
info.Timezone,
info.City,
info.Country,
info.Coordinates,
clientOS,
clientDevice,
}
}