Skip to content

Commit 1eba785

Browse files
author
lijianguo
committed
feat(OIDC): fetch token
1 parent 3bb0133 commit 1eba785

3 files changed

Lines changed: 62 additions & 40 deletions

File tree

api/auth.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ type AuthRegisterRequest struct {
99
LastName string `json:"last_name,omitempty"`
1010
}
1111

12-
// AuthLoginRequest is the definition of POST /v2/auth/login/.
13-
type AuthLoginRequest struct {
14-
Username string `json:"username"`
15-
Password string `json:"password"`
16-
}
17-
1812
// AuthLoginResponse is the definition of /v2/auth/login/.
1913
type AuthLoginResponse tokenResponse
2014

@@ -41,5 +35,6 @@ type AuthRegenerateResponse tokenResponse
4135

4236
// A generic defenition of a token response.
4337
type tokenResponse struct {
44-
Token string `json:"token"`
38+
Token string `json:"token"`
39+
Username string `json:"username,omitempty"`
4540
}

auth/auth.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package auth
33

44
import (
55
"encoding/json"
6-
6+
"fmt"
77
drycc "github.com/drycc/controller-sdk-go"
88
"github.com/drycc/controller-sdk-go/api"
9+
"net/http"
910
)
1011

1112
// Register a new user with the controller.
@@ -26,27 +27,35 @@ func Register(c *drycc.Client, username, password, email string) error {
2627
return err
2728
}
2829

29-
// Login to the controller and get a token
30-
func Login(c *drycc.Client, username, password string) (string, error) {
31-
user := api.AuthLoginRequest{Username: username, Password: password}
32-
reqBody, err := json.Marshal(user)
33-
34-
if err != nil {
35-
return "", err
30+
// Login to the controller and get a oauth url
31+
func Login(c *drycc.Client) (string, error) {
32+
c.HTTPClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
33+
return http.ErrUseLastResponse
3634
}
35+
res, reqErr := c.Request("POST", "/v2/auth/login/", nil)
3736

38-
res, reqErr := c.Request("POST", "/v2/auth/login/", reqBody)
3937
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
4038
return "", reqErr
4139
}
4240
defer res.Body.Close()
4341

42+
URL := res.Header.Get("Location")
43+
return URL, reqErr
44+
}
45+
46+
// Token to the controller and get a token
47+
func Token(c *drycc.Client, key string) (api.AuthLoginResponse, error) {
48+
path := fmt.Sprintf("/v2/auth/token/%s/", key)
49+
res, reqErr := c.Request("GET", path, nil)
50+
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
51+
return api.AuthLoginResponse{}, reqErr
52+
}
53+
defer res.Body.Close()
4454
token := api.AuthLoginResponse{}
45-
if err = json.NewDecoder(res.Body).Decode(&token); err != nil {
46-
return "", err
55+
if err := json.NewDecoder(res.Body).Decode(&token); err != nil {
56+
return api.AuthLoginResponse{}, err
4757
}
48-
49-
return token.Token, reqErr
58+
return token, reqErr
5059
}
5160

5261
// Delete deletes a user.

auth/auth_test.go

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

33
import (
44
"fmt"
5+
"github.com/drycc/controller-sdk-go/api"
56
"io/ioutil"
67
"net/http"
78
"net/http/httptest"
@@ -11,7 +12,6 @@ import (
1112
)
1213

1314
const registerExpected string = `{"username":"test","password":"opensesame","email":"test@example.com"}`
14-
const loginExpected string = `{"username":"test","password":"opensesame"}`
1515
const passwdExpected string = `{"username":"test","password":"old","new_password":"new"}`
1616
const regenAllExpected string = `{"all":true}`
1717
const regenUserExpected string = `{"username":"test"}`
@@ -51,23 +51,15 @@ func (f *fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
5151
}
5252

5353
if req.URL.Path == "/v2/auth/login/" && req.Method == "POST" {
54-
body, err := ioutil.ReadAll(req.Body)
55-
56-
if err != nil {
57-
fmt.Println(err)
58-
res.WriteHeader(http.StatusInternalServerError)
59-
res.Write(nil)
60-
return
61-
}
62-
63-
if string(body) != loginExpected {
64-
fmt.Printf("Expected '%s', Got '%s'\n", loginExpected, body)
65-
res.WriteHeader(http.StatusInternalServerError)
66-
res.Write(nil)
67-
return
68-
}
54+
res.Header().Add("Location", "/v2/login/drycc/?key=fdbf3b34742e4ed2be4dfa848af13007/")
55+
res.WriteHeader(http.StatusFound)
56+
res.Write(nil)
57+
return
58+
}
6959

70-
res.Write([]byte(`{"token":"abc"}`))
60+
if req.URL.Path == "/v2/auth/token/fdbf3b34742e4ed2be4dfa848af13007/" && req.Method == "GET" {
61+
res.WriteHeader(http.StatusOK)
62+
res.Write([]byte(`{"username":"test","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"}`))
7163
return
7264
}
7365

@@ -187,19 +179,45 @@ func TestLogin(t *testing.T) {
187179
t.Fatal(err)
188180
}
189181

190-
actual, err := Login(drycc, "test", "opensesame")
182+
actual, err := Login(drycc)
191183

192184
if err != nil {
193185
t.Error(err)
194186
}
195187

196-
expected := "abc"
197-
188+
expected := "/v2/login/drycc/?key=fdbf3b34742e4ed2be4dfa848af13007/"
198189
if actual != expected {
199190
t.Errorf("Expected %s, Got %s", expected, actual)
200191
}
201192
}
202193

194+
func TestToken(t *testing.T) {
195+
t.Parallel()
196+
197+
handler := fakeHTTPServer{}
198+
server := httptest.NewServer(&handler)
199+
defer server.Close()
200+
201+
drycc, err := drycc.New(false, server.URL, "abc")
202+
if err != nil {
203+
t.Fatal(err)
204+
}
205+
expected := api.AuthLoginResponse{
206+
Username: "test",
207+
Token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
208+
}
209+
210+
token, err := Token(drycc, "fdbf3b34742e4ed2be4dfa848af13007")
211+
212+
if err != nil {
213+
t.Error(err)
214+
}
215+
216+
if token != expected {
217+
t.Errorf("Expected %s, Got %s", expected, token)
218+
}
219+
}
220+
203221
func TestPasswd(t *testing.T) {
204222
t.Parallel()
205223

0 commit comments

Comments
 (0)