-
Notifications
You must be signed in to change notification settings - Fork 66
/
http.go
170 lines (150 loc) · 4.95 KB
/
http.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
package util
import (
"net/http"
"regexp"
"strings"
"time"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"golang.org/x/xerrors"
logging "github.com/ipfs/go-log/v2"
)
var log = logging.Logger("util")
const (
ERR_INVALID_TOKEN = "ERR_INVALID_TOKEN"
ERR_TOKEN_EXPIRED = "ERR_TOKEN_EXPIRED"
ERR_AUTH_MISSING = "ERR_AUTH_MISSING"
ERR_WRONG_AUTH_FORMAT = "ERR_WRONG_AUTH_FORMAT"
ERR_INVALID_AUTH = "ERR_INVALID_AUTH"
ERR_AUTH_MISSING_BEARER = "ERR_AUTH_MISSING_BEARER"
ERR_NOT_AUTHORIZED = "ERR_NOT_AUTHORIZED"
ERR_MINER_NOT_OWNED = "ERR_MINER_NOT_OWNED"
ERR_INVALID_INVITE = "ERR_INVALID_INVITE"
ERR_USERNAME_TAKEN = "ERR_USERNAME_TAKEN"
ERR_USER_CREATION_FAILED = "ERR_USER_CREATION_FAILED"
ERR_USER_NOT_FOUND = "ERR_USER_NOT_FOUND"
ERR_INVALID_PASSWORD = "ERR_INVALID_PASSWORD"
ERR_INVITE_ALREADY_USED = "ERR_INVITE_ALREADY_USED"
ERR_CONTENT_ADDING_DISABLED = "ERR_CONTENT_ADDING_DISABLED"
ERR_INVALID_INPUT = "ERR_INVALID_INPUT"
ERR_CONTENT_SIZE_OVER_LIMIT = "ERR_CONTENT_SIZE_OVER_LIMIT"
ERR_PEERING_PEERS_ADD_ERROR = "ERR_PEERING_PEERS_ADD_ERROR"
ERR_PEERING_PEERS_REMOVE_ERROR = "ERR_PEERING_PEERS_REMOVE_ERROR"
ERR_PEERING_PEERS_START_ERROR = "ERR_PEERING_PEERS_START_ERROR"
ERR_PEERING_PEERS_STOP_ERROR = "ERR_PEERING_PEERS_STOP_ERROR"
ERR_CONTENT_NOT_FOUND = "ERR_CONTENT_NOT_FOUND"
ERR_INVALID_PINNING_STATUS = "ERR_INVALID_PINNING_STATUS"
)
type HttpError struct {
Code int `json:"code,omitempty"`
Reason string `json:"reason"`
Details string `json:"details"`
}
func (he HttpError) Error() string {
if he.Details == "" {
return he.Reason
}
return he.Reason + ": " + he.Details
}
type HttpErrorResponse struct {
Error HttpError `json:"error"`
}
const (
PermLevelUpload = 1
PermLevelUser = 2
PermLevelAdmin = 10
)
// isValidAuth checks if authStr is a valid
// returns false if authStr is not in a valid format
// returns true otherwise
func isValidAuth(authStr string) bool {
matchEst, _ := regexp.MatchString("^EST(.+)ARY$", authStr)
matchSecret, _ := regexp.MatchString("^SECRET(.+)SECRET$", authStr)
if !matchEst && !matchSecret {
return false
}
// only get the uuid from the string
uuidStr := strings.ReplaceAll(authStr, "SECRET", "")
uuidStr = strings.ReplaceAll(uuidStr, "EST", "")
uuidStr = strings.ReplaceAll(uuidStr, "ARY", "")
// check if uuid is valid
_, err := uuid.Parse(uuidStr)
if err != nil {
return false
}
return true
}
func ExtractAuth(c echo.Context) (string, error) {
auth := c.Request().Header.Get("Authorization")
// undefined will be the auth value if ESTUARY_TOKEN cookie is removed.
if auth == "" || auth == "undefined" {
return "", &HttpError{
Code: http.StatusUnauthorized,
Reason: ERR_AUTH_MISSING,
Details: "no api key was specified",
}
}
parts := strings.Split(auth, " ")
if len(parts) != 2 {
return "", &HttpError{
Code: http.StatusUnauthorized,
Reason: ERR_INVALID_AUTH,
Details: "invalid api key was specified",
}
}
if parts[0] != "Bearer" {
return "", &HttpError{
Code: http.StatusUnauthorized,
Reason: ERR_AUTH_MISSING_BEARER,
Details: "invalid api key was specified",
}
}
return parts[1], nil
}
type UserSettings struct {
Replication int `json:"replication"`
Verified bool `json:"verified"`
DealDuration int `json:"dealDuration"`
MaxStagingWait time.Duration `json:"maxStagingWait"`
FileStagingThreshold int64 `json:"fileStagingThreshold"`
ContentAddingDisabled bool `json:"contentAddingDisabled"`
DealMakingDisabled bool `json:"dealMakingDisabled"`
UploadEndpoints []string `json:"uploadEndpoints"`
Flags int `json:"flags"`
}
type ViewerResponse struct {
Username string `json:"username"`
Perms int `json:"perms"`
ID uint `json:"id"`
Address string `json:"address,omitempty"`
Miners []string `json:"miners,omitempty"`
AuthExpiry time.Time `json:"auth_expiry,omitempty"`
Settings UserSettings `json:"settings"`
}
func ErrorHandler(err error, ctx echo.Context) {
var httpRespErr *HttpError
if xerrors.As(err, &httpRespErr) {
log.Errorf("handler error: %s", err)
ctx.JSON(httpRespErr.Code, HttpErrorResponse{Error: *httpRespErr})
return
}
var echoErr *echo.HTTPError
if xerrors.As(err, &echoErr) {
ctx.JSON(echoErr.Code, HttpErrorResponse{
Error: HttpError{
Code: echoErr.Code,
Reason: http.StatusText(echoErr.Code),
Details: echoErr.Message.(string),
},
})
return
}
log.Errorf("handler error: %s", err)
ctx.JSON(http.StatusInternalServerError, HttpErrorResponse{
Error: HttpError{
Code: http.StatusInternalServerError,
Reason: http.StatusText(http.StatusInternalServerError),
Details: err.Error(),
},
})
}