mirror of
https://github.com/cert-manager/webhook-example.git
synced 2026-03-16 18:02:51 +01:00
add auth package to hash requests
This commit is contained in:
parent
e57412778d
commit
b1104cc004
1 changed files with 57 additions and 0 deletions
57
auth/auth.go
Normal file
57
auth/auth.go
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
saltChars = "0123456789ABCDEFGHIJKLMNOPQRTSUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||
baseUrl = "https://api.nearlyfreespeech.net"
|
||||
authHeaderName = "X-NFSN-Authentication"
|
||||
maxSalt big.Int
|
||||
)
|
||||
|
||||
func init() {
|
||||
maxSalt.SetInt64(int64(len(saltChars)))
|
||||
}
|
||||
|
||||
func Salt() (string, error) {
|
||||
salt := make([]string, 16)
|
||||
for i := 0; i < 16; i++ {
|
||||
rand_index, err := rand.Int(rand.Reader, &maxSalt)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
salt[i] = string(saltChars[rand_index.Int64()])
|
||||
}
|
||||
|
||||
return strings.Join(salt, ""), nil
|
||||
}
|
||||
|
||||
func Timestamp() string {
|
||||
return strconv.FormatInt(time.Now().Unix(), 10)
|
||||
}
|
||||
|
||||
func ComputeHash(data string) string {
|
||||
result := sha1.Sum([]byte(data))
|
||||
return hex.EncodeToString(result[:])
|
||||
}
|
||||
|
||||
func GetAuthHeader(login string, apiKey string, url string, body string) (string, error) {
|
||||
timestamp := Timestamp()
|
||||
salt, err := Salt()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
bodyHash := ComputeHash(body)
|
||||
hash := ComputeHash(fmt.Sprintf("%s;%s;%s;%s;%s;%s", login, timestamp, salt, apiKey, url, bodyHash))
|
||||
return hash, nil
|
||||
}
|
||||
Loading…
Reference in a new issue