/
ServiceController.go
347 lines (281 loc) · 10.3 KB
/
ServiceController.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright 2013-2018 Adam Presley. All rights reserved
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
package controllers
import (
"bytes"
"encoding/base64"
"math"
"net/http"
"strconv"
"time"
"strings"
"github.com/labstack/echo"
"github.com/mailslurper/mailslurper/pkg/auth/auth"
"github.com/mailslurper/mailslurper/pkg/auth/authfactory"
"github.com/mailslurper/mailslurper/pkg/auth/jwt"
"github.com/mailslurper/mailslurper/pkg/cache"
"github.com/mailslurper/mailslurper/pkg/contexts"
"github.com/mailslurper/mailslurper/pkg/mailslurper"
"github.com/sirupsen/logrus"
)
/*
ServiceController provides methods for handling service endpoints.
This is to primarily support the API
*/
type ServiceController struct {
AuthFactory authfactory.IAuthFactory
CacheService cache.ICacheService
Config *mailslurper.Configuration
Database mailslurper.IStorage
JWTService jwt.IJWTService
Logger *logrus.Entry
ServerVersion string
}
/*
DeleteMail is a request to delete mail items. This expects a body containing
a DeleteMailRequest object.
DELETE: /mail/{pruneCode}
*/
func (c *ServiceController) DeleteMail(ctx echo.Context) error {
var err error
var deleteMailRequest *mailslurper.DeleteMailRequest
var rowsDeleted int64
context := contexts.GetAdminContext(ctx)
if err = ctx.Bind(&deleteMailRequest); err != nil {
c.Logger.Errorf("Invalid delete request in DeleteMail: %s", err.Error())
return context.String(http.StatusBadRequest, "Invalid delete request")
}
if !deleteMailRequest.PruneCode.IsValid() {
c.Logger.Errorf("Attempt to use invalid prune code - %s", deleteMailRequest.PruneCode)
return context.String(http.StatusBadRequest, "Invalid prune type")
}
startDate := deleteMailRequest.PruneCode.ConvertToDate()
if rowsDeleted, err = c.Database.DeleteMailsAfterDate(startDate); err != nil {
c.Logger.Errorf("Problem deleting mails with code %s - %s", deleteMailRequest.PruneCode.String(), err.Error())
return context.String(http.StatusInternalServerError, "There was a problem deleting mails")
}
c.Logger.Infof("Deleting %d mails, code %s before %s", rowsDeleted, deleteMailRequest.PruneCode.String(), startDate)
return context.String(http.StatusOK, strconv.Itoa(int(rowsDeleted)))
}
/*
GetMail returns a single mail item by ID.
GET: /mail/{id}
*/
func (c *ServiceController) GetMail(ctx echo.Context) error {
var mailID string
var result *mailslurper.MailItem
var err error
var mailBody string
var convertSucess bool
context := contexts.GetAdminContext(ctx)
mailID = context.Param("id")
/*
* Retrieve the mail item
*/
if result, err = c.Database.GetMailByID(mailID); err != nil {
c.Logger.Errorf("Problem getting mail item %s - %s", mailID, err.Error())
return context.String(http.StatusInternalServerError, "Problem getting mail item")
}
if mailBody, convertSucess = c.ConvertFromBase64(result.Body); convertSucess==true{
result.Body=mailBody
result.HTMLBody=mailBody
}
c.Logger.Infof("Mail item %s retrieved", mailID)
return context.JSON(http.StatusOK, result)
}
/*
GetMailCollection returns a collection of mail items. This is constrianed
by a page number. A page of data contains 50 items.
GET: /mails?pageNumber={pageNumber}
*/
func (c *ServiceController) GetMailCollection(ctx echo.Context) error {
var err error
var pageNumberString string
var pageNumber int
var mailCollection []*mailslurper.MailItem
var totalRecordCount int
context := contexts.GetAdminContext(ctx)
/*
* Validate incoming arguments. A page is currently 50 items, hard coded
*/
pageNumberString = context.QueryParam("pageNumber")
if pageNumberString == "" {
pageNumber = 1
} else {
if pageNumber, err = strconv.Atoi(pageNumberString); err != nil {
c.Logger.Errorf("Invalid page number passed to GetMailCollection - %s", pageNumberString)
return context.String(http.StatusBadRequest, "A valid page number is required")
}
}
length := 50
offset := (pageNumber - 1) * length
/*
* Retrieve mail items
*/
mailSearch := &mailslurper.MailSearch{
Message: context.QueryParam("message"),
Start: context.QueryParam("start"),
End: context.QueryParam("end"),
From: context.QueryParam("from"),
To: context.QueryParam("to"),
OrderByField: context.QueryParam("orderby"),
OrderByDirection: context.QueryParam("dir"),
}
if mailCollection, err = c.Database.GetMailCollection(offset, length, mailSearch); err != nil {
c.Logger.Errorf("Problem getting mail collection - %s", err.Error())
return context.String(http.StatusInternalServerError, "Problem getting mail collection")
}
if totalRecordCount, err = c.Database.GetMailCount(mailSearch); err != nil {
c.Logger.Errorf("Problem getting record count in GetMailCollection - %s", err.Error())
return context.String(http.StatusInternalServerError, "Error getting record count")
}
totalPages := int(math.Ceil(float64(totalRecordCount / length)))
if totalPages*length < totalRecordCount {
totalPages++
}
c.Logger.Infof("Mail collection page %d retrieved", pageNumber)
result := &mailslurper.MailCollectionResponse{
MailItems: mailCollection,
TotalPages: totalPages,
TotalRecords: totalRecordCount,
}
return context.JSON(http.StatusOK, result)
}
/*
GetMailCount returns the number of mail items in storage.
GET: /mailcount
*/
func (c *ServiceController) GetMailCount(ctx echo.Context) error {
var err error
var mailItemCount int
context := contexts.GetAdminContext(ctx)
/*
* Get the count
*/
if mailItemCount, err = c.Database.GetMailCount(&mailslurper.MailSearch{}); err != nil {
c.Logger.Errorf("Problem getting mail item count in GetMailCount - %s", err.Error())
return context.String(http.StatusInternalServerError, "Problem getting mail count")
}
c.Logger.Infof("Mail item count - %d", mailItemCount)
result := &mailslurper.MailCountResponse{
MailCount: mailItemCount,
}
return context.JSON(http.StatusOK, result)
}
/*
GetMailMessage returns the message contents of a single mail item
GET: /mail/{id}/message
*/
func (c *ServiceController) GetMailMessage(ctx echo.Context) error {
var mailID string
var mailItem *mailslurper.MailItem
var err error
var mailBody string
var convertSucess bool
context := contexts.GetAdminContext(ctx)
mailID = context.Param("id")
/*
* Retrieve the mail item
*/
if mailItem, err = c.Database.GetMailByID(mailID); err != nil {
c.Logger.Errorf("Problem getting mail item %s in GetMailMessage - %s", mailID, err.Error())
return context.String(http.StatusInternalServerError, "Problem getting mail item")
}
if mailBody, convertSucess = c.ConvertFromBase64(mailItem.Body); convertSucess==true{
return context.HTML(http.StatusOK, mailBody)
}
c.Logger.Infof("Mail item %s retrieved", mailID)
return context.HTML(http.StatusOK, mailItem.Body)
}
func (c *ServiceController ) ConvertFromBase64(s string) (string,bool) {
var mailBody []byte
var err error
if mailBody, err = base64.StdEncoding.DecodeString(strings.Replace( s, " ", "", -1) ); err == nil {
return string(mailBody[:]),true
}
return "",false
}
/*
GetPruneOptions retrieves the set of options available to users for pruning
GET: /pruneoptions
*/
func (c *ServiceController) GetPruneOptions(ctx echo.Context) error {
context := contexts.GetAdminContext(ctx)
return context.JSON(http.StatusOK, mailslurper.PruneOptions)
}
/*
DownloadAttachment retrieves binary database from storage and streams
it back to the caller
GET: /mail/{mailID}/attachment/{attachmentID}
*/
func (c *ServiceController) DownloadAttachment(ctx echo.Context) error {
var err error
var attachmentID string
var mailID string
var attachment *mailslurper.Attachment
var data []byte
context := contexts.GetAdminContext(ctx)
mailID = context.Param("mailID")
attachmentID = context.Param("attachmentID")
/*
* Retrieve the attachment
*/
if attachment, err = c.Database.GetAttachment(mailID, attachmentID); err != nil {
c.Logger.Errorf("Problem getting attachment %s - %s", attachmentID, err.Error())
return context.String(http.StatusInternalServerError, "Error getting attachment")
}
/*
* Decode the base64 data and stream it back
*/
if attachment.IsContentBase64() {
if data, err = base64.StdEncoding.DecodeString(attachment.Contents); err != nil {
c.Logger.Errorf("Problem decoding attachment %s - %s", attachmentID, err.Error())
return context.String(http.StatusInternalServerError, "Cannot decode attachment")
}
} else {
data = []byte(attachment.Contents)
}
c.Logger.Infof("Attachment %s retrieved", attachmentID)
reader := bytes.NewReader(data)
return context.Stream(http.StatusOK, attachment.Headers.ContentType, reader)
}
/*
Login is an endpoint used to create a JWT token for use in service calls.
This also stores that token in an in-memory cache so when a user logs
out that token can be rendered invalid
*/
func (c *ServiceController) Login(ctx echo.Context) error {
var err error
var token string
var encryptedToken string
authService := c.AuthFactory.Get()
credentials := &auth.AuthCredentials{
UserName: ctx.FormValue("userName"),
Password: ctx.FormValue("password"),
}
if err = authService.Login(credentials); err != nil {
c.Logger.WithError(err).Errorf("Invalid service login attempt")
return ctx.String(http.StatusForbidden, "Invalid credentials")
}
if token, err = c.JWTService.CreateToken(c.Config.AuthSecret, credentials.UserName); err != nil {
c.Logger.WithError(err).Errorf("Problem creating token in service login")
return ctx.String(http.StatusInternalServerError, "Problem creating JWT token")
}
if encryptedToken, err = c.JWTService.EncryptToken(token); err != nil {
c.Logger.WithError(err).Errorf("Error encrypting JWT token")
return ctx.String(http.StatusInternalServerError, "Error encrypting JWT token")
}
c.CacheService.Set(credentials.UserName, encryptedToken, time.Minute*time.Duration(c.Config.AuthTimeoutInMinutes))
c.Logger.WithField("token", encryptedToken).Debugf("Encrypted JWT token generated")
return ctx.String(http.StatusOK, encryptedToken)
}
func (c *ServiceController) Logout(ctx echo.Context) error {
context := contexts.GetAdminContext(ctx)
c.CacheService.Delete(context.User)
return context.String(http.StatusOK, "OK")
}
func (c *ServiceController) Version(ctx echo.Context) error {
context := contexts.GetAdminContext(ctx)
return context.String(http.StatusOK, c.ServerVersion)
}