This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
validator_gravatar.go
154 lines (126 loc) · 3.81 KB
/
validator_gravatar.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
package ev
import (
"crypto/md5" //nolint:gosec
"fmt"
"github.com/go-email-validator/go-email-validator/pkg/ev/utils"
"net/http"
"time"
)
const (
// GravatarValidatorName is name for validation by https://www.gravatar.com/
GravatarValidatorName ValidatorName = "Gravatar"
// GravatarURL is url for gravatar validation
GravatarURL string = "https://www.gravatar.com/avatar/%x?d=404"
// DefaultGravatarTimeout is default timeout for gravatar connection
DefaultGravatarTimeout = 5 * time.Second
)
// GravatarErr is text for GravatarError.Error
const GravatarErr = "GravatarError"
// GravatarValidationResult is result of GravatarValidatorName
type GravatarValidationResult interface {
URL() string
ValidationResult
}
// NewGravatarValidationResult instantiates result of GravatarValidationResult
func NewGravatarValidationResult(url string, result *AValidationResult) GravatarValidationResult {
return gravatarValidationResult{url: url, AValidationResult: result}
}
// GetMD5Hash is function interface, which returns md5 for string
type GetMD5Hash func(str string) []byte
// DefaultGetMD5Hash is a default implementation of GetMD5Hash
func DefaultGetMD5Hash(str string) []byte {
h := md5.New()
h.Write([]byte(str))
return h.Sum(nil)
}
// GravatarError is GravatarValidatorName error
type GravatarError struct{}
func (GravatarError) Error() string {
return GravatarErr
}
// NewGravatarValidator instantiates GravatarValidatorName validator with GravatarURL for validation
func NewGravatarValidator() Validator {
return NewGravatarValidatorWithURL(GravatarURL)
}
// NewGravatarValidatorWithURL instantiates GravatarValidatorName validator
func NewGravatarValidatorWithURL(gravatarURL string) Validator {
return gravatarValidator{
h: DefaultGetMD5Hash,
url: gravatarURL,
}
}
type gravatarValidator struct {
AValidatorWithoutDeps
h GetMD5Hash
url string
}
func (g gravatarValidator) GetDeps() []ValidatorName {
return []ValidatorName{SyntaxValidatorName}
}
func (g gravatarValidator) Validate(input Input, results ...ValidationResult) ValidationResult {
syntaxResult := results[0].(SyntaxValidatorResult)
if !syntaxResult.IsValid() {
return gravatarGetError(NewDepsError())
}
var opts = DefaultGravatarOptions()
if optsInterface := input.Option(GravatarValidatorName); optsInterface != nil {
opts = optsInterface.(GravatarOptions)
}
client := &http.Client{Timeout: opts.Timeout()}
gravatarURL := fmt.Sprintf(
GravatarURL,
g.h(input.Email().String()),
)
resp, err := client.Head(gravatarURL)
if err != nil {
return gravatarGetError(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return gravatarGetError(GravatarError{})
}
return NewGravatarValidationResult(
gravatarURL,
NewValidResult(GravatarValidatorName).(*AValidationResult),
)
}
func gravatarGetError(err error) ValidationResult {
return NewGravatarValidationResult(
"",
NewResult(false, utils.Errs(err), nil, GravatarValidatorName).(*AValidationResult),
)
}
type gravatarValidationResult struct {
*AValidationResult
url string
}
func (v gravatarValidationResult) URL() string {
return v.url
}
// GravatarOptions describes gravatar options
type GravatarOptions interface {
Timeout() time.Duration
}
// GravatarOptionsDTO is dto for NewGravatarOptions
type GravatarOptionsDTO struct {
Timeout time.Duration
}
var defaultOptions = NewGravatarOptions(GravatarOptionsDTO{
Timeout: DefaultGravatarTimeout,
})
// DefaultGravatarOptions returns options with default values
func DefaultGravatarOptions() GravatarOptions {
return defaultOptions
}
// NewGravatarOptions instantiates GravatarOptions
func NewGravatarOptions(dto GravatarOptionsDTO) GravatarOptions {
return &gravatarOptions{
timeout: dto.Timeout,
}
}
type gravatarOptions struct {
timeout time.Duration
}
func (i *gravatarOptions) Timeout() time.Duration {
return i.timeout
}