-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
generator.go
212 lines (184 loc) · 7.56 KB
/
generator.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package domainverifier
import (
"errors"
"fmt"
"github.com/egbakou/domainverifier/config"
"github.com/segmentio/ksuid"
"strings"
)
const (
jsonKeySuffix = "_site_verification"
jsonFileNameSuffix = "-site-verification.json"
xmlRootName = "verification"
xmlFileNameSuffix = "SiteAuth.xml"
txtRecordAttributeSuffix = "-site-verification"
)
// InvalidAppNameError indicates that the app name is invalid.
var InvalidAppNameError = errors.New("app name cannot be empty")
// HtmlMetaInstruction is the Html meta tag instruction.
type HtmlMetaInstruction struct {
Code string
Action string
}
// FileInstruction is the JSON or XML file instruction.
type FileInstruction struct {
FileName string
FileContent string
Action string
}
// DnsRecordInstruction is the CNAME or TXT record instruction.
type DnsRecordInstruction struct {
HostName string
Record string
Action string
}
// GenerateHtmlMetaFromConfig generates the HTML meta tag verification method instructions.
// It uses the provided config.HmlMetaTagGenerator to generate the instructions.
// If useInternalCode is true, internal K-Sortable Globally Unique ID will be generated.
// Otherwise, the code in the config.HmlMetaTagGenerator will be used.
func GenerateHtmlMetaFromConfig(config *config.HmlMetaTagGenerator, useInternalCode bool) (*HtmlMetaInstruction, error) {
if config != nil && useInternalCode {
config.Code = ksuid.New().String()
}
if err := config.Validate(); err != nil {
return nil, err
}
return &HtmlMetaInstruction{
Code: getMetaTagContent(config.TagName, config.Code),
Action: getMetaTagInstruction(config.TagName, config.Code),
}, nil
}
// GenerateHtmlMeta generates the HTML meta tag verification method instructions.
// appName is the name of the app that is requesting the verification (e.g. msvalidate.01, mysuperapp, etc.).
// It will be used as the name of the meta tag.
// Note that the appName will be sanitized to non-alphanumeric characters.
func GenerateHtmlMeta(appName string, sanitizeAppName bool) (*HtmlMetaInstruction, error) {
if strings.TrimSpace(appName) == "" {
return nil, InvalidAppNameError
}
if sanitizeAppName {
appName = sanitizeString(appName)
}
htmTagConfig := &config.HmlMetaTagGenerator{
TagName: appName,
Code: ksuid.New().String(),
}
return GenerateHtmlMetaFromConfig(htmTagConfig, false)
}
// GenerateJsonFromConfig generates the JSON verification method instructions.
// It uses the provided config.JsonGenerator to generate the instructions.
// If useInternalCode is true, internal K-Sortable Globally Unique ID will be generated.
// Otherwise, the code in the config.JsonGenerator will be used.
func GenerateJsonFromConfig(config *config.JsonGenerator, useInternalCode bool) (*FileInstruction, error) {
if config != nil && useInternalCode {
config.Code = ksuid.New().String()
}
if err := config.Validate(); err != nil {
return nil, err
}
fileName := ensureFileExtension(config.FileName, ".json")
return &FileInstruction{
FileName: config.FileName,
FileContent: getJsonContent(config.Attribute, config.Code),
Action: getJsonInstruction(fileName, config.Attribute, config.Code),
}, nil
}
// GenerateJson generates the JSON verification method instructions.
// appName is the name of the app that is requesting the verification (e.g. google, bing, etc.).
// It will be used as prefix of the file name and the attribute name.
// Note that the appName will be sanitized to non-alphanumeric characters.
func GenerateJson(appName string) (*FileInstruction, error) {
if strings.TrimSpace(appName) == "" {
return nil, InvalidAppNameError
}
appName = sanitizeString(appName)
jsonConfig := &config.JsonGenerator{
FileName: fmt.Sprintf("%s%s", appName, jsonFileNameSuffix),
Attribute: fmt.Sprintf("%s%s", appName, jsonKeySuffix),
Code: ksuid.New().String(),
}
return GenerateJsonFromConfig(jsonConfig, false)
}
// GenerateXmlFromConfig generates the XML verification method instructions.
// It uses the provided config.XmlGenerator to generate the instructions.
// If useInternalCode is true, internal K-Sortable Globally Unique ID will be generated.
// Otherwise, the code in the config.XmlGenerator will be used.
func GenerateXmlFromConfig(config *config.XmlGenerator, useInternalCode bool) (*FileInstruction, error) {
if config != nil && useInternalCode {
config.Code = ksuid.New().String()
}
if err := config.Validate(); err != nil {
return nil, err
}
fileName := ensureFileExtension(config.FileName, ".xml")
return &FileInstruction{
FileName: config.FileName,
FileContent: getXmlContent(config.RootName, config.Code),
Action: getXmlInstruction(fileName, config.RootName, config.Code),
}, nil
}
// GenerateXml generates the XML verification method instructions.
// appName is the name of the app that is requesting the verification (e.g. bing, google, etc.).
// It will be used as prefix of the file name.
// Note that the appName will be sanitized to non-alphanumeric characters.
func GenerateXml(appName string, sanitizeAppName bool) (*FileInstruction, error) {
if strings.TrimSpace(appName) == "" {
return nil, InvalidAppNameError
}
if sanitizeAppName {
appName = sanitizeString(appName)
// capitalize the first letter
appName = strings.ToUpper(appName[:1]) + appName[1:]
}
xmlConfig := &config.XmlGenerator{
FileName: fmt.Sprintf("%s%s", appName, xmlFileNameSuffix),
RootName: xmlRootName,
Code: ksuid.New().String(),
}
return GenerateXmlFromConfig(xmlConfig, false)
}
// GenerateTxtRecordFromConfig generates the TXT verification method instructions.
// It uses the provided config.TxtGenerator to generate the instructions.
// If useInternalCode is true, internal K-Sortable Globally Unique ID will be generated for the record attribute value.
// Otherwise, the RecordAttribute in the config.TxtGenerator will be used.
func GenerateTxtRecordFromConfig(config *config.TxtRecordGenerator, useInternalCode bool) (*DnsRecordInstruction, error) {
if config != nil && useInternalCode {
config.RecordAttributeValue = ksuid.New().String()
}
if err := config.Validate(); err != nil {
return nil, err
}
return &DnsRecordInstruction{
HostName: config.HostName,
Record: fmt.Sprintf("%s=%s", config.RecordAttribute, config.RecordAttributeValue),
Action: fmt.Sprintf(`Create a TXT record with the name %s and the content %s=%s`,
config.HostName, config.RecordAttribute, config.RecordAttributeValue),
}, nil
}
// GenerateTxtRecord generates the TXT verification method instructions.
// appName is the name of the app that is requesting the verification (e.g. bing, google, etc.).
// It will be used as prefix of the record attribute.
func GenerateTxtRecord(appName string) (*DnsRecordInstruction, error) {
if strings.TrimSpace(appName) == "" {
return nil, InvalidAppNameError
}
txtConfig := &config.TxtRecordGenerator{
HostName: rootDomain,
RecordAttribute: fmt.Sprintf("%s%s", appName, txtRecordAttributeSuffix),
RecordAttributeValue: ksuid.New().String(),
}
return GenerateTxtRecordFromConfig(txtConfig, false)
}
// GenerateCnameRecordFromConfig generates the CNAME verification method instructions.
// It uses the provided config.CnameGenerator to generate the instructions.
func GenerateCnameRecordFromConfig(config *config.CnameRecordGenerator) (*DnsRecordInstruction, error) {
if err := config.Validate(); err != nil {
return nil, err
}
return &DnsRecordInstruction{
HostName: config.RecordName,
Record: config.RecordTarget,
Action: fmt.Sprintf(`Add CNAME (alias) record with name %s and value %s.`,
config.RecordName, config.RecordTarget),
}, nil
}