feat: import Sea vHost single-server testing release
This commit is contained in:
commit
248badd33e
441 files changed
+63221
No files matched your search
@@ -0,0 +1,56 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func VerifyPassword(hash, password string) bool {
|
||||
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) == nil
|
||||
}
|
||||
|
||||
var dummyPasswordHash = func() string {
|
||||
hash, _ := bcrypt.GenerateFromPassword([]byte("sea-vhost-dummy-credential"), bcrypt.DefaultCost)
|
||||
return string(hash)
|
||||
}()
|
||||
|
||||
func VerifyPasswordConstantWork(hash, password string) bool {
|
||||
if hash == "" {
|
||||
_ = bcrypt.CompareHashAndPassword([]byte(dummyPasswordHash), []byte(password))
|
||||
return false
|
||||
}
|
||||
return VerifyPassword(hash, password)
|
||||
}
|
||||
|
||||
func HashPassword(password string) (string, error) {
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
return string(hash), err
|
||||
}
|
||||
|
||||
func NewSessionToken(secret string) (string, error) {
|
||||
random := make([]byte, 32)
|
||||
if _, err := rand.Read(random); err != nil {
|
||||
return "", err
|
||||
}
|
||||
mac := hmac.New(sha256.New, []byte(secret))
|
||||
_, _ = mac.Write(random)
|
||||
payload := append(random, mac.Sum(nil)...)
|
||||
return base64.RawURLEncoding.EncodeToString(payload), nil
|
||||
}
|
||||
|
||||
func ValidateSessionToken(secret, token string) bool {
|
||||
payload, err := base64.RawURLEncoding.DecodeString(token)
|
||||
if err != nil || len(payload) != 64 {
|
||||
return false
|
||||
}
|
||||
mac := hmac.New(sha256.New, []byte(secret))
|
||||
_, _ = mac.Write(payload[:32])
|
||||
return hmac.Equal(payload[32:], mac.Sum(nil))
|
||||
}
|
||||
|
||||
var ErrInvalidCredentials = errors.New("invalid credentials")
|
||||
@@ -0,0 +1,86 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/rand"
|
||||
"crypto/sha1"
|
||||
"crypto/sha256"
|
||||
"encoding/base32"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
totpPeriod = int64(30)
|
||||
totpDigits = 6
|
||||
)
|
||||
|
||||
func NewTOTPSecret() (string, error) {
|
||||
random := make([]byte, 20)
|
||||
if _, err := rand.Read(random); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(random), nil
|
||||
}
|
||||
|
||||
func TOTPURI(secret, account string) string {
|
||||
label := url.PathEscape("Sea vHost:" + account)
|
||||
query := url.Values{"secret": {secret}, "issuer": {"Sea vHost"}, "algorithm": {"SHA1"}, "digits": {strconv.Itoa(totpDigits)}, "period": {strconv.FormatInt(totpPeriod, 10)}}
|
||||
return "otpauth://totp/" + label + "?" + query.Encode()
|
||||
}
|
||||
|
||||
func VerifyTOTP(secret, code string, now time.Time) bool {
|
||||
code = strings.TrimSpace(code)
|
||||
if len(code) != totpDigits {
|
||||
return false
|
||||
}
|
||||
for _, character := range code {
|
||||
if character < '0' || character > '9' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
for offset := int64(-1); offset <= 1; offset++ {
|
||||
if hmac.Equal([]byte(code), []byte(totpAt(secret, now.Unix()/totpPeriod+offset))) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func totpAt(secret string, counter int64) string {
|
||||
key, err := base32.StdEncoding.WithPadding(base32.NoPadding).DecodeString(strings.ToUpper(secret))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
message := make([]byte, 8)
|
||||
binary.BigEndian.PutUint64(message, uint64(counter))
|
||||
mac := hmac.New(sha1.New, key)
|
||||
_, _ = mac.Write(message)
|
||||
digest := mac.Sum(nil)
|
||||
offset := digest[len(digest)-1] & 0x0f
|
||||
value := binary.BigEndian.Uint32(digest[offset:offset+4]) & 0x7fffffff
|
||||
return fmt.Sprintf("%0*d", totpDigits, value%1000000)
|
||||
}
|
||||
|
||||
func NewRecoveryCodes(count int) ([]string, error) {
|
||||
codes := make([]string, 0, count)
|
||||
for len(codes) < count {
|
||||
random := make([]byte, 8)
|
||||
if _, err := rand.Read(random); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
encoded := strings.ToLower(base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(random))
|
||||
codes = append(codes, encoded[:4]+"-"+encoded[4:8]+"-"+encoded[8:12])
|
||||
}
|
||||
return codes, nil
|
||||
}
|
||||
|
||||
func RecoveryCodeHash(code string) []byte {
|
||||
normalized := strings.ToLower(strings.ReplaceAll(strings.TrimSpace(code), "-", ""))
|
||||
hash := sha256.Sum256([]byte("sea-vhost/mfa-recovery/v1\x00" + normalized))
|
||||
return hash[:]
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestTOTPVerificationWindow(t *testing.T) {
|
||||
secret := "JBSWY3DPEHPK3PXP"
|
||||
now := time.Unix(1_700_000_000, 0)
|
||||
code := totpAt(secret, now.Unix()/totpPeriod)
|
||||
if !VerifyTOTP(secret, code, now) {
|
||||
t.Fatal("current TOTP code was rejected")
|
||||
}
|
||||
if VerifyTOTP(secret, "0000000", now) || VerifyTOTP(secret, "abcdef", now) {
|
||||
t.Fatal("malformed TOTP code was accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecoveryCodeHashNormalizesFormatting(t *testing.T) {
|
||||
if !bytes.Equal(RecoveryCodeHash("abcd-efgh-ijkl"), RecoveryCodeHash(" ABCDEFGHIJKL ")) {
|
||||
t.Fatal("equivalent recovery codes produced different hashes")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user