Skip to content

Commit

Permalink
Add helper functions for checking/setting invite expiration (#3698)
Browse files Browse the repository at this point in the history
* Add helper functions for checking/setting invite expiration

Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com>

* Update internal/invite/invite.go

Co-authored-by: Eleftheria Stein-Kousathana <eleftheria@stacklok.com>

---------

Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com>
Co-authored-by: Eleftheria Stein-Kousathana <eleftheria@stacklok.com>
  • Loading branch information
rdimitrov and eleftherias authored Jun 24, 2024
1 parent 07fc788 commit 5f200ed
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 returns the expiration date of the invitation 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 5f200ed

Please sign in to comment.