|
| 1 | +package servers |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | +) |
| 8 | + |
| 9 | +const ( |
| 10 | + strNoClientCert = "NoClientCert" |
| 11 | + strRequestClientCert = "RequestClientCert" |
| 12 | + strRequireAnyClientCert = "RequireAnyClientCert" |
| 13 | + strVerifyClientCertIfGiven = "VerifyClientCertIfGiven" |
| 14 | + strRequireAndVerifyClientCert = "RequireAndVerifyClientCert" |
| 15 | +) |
| 16 | + |
| 17 | +type clientAuthTypeTLS tls.ClientAuthType |
| 18 | + |
| 19 | +const clientAuthTypeTLSDefault clientAuthTypeTLS = clientAuthTypeTLS(tls.NoClientCert) |
| 20 | +const clientAuthTypeTLSUnknown clientAuthTypeTLS = -1 |
| 21 | + |
| 22 | +func (c clientAuthTypeTLS) String() string { |
| 23 | + switch tls.ClientAuthType(c) { |
| 24 | + case tls.NoClientCert: |
| 25 | + return strNoClientCert |
| 26 | + case tls.RequestClientCert: |
| 27 | + return strRequestClientCert |
| 28 | + case tls.RequireAnyClientCert: |
| 29 | + return strRequireAnyClientCert |
| 30 | + case tls.VerifyClientCertIfGiven: |
| 31 | + return strVerifyClientCertIfGiven |
| 32 | + case tls.RequireAndVerifyClientCert: |
| 33 | + return strRequireAndVerifyClientCert |
| 34 | + case tls.ClientAuthType(clientAuthTypeTLSUnknown): |
| 35 | + return "unknown" |
| 36 | + default: |
| 37 | + return "undefined" |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func (c clientAuthTypeTLS) SetedOrDefault() string { |
| 42 | + if c.isDefined() { |
| 43 | + return c.String() |
| 44 | + } |
| 45 | + return versionTLSDefault.String() |
| 46 | +} |
| 47 | + |
| 48 | +func (c clientAuthTypeTLS) isDefined() bool { |
| 49 | + switch tls.ClientAuthType(c) { |
| 50 | + case tls.NoClientCert: |
| 51 | + return true |
| 52 | + case tls.RequestClientCert: |
| 53 | + return true |
| 54 | + case tls.RequireAnyClientCert: |
| 55 | + return true |
| 56 | + case tls.VerifyClientCertIfGiven: |
| 57 | + return true |
| 58 | + case tls.RequireAndVerifyClientCert: |
| 59 | + return true |
| 60 | + default: |
| 61 | + return false |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func (c clientAuthTypeTLS) setedOrDefault() clientAuthTypeTLS { |
| 66 | + if c.isDefined() { |
| 67 | + return c |
| 68 | + } |
| 69 | + return clientAuthTypeTLSDefault |
| 70 | +} |
| 71 | + |
| 72 | +func (c *clientAuthTypeTLS) unmarshal(fn func(interface{}) error) error { |
| 73 | + var raw string |
| 74 | + |
| 75 | + if err := fn(&raw); err != nil { |
| 76 | + return fmt.Errorf("error unmarshal client authType: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + if *c = newClientAuthTypeTLS(raw); *c == clientAuthTypeTLSUnknown { |
| 80 | + return fmt.Errorf("error unmarshal client authType: %s", clientAuthTypeTLSUnknown) |
| 81 | + } |
| 82 | + |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +func (c clientAuthTypeTLS) MarshalJSON() ([]byte, error) { |
| 87 | + return []byte(fmt.Sprintf("%q", c.String())), nil |
| 88 | +} |
| 89 | + |
| 90 | +func (c clientAuthTypeTLS) MarshalYAML() (interface{}, error) { |
| 91 | + return c.String(), nil |
| 92 | +} |
| 93 | + |
| 94 | +func (c *clientAuthTypeTLS) UnmarshalJSON(data []byte) error { |
| 95 | + return c.unmarshal(func(c interface{}) error { return json.Unmarshal(data, c) }) |
| 96 | +} |
| 97 | + |
| 98 | +func (c *clientAuthTypeTLS) UnmarshalYAML(unmarshal func(interface{}) error) error { |
| 99 | + return c.unmarshal(unmarshal) |
| 100 | +} |
| 101 | + |
| 102 | +func newClientAuthTypeTLS(raw string) clientAuthTypeTLS { |
| 103 | + switch raw { |
| 104 | + case strNoClientCert: |
| 105 | + return clientAuthTypeTLS(tls.NoClientCert) |
| 106 | + case strRequestClientCert: |
| 107 | + return clientAuthTypeTLS(tls.RequestClientCert) |
| 108 | + case strRequireAnyClientCert: |
| 109 | + return clientAuthTypeTLS(tls.RequireAnyClientCert) |
| 110 | + case strVerifyClientCertIfGiven: |
| 111 | + return clientAuthTypeTLS(tls.VerifyClientCertIfGiven) |
| 112 | + case strRequireAndVerifyClientCert: |
| 113 | + return clientAuthTypeTLS(tls.RequireAndVerifyClientCert) |
| 114 | + default: |
| 115 | + return clientAuthTypeTLSUnknown |
| 116 | + } |
| 117 | +} |
0 commit comments