Skip to content

Commit 10d3dbb

Browse files
committed
feat(auth): add password login
1 parent 6ed94b0 commit 10d3dbb

3 files changed

Lines changed: 66 additions & 51 deletions

File tree

api/auth.go

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,16 @@
11
package api
22

3-
// AuthRegisterRequest is the definition of POST /v2/auth/register/.
4-
type AuthRegisterRequest struct {
5-
Username string `json:"username"`
6-
Password string `json:"password"`
7-
Email string `json:"email"`
8-
FirstName string `json:"first_name,omitempty"`
9-
LastName string `json:"last_name,omitempty"`
10-
}
11-
12-
// AuthLoginResponse is the definition of /v2/auth/login/.
13-
type AuthLoginResponse tokenResponse
14-
15-
// AuthPasswdRequest is the definition of POST /v2/auth/passwd/.
16-
type AuthPasswdRequest struct {
17-
Username string `json:"username,omitempty"`
18-
Password string `json:"password,omitempty"`
19-
NewPassword string `json:"new_password"`
20-
}
21-
22-
// AuthRegenerateRequest is the definition of POST /v2/auth/tokens/.
23-
type AuthRegenerateRequest struct {
24-
Name string `json:"username,omitempty"`
25-
All bool `json:"all,omitempty"`
3+
type AuthLoginRequest struct {
4+
Username string `json:"username,omitempty"`
5+
Password string `json:"password,omitempty"`
266
}
277

28-
// AuthCancelRequest is the definition of POST /v2/auth/cancel/.
29-
type AuthCancelRequest struct {
30-
Username string `json:"username"`
8+
type AuthLoginResponse struct {
9+
Key string `json:"key,omitempty"`
3110
}
3211

33-
// AuthRegenerateResponse is the definition of /v2/auth/tokens/.
34-
type AuthRegenerateResponse tokenResponse
35-
36-
// A generic defenition of a token response.
37-
type tokenResponse struct {
12+
// AuthTokenResponse is the definition of /v2/auth/login/.
13+
type AuthTokenResponse struct {
3814
Token string `json:"token"`
3915
Username string `json:"username,omitempty"`
4016
}

auth/auth.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,49 @@ import (
1111
)
1212

1313
// Login to the controller and get a oauth url
14-
func Login(c *drycc.Client) (string, error) {
14+
func Login(c *drycc.Client, username, password string) (string, error) {
1515
c.HTTPClient.CheckRedirect = func(*http.Request, []*http.Request) error {
1616
return http.ErrUseLastResponse
1717
}
18-
res, reqErr := c.Request("POST", "/v2/auth/login/", nil)
18+
var err error
19+
var body []byte
20+
if username != "" && password != "" {
21+
body, err = json.Marshal(api.AuthLoginRequest{Username: username, Password: password})
22+
if err != nil {
23+
return "", err
24+
}
25+
} else {
26+
body = nil
27+
}
1928

20-
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
21-
return "", reqErr
29+
res, err := c.Request("POST", "/v2/auth/login/", body)
30+
if err != nil && !drycc.IsErrAPIMismatch(err) {
31+
return "", err
2232
}
2333
defer res.Body.Close()
34+
if username != "" && password != "" {
35+
login := api.AuthLoginResponse{}
36+
if err := json.NewDecoder(res.Body).Decode(&login); err != nil {
37+
return "", err
38+
}
39+
return login.Key, nil
40+
}
2441

25-
URL := res.Header.Get("Location")
26-
return URL, reqErr
42+
url := res.Header.Get("Location")
43+
return url, err
2744
}
2845

2946
// Token to the controller and get a token
30-
func Token(c *drycc.Client, key string) (api.AuthLoginResponse, error) {
47+
func Token(c *drycc.Client, key string) (api.AuthTokenResponse, error) {
3148
path := fmt.Sprintf("/v2/auth/token/%s/", key)
3249
res, reqErr := c.Request("GET", path, nil)
3350
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
34-
return api.AuthLoginResponse{}, reqErr
51+
return api.AuthTokenResponse{}, reqErr
3552
}
3653
defer res.Body.Close()
37-
token := api.AuthLoginResponse{}
54+
token := api.AuthTokenResponse{}
3855
if err := json.NewDecoder(res.Body).Decode(&token); err != nil {
39-
return api.AuthLoginResponse{}, err
56+
return api.AuthTokenResponse{}, err
4057
}
4158
return token, reqErr
4259
}

auth/auth_test.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package auth
22

33
import (
44
"fmt"
5+
"io"
56
"net/http"
67
"net/http/httptest"
78
"testing"
@@ -11,25 +12,37 @@ import (
1112
drycc "github.com/drycc/controller-sdk-go"
1213
)
1314

15+
const keyFixture = "61a7907bf5b34659a14f96371fed2ebc"
16+
1417
type fakeHTTPServer struct {
1518
}
1619

1720
func (f *fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
1821
res.Header().Add("DRYCC_API_VERSION", drycc.APIVersion)
1922

2023
if req.URL.Path == "/v2/auth/login/" && req.Method == "POST" {
21-
res.Header().Add("Location", "/v2/login/drycc/?key=fdbf3b34742e4ed2be4dfa848af13007/")
22-
res.WriteHeader(http.StatusFound)
23-
res.Write(nil)
24-
return
24+
body, err := io.ReadAll(req.Body)
25+
26+
if err != nil {
27+
fmt.Println(err)
28+
res.WriteHeader(http.StatusInternalServerError)
29+
res.Write(nil)
30+
}
31+
if len(body) == 0 {
32+
res.Header().Add("Location", fmt.Sprintf("/v2/login/drycc/?key=%s/", keyFixture))
33+
res.WriteHeader(http.StatusFound)
34+
res.Write(nil)
35+
} else {
36+
res.WriteHeader(http.StatusFound)
37+
res.Write([]byte(fmt.Sprintf(`{"key": "%s"}`, keyFixture)))
38+
}
2539
}
2640

27-
if req.URL.Path == "/v2/auth/token/fdbf3b34742e4ed2be4dfa848af13007/" && req.Method == "GET" {
41+
if req.URL.Path == "/v2/auth/token/61a7907bf5b34659a14f96371fed2ebc/" && req.Method == "GET" {
2842
res.WriteHeader(http.StatusOK)
2943
res.Write([]byte(`{"username":"test","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"}`))
3044
return
3145
}
32-
3346
fmt.Printf("Unrecognized URL %s\n", req.URL)
3447
res.WriteHeader(http.StatusNotFound)
3548
res.Write(nil)
@@ -47,16 +60,25 @@ func TestLogin(t *testing.T) {
4760
t.Fatal(err)
4861
}
4962

50-
actual, err := Login(drycc)
63+
actual, err := Login(drycc, "", "")
5164

5265
if err != nil {
5366
t.Error(err)
5467
}
5568

56-
expected := "/v2/login/drycc/?key=fdbf3b34742e4ed2be4dfa848af13007/"
69+
expected := fmt.Sprintf("/v2/login/drycc/?key=%s/", keyFixture)
5770
if actual != expected {
5871
t.Errorf("Expected %s, Got %s", expected, actual)
5972
}
73+
74+
actual, err = Login(drycc, "admin", "admin")
75+
if err != nil {
76+
t.Error(err)
77+
}
78+
if actual != keyFixture {
79+
t.Errorf("Expected %s, Got %s", expected, actual)
80+
}
81+
6082
}
6183

6284
func TestToken(t *testing.T) {
@@ -70,12 +92,12 @@ func TestToken(t *testing.T) {
7092
if err != nil {
7193
t.Fatal(err)
7294
}
73-
expected := api.AuthLoginResponse{
95+
expected := api.AuthTokenResponse{
7496
Username: "test",
7597
Token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
7698
}
7799

78-
token, err := Token(drycc, "fdbf3b34742e4ed2be4dfa848af13007")
100+
token, err := Token(drycc, keyFixture)
79101

80102
if err != nil {
81103
t.Error(err)

0 commit comments

Comments
 (0)