2024-04-03 19:53:50 -04:00
|
|
|
package keys
|
2024-03-28 12:57:35 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
2024-04-03 19:53:50 -04:00
|
|
|
"git.hatecomputers.club/hatecomputers/hatecomputers.club/api/types"
|
2024-03-28 12:57:35 -04:00
|
|
|
"git.hatecomputers.club/hatecomputers/hatecomputers.club/database"
|
|
|
|
"git.hatecomputers.club/hatecomputers/hatecomputers.club/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
const MAX_USER_API_KEYS = 5
|
|
|
|
|
2024-04-03 19:53:50 -04:00
|
|
|
func ListAPIKeysContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
|
|
|
|
return func(success types.Continuation, failure types.Continuation) types.ContinuationChain {
|
|
|
|
typesKeys, err := database.ListUserAPIKeys(context.DBConn, context.User.ID)
|
2024-03-28 12:57:35 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
resp.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return failure(context, req, resp)
|
|
|
|
}
|
|
|
|
|
2024-04-03 19:53:50 -04:00
|
|
|
(*context.TemplateData)["APIKeys"] = typesKeys
|
2024-03-28 12:57:35 -04:00
|
|
|
return success(context, req, resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-03 19:53:50 -04:00
|
|
|
func CreateAPIKeyContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
|
|
|
|
return func(success types.Continuation, failure types.Continuation) types.ContinuationChain {
|
|
|
|
formErrors := types.FormError{
|
2024-03-28 12:57:35 -04:00
|
|
|
Errors: []string{},
|
|
|
|
}
|
|
|
|
|
2024-03-29 18:35:04 -04:00
|
|
|
numKeys, err := database.CountUserAPIKeys(context.DBConn, context.User.ID)
|
2024-03-28 12:57:35 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
resp.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return failure(context, req, resp)
|
|
|
|
}
|
|
|
|
|
2024-03-29 18:35:04 -04:00
|
|
|
if numKeys >= MAX_USER_API_KEYS {
|
2024-04-03 19:53:50 -04:00
|
|
|
formErrors.Errors = append(formErrors.Errors, "max types keys reached")
|
2024-03-28 12:57:35 -04:00
|
|
|
}
|
|
|
|
|
2024-03-29 18:35:04 -04:00
|
|
|
if len(formErrors.Errors) > 0 {
|
|
|
|
(*context.TemplateData)["FormError"] = formErrors
|
|
|
|
return failure(context, req, resp)
|
|
|
|
}
|
|
|
|
|
2024-03-28 12:57:35 -04:00
|
|
|
_, 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-03 19:53:50 -04:00
|
|
|
func DeleteAPIKeyContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
|
|
|
|
return func(success types.Continuation, failure types.Continuation) types.ContinuationChain {
|
2024-03-28 12:57:35 -04:00
|
|
|
key := req.FormValue("key")
|
|
|
|
|
2024-04-03 19:53:50 -04:00
|
|
|
typesKey, err := database.GetAPIKey(context.DBConn, key)
|
2024-03-28 12:57:35 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
resp.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return failure(context, req, resp)
|
|
|
|
}
|
2024-04-03 19:53:50 -04:00
|
|
|
if (typesKey == nil) || (typesKey.UserID != context.User.ID) {
|
2024-03-28 12:57:35 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|