summaryrefslogtreecommitdiff
path: root/api/api_keys.go
diff options
context:
space:
mode:
authorsimponic <simponic@hatecomputers.club>2024-04-06 15:43:18 -0400
committersimponic <simponic@hatecomputers.club>2024-04-06 15:43:18 -0400
commit83cc6267fd5ce2f61200314424c5f400f65ff2ba (patch)
treeeafb35310236a15572cbb6e16ff8d6f181bfe240 /api/api_keys.go
parent569d2788ebfb90774faf361f62bfe7968e091465 (diff)
parentcad8e2c4ed5e3bab61ff243f8677f8a46eaeafb0 (diff)
downloadhatecomputers.club-83cc6267fd5ce2f61200314424c5f400f65ff2ba.tar.gz
hatecomputers.club-83cc6267fd5ce2f61200314424c5f400f65ff2ba.zip
Merge pull request 'testing | dont be recursive for external domains | finalize oauth' (#5) from dont-be-authoritative into main
Reviewed-on: https://git.hatecomputers.club/hatecomputers/hatecomputers.club/pulls/5
Diffstat (limited to 'api/api_keys.go')
-rw-r--r--api/api_keys.go87
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)
- }
-}