aboutsummaryrefslogtreecommitdiff
path: root/authorize.go
blob: 0a41edb1081e34edfee31a85a698fe7557ff44eb (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
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(sols Solvers) []Challenge {
	supported := func(com []int) bool {
		for _, n := range com {
			if n < 0 || n >= len(a.Challenges) {
				return false
			}
			if _, ok := sols[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(sols Solvers, domain string) error {
	req := &Authorization{
		Resource: ResNewAuthz,
		Identifier: Identifier{
			Type:  IdentDNS,
			Value: domain,
		},
	}
	resp, err := p.postJson(p.NewAuthz, req)
	if err != nil {
		return err
	}
	if err := parseJson(resp, req); err != nil {
		return err
	}
	for _, ch := range req.Supported(sols) {
		if err := p.Solve(ch, sols[ch.Type]); err != nil {
			return err
		}
	}
	return nil
}