Implementing signed links for email message

This commit is contained in:
2024-08-21 11:34:25 +02:00
parent 2642f706bf
commit 7a7a823715
4 changed files with 54 additions and 0 deletions

View File

@ -0,0 +1,44 @@
package urlsigner
import (
"fmt"
"strings"
"time"
goalone "github.com/bwmarrin/go-alone"
)
type Signer struct {
Secret []byte
}
func (s *Signer) GenerateTokenFromString(data string) string {
var urlToSign string
crypt := goalone.New(s.Secret, goalone.Timestamp)
if strings.Contains(data, "?") {
urlToSign = fmt.Sprintf("%s&hash=", data)
} else {
urlToSign = fmt.Sprintf("%s?hash=", data)
}
tokenBytes := crypt.Sign([]byte(urlToSign))
token := string(tokenBytes)
return token
}
func (s *Signer) VerifyToken(token string) bool {
crypt := goalone.New(s.Secret, goalone.Timestamp)
_, err := crypt.Unsign([]byte(token))
if err != nil {
fmt.Println(err)
}
return true
}
func (s *Signer) Expired(token string, minutesUntilExpire int) bool {
crypt := goalone.New(s.Secret, goalone.Timestamp)
ts := crypt.Parse([]byte(token))
return time.Since(ts.Timestamp) > time.Duration(minutesUntilExpire)*time.Minute
}