|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "reflect" |
| 10 | + "strconv" |
| 11 | +) |
| 12 | + |
| 13 | +type APIErrorString string |
| 14 | + |
| 15 | +const ( |
| 16 | + APIErrorAuthenticationFailed APIErrorString = "AuthenticationFailed" |
| 17 | + APIErrorInvalidQuery APIErrorString = "InvalidQuery" |
| 18 | + APIErrorInvalidProfileID APIErrorString = "InvalidProfileID" |
| 19 | + APIErrorInvalidBanReason APIErrorString = "InvalidBanReason" |
| 20 | + APIErrorInvalidBanLength APIErrorString = "InvalidBanLength" |
| 21 | + APIErrorBanFailed APIErrorString = "BanFailed" |
| 22 | + APIErrorInvalidBanQuery APIErrorString = "InvalidBanQuery" |
| 23 | + APIErrorBanNotFound APIErrorString = "BanNotFound" |
| 24 | +) |
| 25 | + |
| 26 | +type APIError struct { |
| 27 | + Error string `json:"error"` |
| 28 | +} |
| 29 | + |
| 30 | +type Role string |
| 31 | + |
| 32 | +// Values currently just made up |
| 33 | +const ( |
| 34 | + RoleNone Role = "none" // Not signed in |
| 35 | + RoleUser Role = "user" |
| 36 | + RoleAdmin Role = "admin" |
| 37 | + RoleModerator Role = "moderator" |
| 38 | +) |
| 39 | + |
| 40 | +type AuthInfo struct { |
| 41 | + Secret string `json:"secret"` |
| 42 | +} |
| 43 | + |
| 44 | +var ( |
| 45 | + errOptionsRequest = errors.New("OPTIONS request") |
| 46 | + errIncorrectMethod = errors.New("incorrect HTTP method") |
| 47 | + errNoAuthInfo = errors.New("request struct does not contain AuthInfo fields") |
| 48 | + errAuthFailed = errors.New("authentication failed") |
| 49 | +) |
| 50 | + |
| 51 | +func parseGet(r *http.Request, w http.ResponseWriter, requiredRole Role) (query url.Values, err error) { |
| 52 | + w.Header().Set("Access-Control-Allow-Origin", "*") |
| 53 | + |
| 54 | + switch { |
| 55 | + case r.Method == http.MethodGet: |
| 56 | + break |
| 57 | + |
| 58 | + case r.Method == http.MethodOptions: |
| 59 | + w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS") |
| 60 | + w.Header().Set("Access-Control-Allow-Headers", "Content-Type") |
| 61 | + w.WriteHeader(http.StatusNoContent) |
| 62 | + return nil, errOptionsRequest |
| 63 | + |
| 64 | + default: |
| 65 | + w.Header().Set("Allow", "GET, OPTIONS") |
| 66 | + w.WriteHeader(http.StatusMethodNotAllowed) |
| 67 | + return nil, errIncorrectMethod |
| 68 | + } |
| 69 | + |
| 70 | + query, err = url.ParseQuery(r.URL.RawQuery) |
| 71 | + if err != nil { |
| 72 | + w.WriteHeader(http.StatusBadRequest) |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + |
| 76 | + if requiredRole == RoleNone { |
| 77 | + return query, nil |
| 78 | + } |
| 79 | + |
| 80 | + authInfo := makeAuthInfo(query) |
| 81 | + if !authenticate(authInfo, requiredRole) { |
| 82 | + replyError(w, http.StatusUnauthorized, APIErrorAuthenticationFailed) |
| 83 | + return nil, errAuthFailed |
| 84 | + } |
| 85 | + return query, nil |
| 86 | +} |
| 87 | + |
| 88 | +func parsePost(r *http.Request, w http.ResponseWriter, parsed any, requiredRole Role) error { |
| 89 | + w.Header().Set("Access-Control-Allow-Origin", "*") |
| 90 | + |
| 91 | + switch { |
| 92 | + case r.Method == http.MethodPost: |
| 93 | + break |
| 94 | + |
| 95 | + case r.Method == http.MethodOptions: |
| 96 | + w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS") |
| 97 | + w.Header().Set("Access-Control-Allow-Headers", "Content-Type") |
| 98 | + w.WriteHeader(http.StatusNoContent) |
| 99 | + return errOptionsRequest |
| 100 | + |
| 101 | + default: |
| 102 | + w.Header().Set("Allow", "POST, OPTIONS") |
| 103 | + w.WriteHeader(http.StatusMethodNotAllowed) |
| 104 | + return errIncorrectMethod |
| 105 | + } |
| 106 | + |
| 107 | + jsonData, err := io.ReadAll(r.Body) |
| 108 | + if err != nil { |
| 109 | + w.WriteHeader(http.StatusBadRequest) |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + err = json.Unmarshal(jsonData, parsed) |
| 114 | + if err != nil { |
| 115 | + w.WriteHeader(http.StatusBadRequest) |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + if requiredRole == RoleNone { |
| 120 | + return nil |
| 121 | + } |
| 122 | + |
| 123 | + authInfo, ok := reflect.ValueOf(parsed).Elem().FieldByName("AuthInfo").Interface().(AuthInfo) |
| 124 | + if !ok { |
| 125 | + w.WriteHeader(http.StatusInternalServerError) |
| 126 | + return errNoAuthInfo |
| 127 | + } |
| 128 | + if !authenticate(authInfo, requiredRole) { |
| 129 | + replyError(w, http.StatusUnauthorized, APIErrorAuthenticationFailed) |
| 130 | + return errAuthFailed |
| 131 | + } |
| 132 | + return nil |
| 133 | +} |
| 134 | + |
| 135 | +func makeAuthInfo(query url.Values) AuthInfo { |
| 136 | + return AuthInfo{ |
| 137 | + Secret: query.Get("secret"), |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func authenticate(authInfo AuthInfo, requiredRole Role) bool { |
| 142 | + return requiredRole == RoleNone || authInfo.Secret == apiSecret |
| 143 | +} |
| 144 | + |
| 145 | +func replyError(w http.ResponseWriter, statusCode int, errMsg APIErrorString) { |
| 146 | + w.Header().Set("Content-Type", "application/json") |
| 147 | + w.WriteHeader(statusCode) |
| 148 | + |
| 149 | + jsonData := []byte(`{"error":"` + string(errMsg) + `"}`) |
| 150 | + w.Header().Set("Content-Length", strconv.Itoa(len(jsonData))) |
| 151 | + _, _ = w.Write(jsonData) |
| 152 | +} |
| 153 | + |
| 154 | +func replyOK(w http.ResponseWriter, data any) { |
| 155 | + if data == nil { |
| 156 | + w.WriteHeader(http.StatusNoContent) |
| 157 | + return |
| 158 | + } |
| 159 | + |
| 160 | + w.Header().Set("Content-Type", "application/json") |
| 161 | + jsonData, err := json.Marshal(data) |
| 162 | + if err != nil { |
| 163 | + w.WriteHeader(http.StatusInternalServerError) |
| 164 | + return |
| 165 | + } |
| 166 | + w.Header().Set("Content-Length", strconv.Itoa(len(jsonData))) |
| 167 | + _, _ = w.Write(jsonData) |
| 168 | +} |
0 commit comments