aboutsummaryrefslogtreecommitdiff
path: root/authorize.go
blob: 0dc82a2b6ed8b96f129e84f3edafac3311f84051 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package acme

import "time"

// Authorization request
type Authorization struct {
	Resource     Resource    `json:"resource"` // new-authz
	Identifier   Identifier  `json:"identifier"`
	Status       Status      `json:"status,omitempty"` // e.g. valid
	Expires      *time.Time  `json:"expires,omitempty"`
	Challenges   []Challenge `json:"challenges,omitempty"`
	Combinations [][]int     `json:"combinations,omitempty"`
}

// Identifier ...
type Identifier struct {
	Type  IdentType `json:"type"`  // dns
	Value string    `json:"value"` // example.com
}

type IdentType string

const IdentDNS IdentType = "dns"

func (a Authorization) Supported(sol map[ChalType]Solver) []Challenge {
	supported := func(com []int) bool {
		for _, n := range com {
			if _, ok := sol[a.Challenges[n].Type]; !ok {
				return false
			}
		}
		return true
	}
	for _, com := range a.Combinations {
		if supported(com) {
			c := make([]Challenge, len(com))
			for i, n := range com {
				c[i] = a.Challenges[n]
			}
			return c
		}
	}
	return nil
}

func (p *Provider) authz(s Signer, domain string, sol map[ChalType]Solver) error {
	// first step: pocke
	req := &Authorization{
		Resource: ResNewAuthz,
		Identifier: Identifier{
			Type:  IdentDNS,
			Value: domain,
		},
	}
	resp, err := p.post(p.NewAuthz, s, req)
	if err != nil {
		return err
	}
	err = parseJson(resp, req)
	if err != nil {
		return err
	}
	// second step: choose and start solver
	for _, ch := range req.Supported(sol) {
		if err = p.solve(s, ch, sol[ch.Type]); err != nil {
			return err
		}
	}
	return nil
}

func (p *Provider) Authorize(s Signer, d *Desire) error {
	for _, domain := range d.altnames {
		if err := p.authz(s, domain, d.solver); err != nil {
			return err
		}
	}
	return nil
}