File: authentication.go

package info (click to toggle)
golang-github-weppos-dnsimple-go 0.0~git20160204.0.65c1ca7-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 144 kB
  • sloc: makefile: 2
file content (73 lines) | stat: -rw-r--r-- 1,888 bytes parent folder | download | duplicates (2)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package dnsimple

import (
	"encoding/base64"
	"fmt"
)

const (
	httpHeaderDomainToken   = "X-DNSimple-Domain-Token"
	httpHeaderApiToken      = "X-DNSimple-Token"
	httpHeaderAuthorization = "Authorization"
)

// Provides credentials that can be used for authenticating with DNSimple
//
// More information on credentials may be found here:
//   http://developer.dnsimple.com/v2/#authentication
type Credentials interface {
	// Get the HTTP header key and value to use for authentication.
	HttpHeader() (string, string)
}

// Domain token authentication

type domainTokenCredentials struct {
	domainToken string
}

// Construct Credentials using the DNSimple Domain Token method
func NewDomainTokenCredentials(domainToken string) Credentials {
	return &domainTokenCredentials{domainToken: domainToken}
}

func (c *domainTokenCredentials) HttpHeader() (string, string) {
	return httpHeaderDomainToken, c.domainToken
}

// HTTP basic authentication

type httpBasicCredentials struct {
	email    string
	password string
}

// Construct Credentials using HTTP Basic Auth
func NewHttpBasicCredentials(email, password string) Credentials {
	return &httpBasicCredentials{email, password}
}

func (c *httpBasicCredentials) HttpHeader() (string, string) {
	return httpHeaderAuthorization, "Basic " + basicAuth(c.email, c.password)
}

func basicAuth(username, password string) string {
	auth := username + ":" + password
	return base64.StdEncoding.EncodeToString([]byte(auth))
}

// API token authentication

type apiTokenCredentials struct {
	email    string
	apiToken string
}

// Construct Credentials using the API Token method.
func NewApiTokenCredentials(email, apiToken string) Credentials {
	return &apiTokenCredentials{email: email, apiToken: apiToken}
}

func (c *apiTokenCredentials) HttpHeader() (string, string) {
	return httpHeaderApiToken, fmt.Sprintf("%v:%v", c.email, c.apiToken)
}