| 1 |
package dnsimple |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/base64" |
| 5 |
"fmt" |
| 6 |
) |
| 7 |
|
| 8 |
const ( |
| 9 |
httpHeaderDomainToken = "X-DNSimple-Domain-Token" |
| 10 |
httpHeaderApiToken = "X-DNSimple-Token" |
| 11 |
httpHeaderAuthorization = "Authorization" |
| 12 |
) |
| 13 |
|
| 14 |
// Provides credentials that can be used for authenticating with DNSimple |
| 15 |
// |
| 16 |
// More information on credentials may be found here: |
| 17 |
// http://developer.dnsimple.com/v2/#authentication |
| 18 |
type Credentials interface { |
| 19 |
// Get the HTTP header key and value to use for authentication. |
| 20 |
HttpHeader() (string, string) |
| 21 |
} |
| 22 |
|
| 23 |
// Domain token authentication |
| 24 |
|
| 25 |
type domainTokenCredentials struct { |
| 26 |
domainToken string |
| 27 |
} |
| 28 |
|
| 29 |
// Construct Credentials using the DNSimple Domain Token method |
| 30 |
func NewDomainTokenCredentials(domainToken string) Credentials { |
| 31 |
return &domainTokenCredentials{domainToken: domainToken} |
| 32 |
} |
| 33 |
|
| 34 |
func (c *domainTokenCredentials) HttpHeader() (string, string) { |
| 35 |
return httpHeaderDomainToken, c.domainToken |
| 36 |
} |
| 37 |
|
| 38 |
// HTTP basic authentication |
| 39 |
|
| 40 |
type httpBasicCredentials struct { |
| 41 |
email string |
| 42 |
password string |
| 43 |
} |
| 44 |
|
| 45 |
// Construct Credentials using HTTP Basic Auth |
| 46 |
func NewHttpBasicCredentials(email, password string) Credentials { |
| 47 |
return &httpBasicCredentials{email, password} |
| 48 |
} |
| 49 |
|
| 50 |
func (c *httpBasicCredentials) HttpHeader() (string, string) { |
| 51 |
return httpHeaderAuthorization, "Basic " + basicAuth(c.email, c.password) |
| 52 |
} |
| 53 |
|
| 54 |
func basicAuth(username, password string) string { |
| 55 |
auth := username + ":" + password |
| 56 |
return base64.StdEncoding.EncodeToString([]byte(auth)) |
| 57 |
} |
| 58 |
|
| 59 |
// API token authentication |
| 60 |
|
| 61 |
type apiTokenCredentials struct { |
| 62 |
email string |
| 63 |
apiToken string |
| 64 |
} |
| 65 |
|
| 66 |
// Construct Credentials using the API Token method. |
| 67 |
func NewApiTokenCredentials(email, apiToken string) Credentials { |
| 68 |
return &apiTokenCredentials{email: email, apiToken: apiToken} |
| 69 |
} |
| 70 |
|
| 71 |
func (c *apiTokenCredentials) HttpHeader() (string, string) { |
| 72 |
return httpHeaderApiToken, fmt.Sprintf("%v:%v", c.email, c.apiToken) |
| 73 |
} |