Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 01:00 UTC
| 1 | package acmeserver |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "strings" |
| 7 | |
| 8 | "github.com/smallstep/certificates/authority/provisioner" |
| 9 | ) |
| 10 | |
| 11 | // ACMEChallenge is an opaque string that represents supported ACME challenges. |
| 12 | type ACMEChallenge string |
| 13 | |
| 14 | const ( |
| 15 | HTTP_01 ACMEChallenge = "http-01" |
| 16 | DNS_01 ACMEChallenge = "dns-01" |
| 17 | TLS_ALPN_01 ACMEChallenge = "tls-alpn-01" |
| 18 | ) |
| 19 | |
| 20 | // validate checks if the given challenge is supported. |
| 21 | func (c ACMEChallenge) validate() error { |
| 22 | switch c { |
| 23 | case HTTP_01, DNS_01, TLS_ALPN_01: |
| 24 | return nil |
| 25 | default: |
| 26 | return fmt.Errorf("acme challenge %q is not supported", c) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // The unmarshaller first marshals the value into a string. Then it |
| 31 | // trims any space around it and lowercase it for normaliztion. The |
| 32 | // method does not and should not validate the value within accepted enums. |
| 33 | func (c *ACMEChallenge) UnmarshalJSON(b []byte) error { |
| 34 | var s string |
| 35 | if err := json.Unmarshal(b, &s); err != nil { |
| 36 | return err |
| 37 | } |
| 38 | *c = ACMEChallenge(strings.ToLower(strings.TrimSpace(s))) |
| 39 | return nil |
| 40 | } |
| 41 | |
| 42 | // String returns a string representation of the challenge. |
| 43 | func (c ACMEChallenge) String() string { |
| 44 | return strings.ToLower(string(c)) |
| 45 | } |
| 46 | |
| 47 | // ACMEChallenges is a list of ACME challenges. |
| 48 | type ACMEChallenges []ACMEChallenge |
| 49 | |
| 50 | // validate checks if the given challenges are supported. |
| 51 | func (c ACMEChallenges) validate() error { |
| 52 | for _, ch := range c { |
| 53 | if err := ch.validate(); err != nil { |
| 54 | return err |
| 55 | } |
| 56 | } |
| 57 | return nil |
| 58 | } |
| 59 | |
| 60 | func (c ACMEChallenges) toSmallstepType() []provisioner.ACMEChallenge { |
| 61 | if len(c) == 0 { |
| 62 | return nil |
| 63 | } |
| 64 | ac := make([]provisioner.ACMEChallenge, len(c)) |
| 65 | for i, ch := range c { |
| 66 | ac[i] = provisioner.ACMEChallenge(ch) |
| 67 | } |
| 68 | return ac |
| 69 | } |
| 70 | |
| 71 | func stringToChallenges(chs []string) ACMEChallenges { |
| 72 | challenges := make(ACMEChallenges, len(chs)) |
| 73 | for i, ch := range chs { |
| 74 | challenges[i] = ACMEChallenge(ch) |
| 75 | } |
| 76 | return challenges |
| 77 | } |