Skip to content

Commit 9e7420d

Browse files
authored
Support basic authentication via HTTPS (#55)
1 parent 3f4dfc8 commit 9e7420d

File tree

3 files changed

+59
-11
lines changed

3 files changed

+59
-11
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,23 @@ Even if protodep.lock exists, you can force update dependenies.
8484
$ protodep up -f
8585
```
8686

87+
### Getting via HTTPS
88+
89+
If you want to get it via HTTPS, do as follows.
90+
91+
```bash
92+
$ protodep up --use-https
93+
```
94+
95+
And also, if Basic authentication is required, do as follows.
96+
If you have 2FA enabled, specify the Personal Access Token as the password.
97+
98+
```bash
99+
$ protodep up --use-https \
100+
--basic-auth-username=your-github-username \
101+
--basic-auth-password=your-github-password
102+
```
103+
87104
License
88105
===
89106
See [LICENSE](LICENSE).

cmd/up.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ var upCmd = &cobra.Command{
5858
}
5959
logger.Info("use https = %t", useHttps)
6060

61+
basicAuthUsername, err := cmd.Flags().GetString("basic-auth-username")
62+
if err != nil {
63+
return err
64+
}
65+
if basicAuthUsername != "" {
66+
logger.Info("https basic auth username = %s", basicAuthUsername)
67+
}
68+
69+
basicAuthPassword, err := cmd.Flags().GetString("basic-auth-password")
70+
if err != nil {
71+
return err
72+
}
73+
if basicAuthPassword != "" {
74+
logger.Info("https basic auth username = %s", strings.Repeat("x", len(basicAuthPassword))) // Do not display the password.
75+
}
76+
6177
pwd, err := os.Getwd()
6278
if err != nil {
6379
return err
@@ -69,7 +85,7 @@ var upCmd = &cobra.Command{
6985
}
7086

7187
if useHttps {
72-
authProvider = helper.NewAuthProvider(helper.WithHTTPS())
88+
authProvider = helper.NewAuthProvider(helper.WithHTTPS(basicAuthUsername, basicAuthPassword))
7389
} else {
7490
if identityFile == "" && password == "" {
7591
authProvider = helper.NewAuthProvider()
@@ -98,4 +114,6 @@ func initDepCmd() {
98114
upCmd.PersistentFlags().StringP("password", "p", "", "set the password for SSH")
99115
upCmd.PersistentFlags().BoolP("cleanup", "c", false, "cleanup cache before exec.")
100116
upCmd.PersistentFlags().BoolP("use-https", "u", false, "use HTTPS to get dependencies.")
117+
upCmd.PersistentFlags().StringP("basic-auth-username", "", "", "set the username with Basic Auth via HTTPS")
118+
upCmd.PersistentFlags().StringP("basic-auth-password", "", "", "set the password or personal access token(when enabled 2FA) with Basic Auth via HTTPS")
101119
}

helper/auth.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@ package helper
22

33
import (
44
"fmt"
5+
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
56

67
"gopkg.in/src-d/go-git.v4/plumbing/transport"
78
"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
89
)
910

1011
type authMethod string
1112

12-
const(
13+
const (
1314
SSHAgent authMethod = "SSHAgent"
14-
SSH = "SSH"
15-
HTTPS = "HTTPS"
15+
SSH = "SSH"
16+
HTTPS = "HTTPS"
1617
)
1718

1819
type authOptions struct {
19-
method authMethod
20-
pemFile string
20+
method authMethod
21+
pemFile string
22+
username string
2123
password string
2224
}
2325

@@ -29,7 +31,6 @@ func (fao *funcAuthOption) apply(do *authOptions) {
2931
fao.f(do)
3032
}
3133

32-
3334
type AuthOption interface {
3435
apply(*authOptions)
3536
}
@@ -48,12 +49,16 @@ type AuthProviderWithSSHAgent struct {
4849
}
4950

5051
type AuthProviderHTTPS struct {
52+
username string
53+
password string
5154
}
5255

53-
func WithHTTPS() AuthOption {
56+
func WithHTTPS(username, password string) AuthOption {
5457
return &funcAuthOption{
5558
f: func(options *authOptions) {
5659
options.method = HTTPS
60+
options.username = username
61+
options.password = password
5762
},
5863
}
5964
}
@@ -85,7 +90,10 @@ func NewAuthProvider(opt ...AuthOption) AuthProvider {
8590
password: opts.password,
8691
}
8792
} else {
88-
authProvider = &AuthProviderHTTPS{}
93+
authProvider = &AuthProviderHTTPS{
94+
username: opts.username,
95+
password: opts.password,
96+
}
8997
}
9098

9199
return authProvider
@@ -128,6 +136,11 @@ func (p *AuthProviderHTTPS) GetRepositoryURL(reponame string) string {
128136
}
129137

130138
func (p *AuthProviderHTTPS) AuthMethod() (transport.AuthMethod, error) {
131-
// nil is ok.
132-
return nil, nil
139+
if p.username == "" && p.password == "" {
140+
return nil, nil
141+
}
142+
return &http.BasicAuth{
143+
Username: p.username,
144+
Password: p.password,
145+
}, nil
133146
}

0 commit comments

Comments
 (0)