Skip to content

Commit 6be5299

Browse files
committed
feat(auth): add token api
1 parent 58c09bd commit 6be5299

5 files changed

Lines changed: 168 additions & 5 deletions

File tree

api/token.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package api
2+
3+
// Token is the structure of the token object.
4+
type Token struct {
5+
UUID string `json:"uuid"`
6+
Owner string `json:"owner"`
7+
Alias string `json:"alias"`
8+
Key string `json:"fuzzy_key"`
9+
Created string `json:"created"`
10+
Updated string `json:"updated"`
11+
}

auth/auth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ func Login(c *drycc.Client, username, password string) (string, error) {
4444
}
4545

4646
// Token to the controller and get a token
47-
func Token(c *drycc.Client, key string) (api.AuthTokenResponse, error) {
48-
path := fmt.Sprintf("/v2/auth/token/%s/", key)
47+
func Token(c *drycc.Client, key, alias string) (api.AuthTokenResponse, error) {
48+
path := fmt.Sprintf("/v2/auth/token/%s/?alias=%s", key, alias)
4949
res, reqErr := c.Request("GET", path, nil)
5050
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
5151
return api.AuthTokenResponse{}, reqErr

auth/auth_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (f *fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
2929
res.Write(nil)
3030
}
3131
if len(body) == 0 {
32-
res.Header().Add("Location", fmt.Sprintf("/v2/login/drycc/?key=%s/", keyFixture))
32+
res.Header().Add("Location", fmt.Sprintf("/v2/login/drycc/?key=%s/?alias=test", keyFixture))
3333
res.WriteHeader(http.StatusFound)
3434
res.Write(nil)
3535
} else {
@@ -66,7 +66,7 @@ func TestLogin(t *testing.T) {
6666
t.Error(err)
6767
}
6868

69-
expected := fmt.Sprintf("/v2/login/drycc/?key=%s/", keyFixture)
69+
expected := fmt.Sprintf("/v2/login/drycc/?key=%s/?alias=test", keyFixture)
7070
if actual != expected {
7171
t.Errorf("Expected %s, Got %s", expected, actual)
7272
}
@@ -97,7 +97,7 @@ func TestToken(t *testing.T) {
9797
Token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
9898
}
9999

100-
token, err := Token(drycc, keyFixture)
100+
token, err := Token(drycc, keyFixture, "test")
101101

102102
if err != nil {
103103
t.Error(err)

tokens/tokens.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package tokens
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
drycc "github.com/drycc/controller-sdk-go"
8+
"github.com/drycc/controller-sdk-go/api"
9+
)
10+
11+
// List tokens.
12+
func List(c *drycc.Client, results int) ([]api.Token, int, error) {
13+
body, count, reqErr := c.LimitedRequest("/v2/tokens/", results)
14+
15+
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
16+
return []api.Token{}, -1, reqErr
17+
}
18+
19+
var tokens []api.Token
20+
if err := json.Unmarshal([]byte(body), &tokens); err != nil {
21+
return []api.Token{}, -1, err
22+
}
23+
24+
return tokens, count, reqErr
25+
}
26+
27+
// Delete a token
28+
func Delete(c *drycc.Client, id string) error {
29+
u := fmt.Sprintf("/v2/tokens/%s/", id)
30+
res, err := c.Request("DELETE", u, nil)
31+
if err == nil {
32+
res.Body.Close()
33+
}
34+
return err
35+
}

tokens/tokens_test.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package tokens
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"net/http/httptest"
7+
"reflect"
8+
"testing"
9+
10+
drycc "github.com/drycc/controller-sdk-go"
11+
"github.com/drycc/controller-sdk-go/api"
12+
)
13+
14+
const tokensFixture string = `
15+
{
16+
"count": 2,
17+
"next": null,
18+
"previous": null,
19+
"results": [
20+
{
21+
"uuid": "f71e3b18-e702-409e-bd7f-8fb0a66d7b12",
22+
"owner": "test",
23+
"alias": "",
24+
"fuzzy_key": "c8e74fa4cbf...e4954d602ec5ed19ba",
25+
"created": "2023-04-18T00:00:00UTC",
26+
"updated": "2023-04-19T00:00:00UTC"
27+
},
28+
{
29+
"uuid": "f71e3b18-e702-499e-bd7f-8fb0a66d7b12",
30+
"owner": "test",
31+
"alias": "test",
32+
"fuzzy_key": "c8e74fa4cbf...e4954d60cec5ed19ba",
33+
"created": "2023-04-18T10:00:00UTC",
34+
"updated": "2023-04-19T12:00:00UTC"
35+
}
36+
]
37+
}`
38+
39+
type fakeHTTPServer struct{}
40+
41+
func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
42+
res.Header().Add("DRYCC_API_VERSION", drycc.APIVersion)
43+
44+
if req.URL.Path == "/v2/tokens/" && req.Method == "GET" {
45+
res.Write([]byte(tokensFixture))
46+
return
47+
}
48+
49+
if req.URL.Path == "/v2/tokens/f71e3b18-e702-499e-bd7f-8fb0a66d7b12/" && req.Method == "DELETE" {
50+
res.WriteHeader(204)
51+
return
52+
}
53+
54+
fmt.Printf("Unrecognized URL %s\n", req.URL)
55+
res.WriteHeader(http.StatusNotFound)
56+
res.Write(nil)
57+
}
58+
59+
func TestTokensList(t *testing.T) {
60+
t.Parallel()
61+
62+
expected := []api.Token{
63+
{
64+
UUID: "f71e3b18-e702-409e-bd7f-8fb0a66d7b12",
65+
Owner: "test",
66+
Alias: "",
67+
Key: "c8e74fa4cbf...e4954d602ec5ed19ba",
68+
Created: "2023-04-18T00:00:00UTC",
69+
Updated: "2023-04-19T00:00:00UTC",
70+
},
71+
{
72+
UUID: "f71e3b18-e702-499e-bd7f-8fb0a66d7b12",
73+
Owner: "test",
74+
Alias: "test",
75+
Key: "c8e74fa4cbf...e4954d60cec5ed19ba",
76+
Created: "2023-04-18T10:00:00UTC",
77+
Updated: "2023-04-19T12:00:00UTC",
78+
},
79+
}
80+
handler := fakeHTTPServer{}
81+
server := httptest.NewServer(handler)
82+
defer server.Close()
83+
84+
drycc, err := drycc.New(false, server.URL, "abc")
85+
if err != nil {
86+
t.Fatal(err)
87+
}
88+
89+
actual, _, err := List(drycc, 100)
90+
91+
if err != nil {
92+
t.Fatal(err)
93+
}
94+
95+
if !reflect.DeepEqual(expected, actual) {
96+
t.Error(fmt.Errorf("Expected %v, Got %v", expected, actual))
97+
}
98+
}
99+
100+
func TestTokensRemove(t *testing.T) {
101+
t.Parallel()
102+
103+
handler := fakeHTTPServer{}
104+
server := httptest.NewServer(handler)
105+
defer server.Close()
106+
107+
drycc, err := drycc.New(false, server.URL, "abc")
108+
if err != nil {
109+
t.Fatal(err)
110+
}
111+
112+
err = Delete(drycc, "f71e3b18-e702-499e-bd7f-8fb0a66d7b12")
113+
114+
if err != nil {
115+
t.Fatal(err)
116+
}
117+
}

0 commit comments

Comments
 (0)