1package message
2
3import (
4 "fmt"
5)
6
7// ../rfc/8601:577
8
9// Authentication-Results header, see RFC 8601.
10type AuthResults struct {
11 Hostname string
12 Comment string // If not empty, header comment without "()", added after Hostname.
13 Methods []AuthMethod
14}
15
16// ../rfc/8601:598
17
18// AuthMethod is a result for one authentication method.
19//
20// Example encoding in the header: "spf=pass smtp.mailfrom=example.net".
21type AuthMethod struct {
22 // E.g. "dkim", "spf", "iprev", "auth".
23 Method string
24 Result string // Each method has a set of known values, e.g. "pass", "temperror", etc.
25 Comment string // Optional, message header comment.
26 Reason string // Optional.
27 Props []AuthProp
28}
29
30// ../rfc/8601:606
31
32// AuthProp describes properties for an authentication method.
33// Each method has a set of known properties.
34// Encoded in the header as "type.property=value", e.g. "smtp.mailfrom=example.net"
35// for spf.
36type AuthProp struct {
37 // Valid values maintained at https://www.iana.org/assignments/email-auth/email-auth.xhtml
38 Type string
39 Property string
40 Value string
41 // Whether value is address-like (localpart@domain, or domain). Or another value,
42 // which is subject to escaping.
43 IsAddrLike bool
44 Comment string // If not empty, header comment without "()", added after Value.
45}
46
47// MakeAuthProp is a convenient way to make an AuthProp.
48func MakeAuthProp(typ, property, value string, isAddrLike bool, Comment string) AuthProp {
49 return AuthProp{typ, property, value, isAddrLike, Comment}
50}
51
52// todo future: we could store fields as dns.Domain, and when we encode as non-ascii also add the ascii version as a comment.
53
54// Header returns an Authentication-Results header, possibly spanning multiple
55// lines, always ending in crlf.
56func (h AuthResults) Header() string {
57 // Escaping of values: ../rfc/8601:684 ../rfc/2045:661
58
59 optComment := func(s string) string {
60 if s != "" {
61 return " (" + s + ")"
62 }
63 return s
64 }
65
66 w := &HeaderWriter{}
67 w.Add("", "Authentication-Results:"+optComment(h.Comment)+" "+value(h.Hostname)+";")
68 for i, m := range h.Methods {
69 w.Newline()
70
71 tokens := []string{}
72 addf := func(format string, args ...any) {
73 s := fmt.Sprintf(format, args...)
74 tokens = append(tokens, s)
75 }
76 addf("%s=%s", m.Method, m.Result)
77 if m.Comment != "" && (m.Reason != "" || len(m.Props) > 0) {
78 addf("(%s)", m.Comment)
79 }
80 if m.Reason != "" {
81 addf("reason=%s", value(m.Reason))
82 }
83 for _, p := range m.Props {
84 v := p.Value
85 if !p.IsAddrLike {
86 v = value(v)
87 }
88 addf("%s.%s=%s%s", p.Type, p.Property, v, optComment(p.Comment))
89 }
90 for j, t := range tokens {
91 var sep string
92 if j > 0 {
93 sep = " "
94 }
95 if j == len(tokens)-1 && i < len(h.Methods)-1 {
96 t += ";"
97 }
98 w.Add(sep, t)
99 }
100 }
101 return w.String()
102}
103
104func value(s string) string {
105 quote := s == ""
106 for _, c := range s {
107 // utf-8 does not have to be quoted. ../rfc/6532:242
108 if c == '"' || c == '\\' || c <= ' ' || c == 0x7f {
109 quote = true
110 break
111 }
112 }
113 if !quote {
114 return s
115 }
116 r := `"`
117 for _, c := range s {
118 if c == '"' || c == '\\' {
119 r += "\\"
120 }
121 r += string(c)
122 }
123 r += `"`
124 return r
125}
126