aboutsummaryrefslogtreecommitdiff
path: root/authorize.go
blob: 240f8021cdd5232096f25e1e6b70b164467a29ec (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) authorize(s Signer, domain string, sol map[ChalType]Solver) ([]Challenge, error) {
	req := &Authorization{
		Resource: ResNewAuthz,
		Identifier: Identifier{
			Type:  IdentDNS,
			Value: domain,
		},
	}
	resp, err := p.post(p.NewAuthz, s, req)
	if err != nil {
		return nil, err
	}
	err = parseJson(resp, req)
	if err != nil {
		return nil, err
	}
	return req.Supported(sol), nil
}

func (p *Provider) Authorize(s Signer, d *Desire) error {
	for _, domain := range d.altnames {
		chal, err := p.authorize(s, domain, d.solver)
		if err != nil {
			return err
		}
		for _, ch := range chal {
			sol := d.solver[ch.Type]
			if err := p.Solve(s, ch, sol); err != nil {
				return err
			}
		}
	}
	return nil
}