-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_header_jwt_test.go
113 lines (108 loc) · 3.44 KB
/
auth_header_jwt_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package rest
import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestHeaderJwtTokenAuth(t *testing.T) {
jwtToken := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.WKQfGgHiRhXdkdz6Qy90gMQhYf3uK-GMeyAQBEs1EbQ"
jwtFail := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.1F5StBaWKNe53iB2919Agg3nMcCdwINDWlT0sNBaMbE"
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("blabla blabla"))
require.NoError(t, err)
})
headerName := "Api-Token"
ts := httptest.NewServer(AuthenticationJwt(
headerName,
"1234567890",
func(claims map[string]interface{}) error { return nil })(handler))
defer ts.Close()
{
req, err := http.NewRequest("GET", ts.URL+"/ping", nil)
require.NoError(t, err)
req.Header.Set(headerName, jwtToken)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
defer resp.Body.Close()
}
{
req, err := http.NewRequest("GET", ts.URL+"/ping", nil)
require.NoError(t, err)
req.Header.Set(headerName, "invalid")
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, "Invalid token\n", string(b))
}
{
req, err := http.NewRequest("GET", ts.URL+"/ping", nil)
require.NoError(t, err)
req.Header.Set(headerName, jwtFail)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
assert.Equal(t, http.StatusForbidden, resp.StatusCode)
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, "Forbidden\n", string(b))
}
{
req, err := http.NewRequest("GET", ts.URL+"/ping", nil)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
assert.Equal(t, http.StatusForbidden, resp.StatusCode)
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, "Can not find token in header\n", string(b))
}
}
func TestHeaderJwtTokenAuthCheckClaim(t *testing.T) {
jwtToken := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiMTIzIn0.tsuWzcw0zCYzHoq0Kflun7cxVJWKMdQwWczNhU2h2IQ"
jwtFail := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJub3RfdXNlciI6IjEyMyJ9.wGj6rh93KK83eaehCoxwmMCyEvyEQXadeJykayMkEd8"
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("blabla blabla"))
require.NoError(t, err)
})
headerName := "Api-Token"
ts := httptest.NewServer(AuthenticationJwt(
headerName,
"1234567890",
func(claims map[string]interface{}) error {
if claims["user_id"] == nil {
return fmt.Errorf("user_id not found")
}
return nil
})(handler))
defer ts.Close()
{
req, err := http.NewRequest("GET", ts.URL+"/ping", nil)
require.NoError(t, err)
req.Header.Set(headerName, jwtToken)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
defer resp.Body.Close()
}
{
req, err := http.NewRequest("GET", ts.URL+"/ping", nil)
require.NoError(t, err)
req.Header.Set(headerName, jwtFail)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, "user_id not found\n", string(b))
}
}