/
api_key.go
51 lines (42 loc) · 1.29 KB
/
api_key.go
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
package credentials
import (
"fmt"
"strings"
)
// APIKey represents a Twilio API Key which can be used to authenticate
// against the Twilio APIs
type APIKey struct {
Account string
Sid string
Value string
}
// Validate ensures the API Keys is valid
func (apiKey APIKey) Validate() error {
validationErrors := make([]string, 0)
if apiKey.Account == "" {
validationErrors = append(validationErrors, "Account SID is required")
} else if !strings.HasPrefix(apiKey.Account, "AC") {
validationErrors = append(validationErrors, fmt.Sprintf("Account SID (%s) must start with AC", apiKey.Account))
}
if apiKey.Sid == "" {
validationErrors = append(validationErrors, "SID is required")
} else if !strings.HasPrefix(apiKey.Sid, "SK") {
validationErrors = append(validationErrors, fmt.Sprintf("SID (%s) must start with SK", apiKey.Sid))
}
if apiKey.Value == "" {
validationErrors = append(validationErrors, "Value is required")
}
if len(validationErrors) > 0 {
return fmt.Errorf("API Key details specified are invalid. Validation errors: [%s]", strings.Join(validationErrors, ", "))
}
return nil
}
func (apiKey APIKey) AccountSid() string {
return apiKey.Account
}
func (apiKey APIKey) username() string {
return apiKey.Sid
}
func (apiKey APIKey) password() string {
return apiKey.Value
}