feat: verify allocated join authorisations with hmac

This commit is contained in:
Josh Creek
2026-09-01 08:48:48 +01:00
parent 66a1ee8007
commit e25d61d80e
7 changed files with 64 additions and 5 deletions
+14
View File
@@ -1,6 +1,8 @@
package domain
import (
"crypto/hmac"
"crypto/sha256"
"fmt"
"time"
)
@@ -29,6 +31,18 @@ func SignJoinAuthorisation(auth JoinAuthorisation, sign func([]byte) ([]byte, er
return SignedJoinAuthorisation{Authorisation: auth, Signature: append([]byte(nil), signature...)}, nil
}
// SignJoinAuthorisationHMAC is the interoperable production profile used by
// the Godot allocated server. The key is mounted out-of-band; the signed
// bytes remain the same canonical claim bytes used by the generic signer.
func SignJoinAuthorisationHMAC(auth JoinAuthorisation, key []byte) (SignedJoinAuthorisation, error) {
if len(key) == 0 {
return SignedJoinAuthorisation{}, ErrJoinAuthorisation
}
mac := hmac.New(sha256.New, key)
_, _ = mac.Write(JoinAuthorisationBytes(auth))
return SignedJoinAuthorisation{Authorisation: auth, Signature: mac.Sum(nil)}, nil
}
func (r *RankedConnections) AdmitSigned(signed SignedJoinAuthorisation, verify func([]byte, []byte) bool, now time.Time) (uint64, error) {
if len(signed.Signature) == 0 || verify == nil || !verify(JoinAuthorisationBytes(signed.Authorisation), signed.Signature) {
return 0, ErrJoinAuthorisation