aboutsummaryrefslogtreecommitdiff
path: root/authorize.go
blob: 54b682d09a66b3fea41c49d034420fcc689c83bc (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
package acme

import "time"

// Authorization request
type Authorization struct {
	Resource     Resource    `json:"resource"` // new-authz
	ID           string      `json:"id,omitempty"`
	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, d *Desire, domain string) error {
	req := &Authorization{
		Resource: ResNewAuthz,
		Identifier: Identifier{
			Type:  IdentDNS,
			Value: domain,
		},
	}
	resp, err := p.post(p.NewAuthz, s, req)
	if err != nil {
		return err
	}
	if err := parseJson(resp, req); err != nil {
		return err
	}
	for _, ch := range req.Supported(d.solver) {
		if err := p.Solve(s, ch, d.solver[ch.Type]); err != nil {
			return err
		}
	}
	return nil
}