Skip to content

Commit 55c26f1

Browse files
committed
chore(auth): add password login
1 parent 3fd7ab3 commit 55c26f1

7 files changed

Lines changed: 53 additions & 32 deletions

File tree

cmd/auth.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,27 @@ import (
1414
"github.com/drycc/workflow-cli/settings"
1515
)
1616

17-
func (d *DryccCmd) doLogin(s settings.Settings) error {
18-
URL, err := auth.Login(s.Client)
17+
func (d *DryccCmd) doLogin(s settings.Settings, username, password string) error {
18+
key, err := auth.Login(s.Client, username, password)
1919
if d.checkAPICompatibility(s.Client, err) != nil {
2020
return err
2121
}
2222
if err != nil {
2323
return nil
2424
}
25-
fmt.Printf("Opening browser to %s\n", URL)
26-
d.Print("Waiting for login... ")
27-
err = d.openBrower(URL)
28-
if err != nil {
29-
d.Print("Cannot open browser, please visit the website in yourself")
30-
}
31-
u, err := url.Parse(URL)
32-
if err != nil {
33-
return err
25+
if username == "" || password == "" {
26+
fmt.Printf("Opening browser to %s\n", key)
27+
d.Print("Waiting for login... ")
28+
err = d.openBrower(key)
29+
if err != nil {
30+
d.Print("Cannot open browser, please visit the website in yourself")
31+
}
32+
u, err := url.Parse(key)
33+
if err != nil {
34+
return err
35+
}
36+
key = u.Query()["key"][0]
3437
}
35-
key := u.Query()["key"][0]
3638
quit := progress(d.WOut)
3739
d.doToken(s, key)
3840
quit <- true
@@ -60,13 +62,13 @@ func (d *DryccCmd) openBrower(URL string) error {
6062
}
6163

6264
func (d *DryccCmd) doToken(s settings.Settings, key string) error {
63-
var token api.AuthLoginResponse
65+
var token api.AuthTokenResponse
6466
for i := 0; i <= 120; i++ {
6567
token, _ = auth.Token(s.Client, key)
66-
if token != (api.AuthLoginResponse{}) {
68+
time.Sleep(time.Duration(5) * time.Second)
69+
if token.Token != "" && token.Username != "" {
6770
break
6871
}
69-
time.Sleep(time.Duration(5) * time.Second)
7072
}
7173
if token.Token == "" || token.Token == "fail" {
7274
d.Printf("Logged fail")
@@ -84,7 +86,7 @@ func (d *DryccCmd) doToken(s settings.Settings, key string) error {
8486
}
8587

8688
// Login to a Drycc controller.
87-
func (d *DryccCmd) Login(controller string, sslVerify bool) error {
89+
func (d *DryccCmd) Login(controller string, sslVerify bool, username, password string) error {
8890
c, err := drycc.New(sslVerify, controller, "")
8991

9092
if err != nil {
@@ -99,7 +101,7 @@ func (d *DryccCmd) Login(controller string, sslVerify bool) error {
99101
}
100102

101103
s := settings.Settings{Client: c}
102-
return d.doLogin(s)
104+
return d.doLogin(s, username, password)
103105
}
104106

105107
// Logout from a Drycc controller.

cmd/auth_test.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ package cmd
33
import (
44
"bytes"
55
"fmt"
6+
"io"
67
"net/http"
78
"testing"
89

910
"github.com/drycc/workflow-cli/pkg/testutil"
1011
"github.com/stretchr/testify/assert"
1112
)
1213

14+
const keyFixture = "fdbf3b34742e4ed2be4dfa848af13007"
15+
1316
func TestLogin(t *testing.T) {
1417
t.Skip("Skip long running tests")
1518
cf, server, err := testutil.NewTestServerAndClient()
@@ -26,20 +29,31 @@ func TestLogin(t *testing.T) {
2629
fmt.Fprintf(w, `{}`)
2730
})
2831

29-
server.Mux.HandleFunc("/v2/auth/login/", func(w http.ResponseWriter, _ *http.Request) {
30-
testutil.SetHeaders(w)
31-
w.Header().Add("Location", "/v2/login/drycc/?key=fdbf3b34742e4ed2be4dfa848af13007/")
32-
w.WriteHeader(http.StatusOK)
33-
w.Write(nil)
32+
server.Mux.HandleFunc("/v2/auth/login/", func(w http.ResponseWriter, r *http.Request) {
33+
body, err := io.ReadAll(r.Body)
34+
if err != nil {
35+
fmt.Println(err)
36+
w.WriteHeader(http.StatusInternalServerError)
37+
w.Write(nil)
38+
}
39+
w.WriteHeader(http.StatusFound)
40+
if len(body) == 0 {
41+
testutil.SetHeaders(w)
42+
w.Header().Add("Location", fmt.Sprintf("/v2/login/drycc/?key=%s/", keyFixture))
43+
w.WriteHeader(http.StatusOK)
44+
w.Write(nil)
45+
} else {
46+
w.Write([]byte(fmt.Sprintf(`{"key": "%s"}`, keyFixture)))
47+
}
3448
})
3549

36-
server.Mux.HandleFunc("/v2/auth/token/fdbf3b34742e4ed2be4dfa848af13007/", func(w http.ResponseWriter, _ *http.Request) {
50+
server.Mux.HandleFunc(fmt.Sprintf("/v2/auth/token/%s/", keyFixture), func(w http.ResponseWriter, _ *http.Request) {
3751
testutil.SetHeaders(w)
3852
w.WriteHeader(http.StatusOK)
39-
w.Write([]byte(`{"username":"test-user","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"}`))
53+
w.Write([]byte(`{"username":"test-user","token":"eaf2d1d85f6b410b81d94bfec159019b"}`))
4054
w.Write(nil)
4155
})
42-
err = cmdr.Login(server.Server.URL, false)
56+
err = cmdr.Login(server.Server.URL, false, "", "")
4357
assert.NoError(t, err)
4458
}
4559

cmd/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type Commander interface {
2020
AutoscaleList(string) error
2121
AutoscaleSet(string, string, int, int, int) error
2222
AutoscaleUnset(string, string) error
23-
Login(string, bool) error
23+
Login(string, bool, string, string) error
2424
Logout() error
2525
Whoami(bool) error
2626
BuildsList(string, int) error

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.22
55
require (
66
github.com/containerd/console v1.0.4
77
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
8-
github.com/drycc/controller-sdk-go v0.0.0-20240408054437-6ed94b0070f7
8+
github.com/drycc/controller-sdk-go v0.0.0-20240412083348-10d3dbba3590
99
github.com/drycc/pkg v0.0.0-20240225112316-78fc9239f51f
1010
github.com/olekukonko/tablewriter v0.0.5
1111
github.com/stretchr/testify v1.9.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
44
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
55
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ=
66
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
7-
github.com/drycc/controller-sdk-go v0.0.0-20240408054437-6ed94b0070f7 h1:K1P84rQyr1GwC2/82LRmHmc6ocUJFYiyi0XhgdzzL58=
8-
github.com/drycc/controller-sdk-go v0.0.0-20240408054437-6ed94b0070f7/go.mod h1:RJ0QxVNWhximzfd08+FkZfKzrX7gOp2gsgXfpqVPJZQ=
7+
github.com/drycc/controller-sdk-go v0.0.0-20240412083348-10d3dbba3590 h1:xzZQEh9kKSCV0cRdWYm6FCITans2lCWG0j4J1jqIelU=
8+
github.com/drycc/controller-sdk-go v0.0.0-20240412083348-10d3dbba3590/go.mod h1:RJ0QxVNWhximzfd08+FkZfKzrX7gOp2gsgXfpqVPJZQ=
99
github.com/drycc/pkg v0.0.0-20240225112316-78fc9239f51f h1:kgjvUQJeAszDoU1Vo4vTTE92KI8Av3JPb6Qn890niXg=
1010
github.com/drycc/pkg v0.0.0-20240225112316-78fc9239f51f/go.mod h1:n+QxGif6ha9CEoxVnlipxb9IdmerybcUSzTEDFkvjiA=
1111
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=

parser/auth.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ Arguments:
4646
a fully-qualified controller URI, e.g. "http://drycc.local3.dryccapp.com/".
4747
4848
Options:
49+
--username=<username>
50+
provide a username for the account.
51+
--password=<password>
52+
provide a password for the account.
4953
--ssl-verify=true
5054
enables/disables SSL certificate verification for API requests
5155
`
@@ -58,12 +62,13 @@ Options:
5862

5963
controller := safeGetString(args, "<controller>")
6064
sslVerify := true
61-
6265
if args["--ssl-verify"] != nil && args["--ssl-verify"].(string) == "false" {
6366
sslVerify = false
6467
}
68+
username := safeGetString(args, "--username")
69+
password := safeGetString(args, "--password")
6570

66-
return cmdr.Login(controller, sslVerify)
71+
return cmdr.Login(controller, sslVerify, username, password)
6772
}
6873

6974
func authLogout(argv []string, cmdr cmd.Commander) error {

parser/auth_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
// Create fake implementations of each method that return the argument
1313
// we expect to have called the function (as an error to satisfy the interface).
1414

15-
func (d FakeDryccCmd) Login(string, bool) error {
15+
func (d FakeDryccCmd) Login(string, bool, string, string) error {
1616
return errors.New("auth:login")
1717
}
1818

0 commit comments

Comments
 (0)