50 lines
1.5 KiB
Go
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
|
|
}
|
|
}
|