2023-01-30 16:27:06 +03:00
|
|
|
package subjectpass
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-08-15 09:25:56 +03:00
|
|
|
"github.com/mjl-/mox/mlog"
|
2023-01-30 16:27:06 +03:00
|
|
|
"github.com/mjl-/mox/smtp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSubjectPass(t *testing.T) {
|
2023-12-05 15:35:58 +03:00
|
|
|
log := mlog.New("subjectpass", nil)
|
|
|
|
|
2023-01-30 16:27:06 +03:00
|
|
|
key := []byte("secret token")
|
|
|
|
addr, _ := smtp.ParseAddress("mox@mox.example")
|
2023-12-05 15:35:58 +03:00
|
|
|
sig := Generate(log.Logger, addr, key, time.Now())
|
2023-01-30 16:27:06 +03:00
|
|
|
|
|
|
|
message := fmt.Sprintf("From: <mox@mox.example>\r\nSubject: let me in %s\r\n\r\nthe message", sig)
|
2023-12-05 15:35:58 +03:00
|
|
|
if err := Verify(log.Logger, strings.NewReader(message), key, time.Hour); err != nil {
|
2023-01-30 16:27:06 +03:00
|
|
|
t.Fatalf("verifyPassToken: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-12-05 15:35:58 +03:00
|
|
|
if err := Verify(log.Logger, strings.NewReader(message), []byte("bad key"), time.Hour); err == nil {
|
2023-01-30 16:27:06 +03:00
|
|
|
t.Fatalf("verifyPassToken did not fail")
|
|
|
|
}
|
|
|
|
|
2023-12-05 15:35:58 +03:00
|
|
|
sig = Generate(log.Logger, addr, key, time.Now().Add(-time.Hour-257))
|
2023-01-30 16:27:06 +03:00
|
|
|
message = fmt.Sprintf("From: <mox@mox.example>\r\nSubject: let me in %s\r\n\r\nthe message", sig)
|
2023-12-05 15:35:58 +03:00
|
|
|
if err := Verify(log.Logger, strings.NewReader(message), key, time.Hour); !errors.Is(err, ErrExpired) {
|
2023-01-30 16:27:06 +03:00
|
|
|
t.Fatalf("verifyPassToken should have expired")
|
|
|
|
}
|
|
|
|
}
|