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 tokens := []string{}
70 addf := func(format string, args ...any) {
71 s := fmt.Sprintf(format, args...)
72 tokens = append(tokens, s)
73 }
74 addf("%s=%s", m.Method, m.Result)
75 if m.Comment != "" && (m.Reason != "" || len(m.Props) > 0) {
76 addf("(%s)", m.Comment)
77 }
78 if m.Reason != "" {
79 addf("reason=%s", value(m.Reason))
80 }
81 for _, p := range m.Props {
82 v := p.Value
83 if !p.IsAddrLike {
84 v = value(v)
85 }
86 addf("%s.%s=%s%s", p.Type, p.Property, v, optComment(p.Comment))
87 }
88 for j, t := range tokens {
89 if j == len(tokens)-1 && i < len(h.Methods)-1 {
90 t += ";"
91 }
92 w.Add(" ", t)
93 }
94 }
95 return w.String()
96}
97
98func value(s string) string {
99 quote := s == ""
100 for _, c := range s {
101 // utf-8 does not have to be quoted. ../rfc/6532:242
102 if c == '"' || c == '\\' || c <= ' ' || c == 0x7f {
103 quote = true
104 break
105 }
106 }
107 if !quote {
108 return s
109 }
110 r := `"`
111 for _, c := range s {
112 if c == '"' || c == '\\' {
113 r += "\\"
114 }
115 r += string(c)
116 }
117 r += `"`
118 return r
119}
120