credentials.go (684B)
1 package main 2 3 import ( 4 "crypto/sha512" 5 "encoding/json" 6 "net/http" 7 8 "github.com/gboncoffee/siga/auth/repository" 9 ) 10 11 type CredentialsRequest struct { 12 Username string 13 Password string 14 } 15 16 func credentials(w http.ResponseWriter, r *http.Request) { 17 var req CredentialsRequest 18 if err := json.NewDecoder(r.Body).Decode(&req); err != nil { 19 http.Error(w, "invalid JSON", http.StatusBadRequest) 20 return 21 } 22 23 c := repository.Credential{ 24 Username: req.Username, 25 Password: sha512.Sum512([]byte(req.Password)), 26 } 27 if err := repository.NewCredential(c); err != nil { 28 http.Error(w, "internal server error", http.StatusInternalServerError) 29 return 30 } 31 32 w.WriteHeader(http.StatusOK) 33 }