This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
/
handle_user_report.go
139 lines (119 loc) · 4.66 KB
/
handle_user_report.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
// Copyright 2021 the Exposure Notifications Verification Server authors
//
// 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 issueapi
import (
"errors"
"net/http"
"time"
"github.com/google/exposure-notifications-verification-server/pkg/api"
"github.com/google/exposure-notifications-verification-server/pkg/controller"
"github.com/google/exposure-notifications-verification-server/pkg/database"
"github.com/google/exposure-notifications-server/pkg/base64util"
"github.com/google/exposure-notifications-server/pkg/logging"
enobs "github.com/google/exposure-notifications-server/pkg/observability"
)
func (c *Controller) HandleUserReport() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if c.config.IsMaintenanceMode() {
c.h.RenderJSON(w, http.StatusTooManyRequests,
api.Errorf("server is read-only for maintenance").WithCode(api.ErrMaintenanceMode))
return
}
ctx := r.Context()
logger := logging.FromContext(ctx).Named("userreportapi.HandleUserReport")
blame := enobs.BlameNone
result := enobs.ResultOK
defer enobs.RecordLatency(ctx, time.Now(), mLatencyMs, &result, &blame)
authApp := controller.AuthorizedAppFromContext(ctx)
if authApp == nil {
blame = enobs.BlameClient
result = enobs.ResultError("MISSING_AUTHORIZED_APP")
controller.MissingAuthorizedApp(w, r, c.h)
return
}
// Parse the UserReportRequest struct.
var request api.UserReportRequest
if err := controller.BindJSON(w, r, &request); err != nil {
logger.Errorw("bad request", "error", err)
blame = enobs.BlameClient
result = enobs.ResultError("FAILED_TO_PARSE_JSON_REQUEST")
c.h.RenderJSON(w, http.StatusBadRequest, api.Error(err).WithCode(api.ErrUnparsableRequest))
return
}
// Ensure realm allows user report.
realm := controller.RealmFromContext(ctx)
if !realm.AllowsUserReport() {
logger.Warnw("realm is requesting user report, but disabled", "realmID", realm.ID)
blame = enobs.BlameClient
result = enobs.ResultError("USER_REPORT_NOT_ENABLED")
c.h.RenderJSON(w, http.StatusBadRequest, api.Errorf("user initiated report is not enabled").WithCode(api.ErrUnsupportedTestType))
return
}
nonce, err := base64util.DecodeString(request.Nonce)
if err != nil {
logger.Errorw("bad request", "error", err)
blame = enobs.BlameClient
result = enobs.ResultError("FAILED_TO_PARSE_JSON_REQUEST")
c.h.RenderJSON(w, http.StatusBadRequest, api.Error(err).WithCode(api.ErrUnparsableRequest))
return
}
if len(nonce) == 0 {
logger.Errorw("bad request", "error", err)
blame = enobs.BlameClient
result = enobs.ResultError("USER_REQUEST_MISSING_NONCE")
c.h.RenderJSON(w, http.StatusBadRequest, api.Errorf("nonce cannot be empty").WithCode(api.ErrMissingNonce))
return
}
if len(request.Phone) == 0 {
logger.Errorw("bad request", "error", err)
blame = enobs.BlameClient
result = enobs.ResultError("USER_REQUEST_MISSING_PHONE")
c.h.RenderJSON(w, http.StatusBadRequest, api.Errorf("phone cannot be empty").WithCode(api.ErrMissingPhone))
return
}
// Issue code and send text.
issueRequest := &IssueRequestInternal{
IssueRequest: &api.IssueCodeRequest{
SymptomDate: request.SymptomDate,
TestDate: request.TestDate,
TestType: api.TestTypeUserReport, // Always test type of user report.
Phone: request.Phone,
SMSTemplateLabel: database.UserReportTemplateLabel,
},
UserRequested: true,
Nonce: nonce,
}
res := c.IssueOne(ctx, issueRequest)
switch res.HTTPCode {
case http.StatusInternalServerError:
controller.InternalError(w, r, c.h, errors.New(res.ErrorReturn.Error))
return
case http.StatusConflict:
c.h.RenderJSON(w, http.StatusOK, &api.UserReportResponse{
ExpiresAt: res.IssueCodeResponse().ExpiresAt,
ExpiresAtTimestamp: res.IssueCodeResponse().ExpiresAtTimestamp,
})
return
case http.StatusOK:
c.h.RenderJSON(w, http.StatusOK, &api.UserReportResponse{
ExpiresAt: res.IssueCodeResponse().ExpiresAt,
ExpiresAtTimestamp: res.IssueCodeResponse().ExpiresAtTimestamp,
})
return
default:
c.h.RenderJSON(w, res.HTTPCode, res.ErrorReturn)
return
}
})
}