forked from ppmathis/cloudns-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
56 lines (48 loc) · 1.52 KB
/
auth.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
52
53
54
55
56
package cloudns
// AuthType is an enumeration of the various ways of authenticating against the ClouDNS API
type AuthType int
// Enumeration values for AuthType
const (
AuthTypeNone AuthType = iota
AuthTypeUserID
AuthTypeSubUserID
AuthTypeSubUserName
)
// Auth provides methods for turning human-friendly credentials into API parameters
type Auth struct {
Type AuthType
UserID int
SubUserID int
SubUserName string
Password string
}
// NewAuth instantiates an empty Auth which contains no credentials / AuthTypeNone
func NewAuth() *Auth {
return &Auth{Type: AuthTypeNone}
}
// GetParams returns the correct API parameters for the ClouDNS API which should be provided by either query parameters
// (when using GET) or the POST body as JSON
func (auth *Auth) GetParams() HTTPParams {
params := make(HTTPParams)
switch auth.Type {
case AuthTypeNone:
break
case AuthTypeUserID:
params["auth-id"] = auth.UserID
params["auth-password"] = auth.Password
case AuthTypeSubUserID:
params["sub-auth-id"] = auth.SubUserID
params["auth-password"] = auth.Password
case AuthTypeSubUserName:
params["sub-auth-user"] = auth.SubUserName
params["auth-password"] = auth.Password
default:
panic("invalid authentication type")
}
return params
}
// getAllParamKeys returns all keys involved in authentication, which is being used to filter credentials out of
// automatically generated test fixtures
func (auth *Auth) getAllParamKeys() []string {
return []string{"auth-id", "sub-auth-id", "sub-auth-user", "auth-password"}
}