forked from mislav/hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_method.go
56 lines (46 loc) · 1.18 KB
/
auth_method.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 octokit
import (
"encoding/base64"
"fmt"
"net/url"
"os"
"path/filepath"
"github.com/github/hub/Godeps/_workspace/src/github.com/fhs/go-netrc/netrc"
)
// See http://developer.github.com/v3/auth/
type AuthMethod interface {
fmt.Stringer
}
type BasicAuth struct {
Login string
Password string
OneTimePassword string // for two-factor authentication
}
func (b BasicAuth) String() string {
return fmt.Sprintf("Basic %s", hashAuth(b.Login, b.Password))
}
type NetrcAuth struct {
NetrcPath string
}
func (n NetrcAuth) String() string {
netrcPath := n.NetrcPath
if netrcPath == "" {
netrcPath = filepath.Join(os.Getenv("HOME"), ".netrc")
}
apiURL, _ := url.Parse(gitHubAPIURL)
credentials, err := netrc.FindMachine(netrcPath, apiURL.Host)
if err != nil {
panic(fmt.Errorf("netrc error (%s): %v", apiURL.Host, err))
}
return fmt.Sprintf("Basic %s", hashAuth(credentials.Login, credentials.Password))
}
func hashAuth(u, p string) string {
var a = fmt.Sprintf("%s:%s", u, p)
return base64.StdEncoding.EncodeToString([]byte(a))
}
type TokenAuth struct {
AccessToken string
}
func (t TokenAuth) String() string {
return fmt.Sprintf("token %s", t.AccessToken)
}