diff --git a/internal/invite/invite.go b/internal/invite/invite.go index 777570a6c0..9faf3cf092 100644 --- a/internal/invite/invite.go +++ b/internal/invite/invite.go @@ -19,19 +19,34 @@ package invite import ( "crypto/rand" "encoding/base64" + "time" + + "google.golang.org/protobuf/types/known/timestamppb" ) const ( - // CodeByteLength = 48 is the length of the invite code in bytes - CodeByteLength = 48 + // codeByteLength = 48 is the length of the invite code in bytes + codeByteLength = 48 + // expireIn7Days is the duration for which an invitation is valid - 7 days + expireIn7Days = 7 * 24 * time.Hour ) // GenerateCode generates a random invite code func GenerateCode() string { - bytes := make([]byte, CodeByteLength) + bytes := make([]byte, codeByteLength) // Generate random bytes if _, err := rand.Read(bytes); err != nil { return "" } return base64.URLEncoding.EncodeToString(bytes) } + +// GetExpireIn7Days sets the expiration date of the invitation to 7 days from t.Now() +func GetExpireIn7Days(t time.Time) *timestamppb.Timestamp { + return timestamppb.New(t.Add(expireIn7Days)) +} + +// IsExpired checks if the invitation has expired +func IsExpired(t time.Time) bool { + return time.Now().After(t.Add(expireIn7Days)) +}