-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase-controller.go
353 lines (290 loc) · 9.46 KB
/
base-controller.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
348
349
350
351
352
353
package controllers
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/astaxie/beego"
"github.com/opensourceways/app-cla-server/config"
"github.com/opensourceways/app-cla-server/models"
"github.com/opensourceways/app-cla-server/util"
)
type failedApiResult struct {
reason error
errCode string
statusCode int
}
func newFailedApiResult(statusCode int, errCode string, err error) *failedApiResult {
return &failedApiResult{
statusCode: statusCode,
errCode: errCode,
reason: err,
}
}
type baseController struct {
beego.Controller
}
func (this *baseController) sendResponse(body interface{}, statusCode int) {
if token, err := this.refreshAccessToken(); err == nil {
this.setCookies(map[string]string{apiAccessToken: token}, true)
}
if statusCode != 0 {
// if success, don't set status code, otherwise the header set in this.ServeJSON
// will not work. The reason maybe the same as above.
this.Ctx.ResponseWriter.WriteHeader(statusCode)
}
this.Data["json"] = struct {
Data interface{} `json:"data"`
}{
Data: body,
}
this.ServeJSON()
}
func (this *baseController) sendSuccessResp(body interface{}) {
this.sendResponse(body, 0)
}
func (this *baseController) newFuncForSendingFailedResp(action string) func(fr *failedApiResult) {
return func(fr *failedApiResult) {
this.sendFailedResponse(fr.statusCode, fr.errCode, fr.reason, action)
}
}
func (this *baseController) sendModelErrorAsResp(err models.IModelError, action string) {
this.sendFailedResultAsResp(parseModelError(err), action)
}
func (this *baseController) sendFailedResultAsResp(fr *failedApiResult, action string) {
this.sendFailedResponse(fr.statusCode, fr.errCode, fr.reason, action)
}
func (this *baseController) sendFailedResponse(statusCode int, errCode string, reason error, action string) {
if statusCode >= 500 {
beego.Error(fmt.Sprintf("Failed to %s, errCode: %s, err: %s", action, errCode, reason.Error()))
errCode = errSystemError
reason = fmt.Errorf("system error")
}
d := struct {
ErrCode string `json:"error_code"`
ErrMsg string `json:"error_message"`
}{
ErrCode: fmt.Sprintf("cla.%s", errCode),
ErrMsg: reason.Error(),
}
this.sendResponse(d, statusCode)
}
func (this *baseController) newApiToken(permission, addr string, pl interface{}) (string, error) {
ac := &accessController{
Expiry: util.Expiry(config.AppConfig.APITokenExpiry),
Permission: permission,
Payload: pl,
RemoteAddr: addr,
}
return ac.newToken(config.AppConfig.APITokenKey)
}
func (this *baseController) refreshAccessToken() (string, *failedApiResult) {
ac, fr := this.getAccessController()
if fr != nil {
return "", fr
}
token, err := ac.refreshToken(config.AppConfig.APITokenExpiry, config.AppConfig.APITokenKey)
if err == nil {
return token, nil
}
return "", newFailedApiResult(500, errSystemError, err)
}
func (this *baseController) tokenPayloadBasedOnCodePlatform() (*acForCodePlatformPayload, *failedApiResult) {
ac, fr := this.getAccessController()
if fr != nil {
return nil, fr
}
if pl, ok := ac.Payload.(*acForCodePlatformPayload); ok {
return pl, nil
}
return nil, newFailedApiResult(500, errSystemError, fmt.Errorf("invalid token payload"))
}
func (this *baseController) tokenPayloadBasedOnCorpManager() (*acForCorpManagerPayload, *failedApiResult) {
ac, fr := this.getAccessController()
if fr != nil {
return nil, fr
}
if pl, ok := ac.Payload.(*acForCorpManagerPayload); ok {
return pl, nil
}
return nil, newFailedApiResult(500, errSystemError, fmt.Errorf("invalid token payload"))
}
func (this *baseController) fetchInputPayload(info interface{}) *failedApiResult {
return fetchInputPayloadData(&this.Ctx.Input.RequestBody, info)
}
func (this *baseController) fetchInputPayloadFromFormData(info interface{}) *failedApiResult {
input := []byte(this.Ctx.Request.FormValue("data"))
return fetchInputPayloadData(&input, info)
}
func (this *baseController) checkPathParameter() *failedApiResult {
rp := this.routerPattern()
if rp == "" {
return nil
}
items := strings.Split(rp, "/")
for _, item := range items {
if strings.HasPrefix(item, ":") && this.GetString(item) == "" {
return newFailedApiResult(
400, errMissingURLPathParameter,
fmt.Errorf("missing path parameter:%s", item))
}
}
return nil
}
func (this *baseController) routerPattern() string {
if v, ok := this.Data["RouterPattern"]; ok {
return v.(string)
}
return ""
}
func (this *baseController) apiPrepare(permission string) {
if permission != "" {
this.apiPrepareWithAC(
this.newAccessController(permission),
[]string{permission},
)
} else {
this.apiPrepareWithAC(nil, nil)
}
}
func (this *baseController) apiPrepareWithAC(ac *accessController, permission []string) {
if fr := this.checkPathParameter(); fr != nil {
this.sendFailedResultAsResp(fr, "")
this.StopRun()
}
if ac != nil && permission != nil {
if fr := this.checkApiReqToken(ac, permission); fr != nil {
this.sendFailedResultAsResp(fr, "")
this.StopRun()
}
this.Data[apiAccessController] = *ac
}
}
func (this *baseController) newAccessController(permission string) *accessController {
var acp interface{}
switch permission {
case PermissionOwnerOfOrg:
acp = &acForCodePlatformPayload{}
case PermissionIndividualSigner:
acp = &acForCodePlatformPayload{}
case PermissionCorpAdmin:
acp = &acForCorpManagerPayload{}
case PermissionEmployeeManager:
acp = &acForCorpManagerPayload{}
}
return &accessController{Payload: acp}
}
func (this *baseController) checkApiReqToken(ac *accessController, permission []string) *failedApiResult {
// Fetch token from Header firstly to avoid fetching wrong token when changing to login as corp manager
// from community manager. Because the token exists in the cookie always.
token := this.apiReqHeader(apiHeaderToken)
if token == "" {
if token = this.Ctx.Input.Cookie(apiAccessToken); token == "" {
return newFailedApiResult(401, errMissingToken, fmt.Errorf("no token passed"))
}
}
if err := ac.parseToken(token, config.AppConfig.APITokenKey); err != nil {
return newFailedApiResult(401, errUnknownToken, err)
}
if ac.isTokenExpired() {
return newFailedApiResult(403, errExpiredToken, fmt.Errorf("token is expired"))
}
addr, fr := this.getRemoteAddr()
if fr != nil {
return fr
}
if err := ac.verify(permission, addr); err != nil {
return newFailedApiResult(403, errUnauthorizedToken, err)
}
return nil
}
func (this *baseController) getAccessController() (*accessController, *failedApiResult) {
ac, ok := this.Data[apiAccessController]
if !ok {
return nil, newFailedApiResult(500, errSystemError, fmt.Errorf("no access controller"))
}
if v, ok := ac.(accessController); ok {
return &v, nil
}
return nil, newFailedApiResult(500, errSystemError, fmt.Errorf("can't convert to access controller instance"))
}
func (this *baseController) apiReqHeader(h string) string {
return this.Ctx.Input.Header(h)
}
func (this *baseController) apiRequestMethod() string {
return this.Ctx.Request.Method
}
func (this *baseController) isPostRequest() bool {
return this.apiRequestMethod() == http.MethodPost
}
func (this *baseController) readInputFile(fileName string, maxSize int) ([]byte, *failedApiResult) {
f, _, err := this.GetFile(fileName)
if err != nil {
return nil, newFailedApiResult(400, errReadingFile, err)
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
return nil, newFailedApiResult(500, errSystemError, err)
}
if maxSize > 0 && len(data) > maxSize {
return nil, newFailedApiResult(400, errTooBigPDF, fmt.Errorf("big pdf file"))
}
if http.DetectContentType(data) != contentTypeOfPDF {
return nil, newFailedApiResult(400, errNotPDFFile, fmt.Errorf("not pdf file"))
}
return data, nil
}
func (this *baseController) downloadFile(file string) {
output := this.Ctx.Output
// check get file error, file not found or other error.
if _, err := os.Stat(file); err != nil {
http.ServeFile(output.Context.ResponseWriter, output.Context.Request, file)
return
}
fName := filepath.Base(file)
//https://tools.ietf.org/html/rfc6266#section-4.3
fn := url.PathEscape(fName)
if fName == fn {
fn = "filename=" + fn
} else {
/**
The parameters "filename" and "filename*" differ only in that
"filename*" uses the encoding defined in [RFC5987], allowing the use
of characters not present in the ISO-8859-1 character set
([ISO-8859-1]).
*/
fn = "filename=" + fName + "; filename*=utf-8''" + fn
}
output.ContentType(filepath.Ext(file))
output.Header("Content-Disposition", "attachment; "+fn)
output.Header("Content-Description", "File Transfer")
output.Header("Content-Transfer-Encoding", "binary")
output.Header("Expires", "0")
output.Header("Cache-Control", "must-revalidate")
output.Header("Pragma", "public")
http.ServeFile(output.Context.ResponseWriter, output.Context.Request, file)
}
func (this *baseController) redirect(webRedirectDir string) {
http.Redirect(
this.Ctx.ResponseWriter, this.Ctx.Request, webRedirectDir, http.StatusFound,
)
}
func (this *baseController) setCookies(value map[string]string, isSensitive bool) {
for k, v := range value {
this.Ctx.SetCookie(k, v, 3600, "/", "", true, isSensitive)
}
}
func (this *baseController) getRemoteAddr() (string, *failedApiResult) {
ips := this.Ctx.Request.Header.Get("x-forwarded-for")
for _, item := range strings.Split(ips, ", ") {
if net.ParseIP(item) != nil {
return item, nil
}
}
return "", newFailedApiResult(400, errCanNotFetchClientIP, fmt.Errorf("can not fetch client ip"))
}