Skip to content
This repository was archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
Add QueryMethod type
Browse files Browse the repository at this point in the history
  • Loading branch information
emersion committed Mar 11, 2019
1 parent 1f1b0f7 commit 054bae4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
12 changes: 10 additions & 2 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,18 @@ type queryResult struct {
Flags []string
}

// QueryMethod is a DKIM query method.
type QueryMethod string

const (
// DNS TXT resource record (RR) lookup algorithm
QueryMethodDNSTXT QueryMethod = "dns/txt"
)

type queryFunc func(domain, selector string) (*queryResult, error)

var queryMethods = map[string]queryFunc{
"dns/txt": queryDNSTXT,
var queryMethods = map[QueryMethod]queryFunc{
QueryMethodDNSTXT: queryDNSTXT,
}

func queryDNSTXT(domain, selector string) (*queryResult, error) {
Expand Down
8 changes: 6 additions & 2 deletions sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type SignOptions struct {
Expiration time.Time

// A list of query methods used to retrieve the public key.
QueryMethods []string
QueryMethods []QueryMethod
}

// Sign signs a message. It reads it from r and writes the signed version to w.
Expand Down Expand Up @@ -167,7 +167,11 @@ func Sign(w io.Writer, r io.Reader, options *SignOptions) error {
}

if options.QueryMethods != nil {
params["q"] = formatTagList(options.QueryMethods)
methods := make([]string, len(options.QueryMethods))
for i, method := range options.QueryMethods {
methods[i] = string(method)
}
params["q"] = formatTagList(methods)
}

if !options.Expiration.IsZero() {
Expand Down
4 changes: 2 additions & 2 deletions verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,13 @@ func verify(h header, r io.Reader, sigField, sigValue string) (*Verification, er

// Query public key
// TODO: compute hash in parallel
methods := []string{"dns/txt"}
methods := []string{string(QueryMethodDNSTXT)}
if methodsStr, ok := params["q"]; ok {
methods = parseTagList(methodsStr)
}
var res *queryResult
for _, method := range methods {
if query, ok := queryMethods[method]; ok {
if query, ok := queryMethods[QueryMethod(method)]; ok {
res, err = query(params["d"], params["s"])
break
}
Expand Down

0 comments on commit 054bae4

Please sign in to comment.