This repository has been archived on 2024-09-28. You can view files and clone it, but cannot push or open issues or pull requests.
nftables-http-api-go/utils.go
Georg Pfuetzenreuter b8ca3cec9f
Basics
Signed-off-by: Georg Pfuetzenreuter <mail@georg-pfuetzenreuter.net>
2024-08-30 05:13:05 +02:00

50 lines
1.5 KiB
Go

/*
* This file is part of nftables-http-api.
* Copyright (C) 2024 Georg Pfuetzenreuter <mail@georg-pfuetzenreuter.net>
*
* The nftables-http-api program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"encoding/json"
"log"
"net/http"
"golang.org/x/crypto/bcrypt"
)
type Response struct {
RError string `json:"error,omitempty"`
RResult string `json:"result,omitempty"`
}
func doReturn(w http.ResponseWriter, status int, text string) {
var response any
if status == http.StatusOK {
response = Response{RResult: text}
} else {
response = Response{RError: text}
}
j, err := json.Marshal(response)
if err != nil {
log.Fatalf("Failed to marshal JSON: %s", err)
}
w.WriteHeader(status)
w.Write(j)
}
func doCheckToken(token string, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(token))
if err == nil {
return true
} else {
log.Printf("Token check failed: %s", err)
return false
}
}