a3e044dec5
Improved some strings, added the basics to prepare for integrating schedules again, and added basic Forgejo CI.
74 lines
2 KiB
Go
Executable file
74 lines
2 KiB
Go
Executable file
/*
|
|
ScheduleTogether Backend
|
|
Copyright (C) 2024, Zervó Zadachin
|
|
|
|
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License version 3
|
|
as published by the Free Software Foundation.
|
|
|
|
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 Affero General Public License version 3 for more details.
|
|
|
|
This program incorporates external libraries for certain functionalities.
|
|
These libraries are covered by their respective licenses, and their usage
|
|
agreements are as outlined in their respective documentation or source
|
|
code.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package cryptography
|
|
|
|
import (
|
|
"crypto/rand"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"git.zervo.org/scheduletogether/backend/pkg/helpers/config"
|
|
"git.zervo.org/scheduletogether/backend/pkg/helpers/logging"
|
|
)
|
|
|
|
var logboi = logging.NewLogger("crypto")
|
|
var tokenComponents = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
|
|
|
// HashString hashes a string
|
|
func HashString(raw string) (string, error) {
|
|
hashed, err := bcrypt.GenerateFromPassword([]byte(raw), bcrypt.DefaultCost) // TODO: replace with safer seed?
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(hashed), nil
|
|
}
|
|
|
|
// GenerateToken generates a random token of the specified length.
|
|
func GenerateToken(length int) (string, error) {
|
|
bytes := make([]byte, length)
|
|
_, err := rand.Read(bytes)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
for k, v := range bytes {
|
|
bytes[k] = tokenComponents[v%byte(len(tokenComponents))]
|
|
}
|
|
|
|
return string(bytes), nil
|
|
}
|
|
|
|
func GetJwtSecret() string {
|
|
// Load configuration
|
|
cfg, err := config.LoadConfig()
|
|
if err != nil {
|
|
logboi.Fatal(err.Error())
|
|
}
|
|
|
|
// Get JWT Secret
|
|
secret := cfg.JWTSecret
|
|
|
|
return secret
|
|
}
|