-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidate.go
More file actions
182 lines (146 loc) · 4.13 KB
/
validate.go
File metadata and controls
182 lines (146 loc) · 4.13 KB
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
package pushover
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/url"
"strings"
"golang.org/x/net/context/ctxhttp"
)
// ValidateRequest is the data to POST to the Pushover
// REST API. See the Pushover Validate API documentation
// for more information on these parameters.
type ValidateRequest struct {
// The URL for the Pushover REST API POST.
//
// Leave this empty unless you wish to override the URL.
PushoverURL string
// Required fields
// Pushover API token
Token string
// The user's token to validate
User string
// User device to validate (optional)
Device string
}
// ValidateResponse is the response from this API. It is read from
// the body of the Pushover REST API response and translated
// to this response structure.
//
// For access to the original, untranslated response, access
// the ResponseBody field.
type ValidateResponse struct {
// Original response body from POST
ResponseBody string
// HTTP Status string
HTTPStatus string
// HTTP Status Code
HTTPStatusCode int
// The status as returned by the Pushover API.
//
// Value of 1 indicates 200 response received.
// Any other value indicates an error with the
// input.
APIStatus int
// This field is returned but not documented
// by the Pushover Validate API
Group int
// ID assigned to the request by Pushover
Request string
// List of registered devices
Devices []string
// List of licensed platforms
Licenses []string
// List of errors returned
//
// Empty if no errors
Errors []string
// Map of parameters and corresponding errors
//
// Empty if no errors
ErrorParameters map[string]string
}
// ValidateContext will submit a POST request to the Pushover
// Validate API. This function will check a user or group token
// to determine if it is valid.
//
// resp, err := pushover.ValidateContext(context.Background(),
// pushover.ValidateRequest{
// Token: token,
// User: user,
// })
func ValidateContext(ctx context.Context, request ValidateRequest) (*ValidateResponse, error) {
if len(request.PushoverURL) == 0 {
request.PushoverURL = validateURL
}
formData := url.Values{
keyToken: {request.Token},
keyUser: {request.User},
}
if len(request.Device) > 0 {
formData.Set(keyDevice, request.Device)
}
resp, err := ctxhttp.PostForm(ctx, &http.Client{}, request.PushoverURL, formData)
if err != nil {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
return nil, err
}
defer resp.Body.Close()
r := new(ValidateResponse)
body := &bytes.Buffer{}
_, err = body.ReadFrom(resp.Body)
if err != nil {
return nil, &ErrInvalidResponse{}
}
r.ResponseBody = body.String()
r.HTTPStatus = resp.Status
r.HTTPStatusCode = resp.StatusCode
// Decode json response
var result map[string]interface{}
if e := json.NewDecoder(strings.NewReader(string(r.ResponseBody))).Decode(&result); e != nil {
return nil, &ErrInvalidResponse{}
}
var ok bool
// Populate request status
if r.APIStatus, ok = mapKeyToInt(keyStatus, result); !ok {
return nil, &ErrInvalidResponse{}
}
delete(result, keyStatus)
// Populate request ID
if r.Request, ok = result[keyRequest].(string); !ok {
return nil, &ErrInvalidResponse{}
}
delete(result, keyRequest)
// Populate group
if r.Group, ok = mapKeyToInt(keyGroup, result); ok {
delete(result, keyGroup)
}
// Populate licenses
r.Licenses = interfaceArrayToStringArray(keyLicenses, result)
delete(result, keyLicenses)
// Populate devices
r.Devices = interfaceArrayToStringArray(keyDevices, result)
delete(result, keyDevices)
// Populate errors
r.Errors = interfaceArrayToStringArray(keyErrors, result)
delete(result, keyErrors)
// Populate parameters with corresponding errors
r.ErrorParameters = interfaceMapToStringMap(result)
return r, nil
}
// Validate will submit a POST request to the Pushover
// Validate API This function will check a user or group token
// to determine if it is valid.
//
// resp, err := pushover.Validate(pushover.ValidateRequest{
// Token: token,
// User: user,
// })
func Validate(request ValidateRequest) (*ValidateResponse, error) {
return ValidateContext(context.Background(), request)
}