/
handlerComplain.go
227 lines (191 loc) · 5.58 KB
/
handlerComplain.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* ****************************************************************************
* Copyright 2020 51 Degrees Mobile Experts Limited (51degrees.com)
*
* 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 cmp
import (
"bytes"
"compress/gzip"
"fmt"
"net/http"
"net/url"
"strings"
"text/template"
"github.com/SWAN-community/swan-demo-go/common"
"github.com/SWAN-community/owid-go"
"github.com/SWAN-community/swan-go"
)
var complaintSubjectTemplate = newComplaintTemplate(
"subject",
"SWAN Complaint: {{ .Organization }}")
var complaintBodyTemplate = newComplaintTemplate("body", `
To whom it may concern,
I believe that {{ .Organization }} used my personal information without a
legal basis on {{ .Date }}.
I provided you the following permissions for use of this data.
Personalize Marketing: {{ .Preferences }}
You cryptographically signed this information. We therefore agree that you were
in posession of the information.
As an organization operating in '{{ .Country }}' you are bound by the following
rules.
{{ .DPRURL }}
I would be grateful if you can respond by email to this address within 7
working days.
Regards,
[INSERT YOU NAME]
--- DO NOT CHANGE THE TEXT BELOW THIS LINE ---
{{ .IDAsString }}
--- DO NOT CHANGE THE TEXT ABOVE THIS LINE ---`)
// Complaint used to format an email template.
type Complaint struct {
ID *swan.ID // The swan.ID that the complaint relates to
DPRURL string
Organization string
Country string
idOWID *owid.OWID // The ID as an OWID
}
// Date to use in the email template.
func (c *Complaint) Date() string {
return c.idOWID.Date.Format("2006-01-02 15:01")
}
// SWID to use in the email template.
func (c *Complaint) SWID() string {
return c.ID.SWID.AsString()
}
// SID to use in the email template.
func (c *Complaint) SID() string {
return c.ID.SID.AsString()
}
// Preferences string to use in the email template.
func (c *Complaint) Preferences() string {
return c.ID.PreferencesAsString()
}
// ID as a string
func (c *Complaint) IDAsString() (string, error) {
return c.ID.AsString()
}
// SWANOWID as a string
func (c *Complaint) SWANOWID() string {
return c.idOWID.AsString()
}
func newComplaintTemplate(n string, b string) *template.Template {
t, err := template.New(n).Parse(strings.TrimSpace(b))
if err != nil {
panic(err)
}
return t
}
func newComplaint(
cfg *common.Configuration,
swanOWID *owid.OWID,
partyOWID *owid.OWID) (*Complaint, error) {
var err error
// Set the static information associated with the complaint. These are
var c Complaint
c.DPRURL = "URL of the DPR"
c.Country = "Region of the CMP"
// Set the ID as an OWID.
c.idOWID = partyOWID
// Work out the swan.ID from the ID OWID provided.
c.ID, err = swan.IDFromOWID(swanOWID)
if err != nil {
return nil, err
}
// Set the organization as the domain for the moment.
c.Organization = partyOWID.Domain
// Return the complain data structure ready for the template email.
return &c, nil
}
func handlerComplain(
d *common.Domain,
w http.ResponseWriter,
r *http.Request) {
// Get the form values from the input request.
err := r.ParseForm()
if err != nil {
common.ReturnServerError(d.Config, w, err)
return
}
// Check that the SWAN ID and the Party ID are present.
if r.Form.Get("swanid") == "" {
common.ReturnStatusCodeError(
d.Config,
w,
fmt.Errorf("'swanid' missing"),
http.StatusBadRequest)
return
}
if r.Form.Get("partyid") == "" {
common.ReturnStatusCodeError(
d.Config,
w,
fmt.Errorf("'partyid' missing"),
http.StatusBadRequest)
return
}
swanOWID, err := owid.FromBase64(r.Form.Get("swanid"))
if err != nil {
common.ReturnStatusCodeError(
d.Config,
w,
fmt.Errorf("'swanid' not a valid OWID"),
http.StatusBadRequest)
return
}
partyOWID, err := owid.FromBase64(r.Form.Get("partyid"))
if err != nil {
common.ReturnStatusCodeError(
d.Config,
w,
fmt.Errorf("'partyid' not a valid OWID"),
http.StatusBadRequest)
return
}
// Create the complaint object.
c, err := newComplaint(d.Config, swanOWID, partyOWID)
if err != nil {
common.ReturnServerError(d.Config, w, err)
return
}
// Get the strings for the subject and the body.
var subject bytes.Buffer
err = complaintSubjectTemplate.Execute(&subject, c)
if err != nil {
common.ReturnServerError(d.Config, w, err)
return
}
var body bytes.Buffer
err = complaintBodyTemplate.Execute(&body, c)
if err != nil {
common.ReturnServerError(d.Config, w, err)
return
}
// Create the URL for the email.
u := fmt.Sprintf("mailto:info@%s?subject=%s&body=%s",
c.idOWID.Domain,
url.PathEscape(subject.String()),
url.PathEscape(body.String()))
// Return the URL as a text string.
g := gzip.NewWriter(w)
defer g.Close()
w.Header().Set("Content-Encoding", "gzip")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("Cache-Control", "no-cache")
_, err = g.Write([]byte(u))
if err != nil {
common.ReturnServerError(d.Config, w, err)
return
}
}