diff options
| author | Elizabeth Hunt <elizabeth@simponic.xyz> | 2024-04-03 17:53:50 -0600 |
|---|---|---|
| committer | Elizabeth Hunt <elizabeth@simponic.xyz> | 2024-04-03 17:53:50 -0600 |
| commit | f38e8719c2a8537fe9b64ed8ceca45858a58e498 (patch) | |
| tree | 5cf2c7c7f6396f75bdb841db00638e4eef8e81e8 /api/api_keys.go | |
| parent | e398cf05402c010d594cea4e2dea307ca1a36dbe (diff) | |
| download | hatecomputers.club-f38e8719c2a8537fe9b64ed8ceca45858a58e498.tar.gz hatecomputers.club-f38e8719c2a8537fe9b64ed8ceca45858a58e498.zip | |
make it compile
Diffstat (limited to 'api/api_keys.go')
| -rw-r--r-- | api/api_keys.go | 87 |
1 files changed, 0 insertions, 87 deletions
diff --git a/api/api_keys.go b/api/api_keys.go deleted file mode 100644 index d636044..0000000 --- a/api/api_keys.go +++ /dev/null @@ -1,87 +0,0 @@ -package api - -import ( - "log" - "net/http" - - "git.hatecomputers.club/hatecomputers/hatecomputers.club/database" - "git.hatecomputers.club/hatecomputers/hatecomputers.club/utils" -) - -const MAX_USER_API_KEYS = 5 - -func ListAPIKeysContinuation(context *RequestContext, req *http.Request, resp http.ResponseWriter) ContinuationChain { - return func(success Continuation, failure Continuation) ContinuationChain { - apiKeys, err := database.ListUserAPIKeys(context.DBConn, context.User.ID) - if err != nil { - log.Println(err) - resp.WriteHeader(http.StatusInternalServerError) - return failure(context, req, resp) - } - - (*context.TemplateData)["APIKeys"] = apiKeys - return success(context, req, resp) - } -} - -func CreateAPIKeyContinuation(context *RequestContext, req *http.Request, resp http.ResponseWriter) ContinuationChain { - return func(success Continuation, failure Continuation) ContinuationChain { - formErrors := FormError{ - Errors: []string{}, - } - - numKeys, err := database.CountUserAPIKeys(context.DBConn, context.User.ID) - if err != nil { - log.Println(err) - resp.WriteHeader(http.StatusInternalServerError) - return failure(context, req, resp) - } - - if numKeys >= MAX_USER_API_KEYS { - formErrors.Errors = append(formErrors.Errors, "max api keys reached") - } - - if len(formErrors.Errors) > 0 { - (*context.TemplateData)["FormError"] = formErrors - return failure(context, req, resp) - } - - _, err = database.SaveAPIKey(context.DBConn, &database.UserApiKey{ - UserID: context.User.ID, - Key: utils.RandomId(), - }) - if err != nil { - log.Println(err) - resp.WriteHeader(http.StatusInternalServerError) - return failure(context, req, resp) - } - return success(context, req, resp) - } -} - -func DeleteAPIKeyContinuation(context *RequestContext, req *http.Request, resp http.ResponseWriter) ContinuationChain { - return func(success Continuation, failure Continuation) ContinuationChain { - key := req.FormValue("key") - - apiKey, err := database.GetAPIKey(context.DBConn, key) - if err != nil { - log.Println(err) - resp.WriteHeader(http.StatusInternalServerError) - return failure(context, req, resp) - } - if (apiKey == nil) || (apiKey.UserID != context.User.ID) { - resp.WriteHeader(http.StatusUnauthorized) - return failure(context, req, resp) - } - - err = database.DeleteAPIKey(context.DBConn, key) - if err != nil { - log.Println(err) - resp.WriteHeader(http.StatusInternalServerError) - return failure(context, req, resp) - } - - http.Redirect(resp, req, "/keys", http.StatusFound) - return success(context, req, resp) - } -} |
