Brijesh's Git Server — watchman @ 070e9230ef8032862eb09dd029f6d4f39705b547

observability tool, needs to be rewritten once identity is stable

internal/projectKeys.go (view raw)

 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
package internal

import (
	"database/sql"
	"encoding/json"
	"math/rand"
	"net/http"
	"watchman/schema"
	"watchman/utils"
)

func GenerateKeys() (string, string) {
	numbers := "1234567890"
	smallAlphabets := "abcdefghijklmnopqrstuvwxyz"
	capitalAlphabets := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

	accessKeyChars := capitalAlphabets + numbers
	secretKeyChars := capitalAlphabets + smallAlphabets + numbers

	accessKeyCharsLen := len(accessKeyChars)
	secretKeyCharsLen := len(secretKeyChars)

	accessKey := make([]byte, 16)
	secretKey := make([]byte, 32)

	for i := range accessKey {
		accessKey[i] = accessKeyChars[rand.Intn(accessKeyCharsLen)]
	}

	for i := range secretKey {
		secretKey[i] = secretKeyChars[rand.Intn(secretKeyCharsLen)]
	}

	return string(accessKey), string(secretKey)
}

func CreateProjectKey(w http.ResponseWriter, r *http.Request, db *sql.DB, ProjectID string) {
	AccessKey, SecretKey := GenerateKeys()

	var ProjectKey schema.ProjectKey
	decoder := json.NewDecoder(r.Body)
	err := decoder.Decode(&ProjectKey)
	utils.HandleError(w, r, http.StatusBadRequest, "Error decoding JSON: ", err)

	stmt, err := db.Prepare("INSERT INTO ProjectKeys (ProjectID, AccessKey, SecretKey, Expiry) VALUES (?, ?, ?, ?)")
	utils.HandleError(w, r, http.StatusInternalServerError, "Error preparing statement: ", err)
	defer stmt.Close()

	_, err = stmt.Exec(ProjectID, AccessKey, SecretKey, ProjectKey.Expiry)
	utils.HandleError(w, r, http.StatusInternalServerError, "Error executing statement: ", err)

	response := schema.ResponseType{
		Status:    "OK",
		Message:   "Project key created successfully",
		RequestID: r.Context().Value(schema.RequestIDKey{}).(string),
	}
	utils.SendResponse(w, r, response)
}

func DeleteProjectKey(w http.ResponseWriter, r *http.Request, db *sql.DB) {
	var ProjectKey schema.ProjectKey
	decoder := json.NewDecoder(r.Body)
	err := decoder.Decode(&ProjectKey)
	utils.HandleError(w, r, http.StatusBadRequest, "Error decoding JSON: ", err)

	stmt, err := db.Prepare("DELETE FROM ProjectKeys WHERE AccessKey = ? AND SecretKey = ?")
	utils.HandleError(w, r, http.StatusInternalServerError, "Error preparing statement: ", err)
	defer stmt.Close()

	_, err = stmt.Exec(ProjectKey.AccessKey, ProjectKey.SecretKey)
	utils.HandleError(w, r, http.StatusInternalServerError, "Error executing statement: ", err)

	response := schema.ResponseType{
		Status:    "OK",
		Message:   "Project key deleted successfully",
		RequestID: r.Context().Value(schema.RequestIDKey{}).(string),
	}
	utils.SendResponse(w, r, response)
}