Implementing signed links for email message
This commit is contained in:
		
							
								
								
									
										44
									
								
								internal/urlsigner/signer.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								internal/urlsigner/signer.go
									
									
									
									
									
										Normal 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
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user