Skip to content

Commit

Permalink
Add helper functions for checking/setting invite expiration
Browse files Browse the repository at this point in the history
Signed-off-by: Radoslav Dimitrov <[email protected]>
  • Loading branch information
rdimitrov committed Jun 24, 2024
1 parent 07fc788 commit 8b4a693
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions internal/invite/invite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit 8b4a693

Please sign in to comment.