-
Notifications
You must be signed in to change notification settings - Fork 0
/
fiberinterceptor_test.go
170 lines (156 loc) · 7.24 KB
/
fiberinterceptor_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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package cidaasinterceptor
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/assert"
)
func TestNewFiberInterceptor_Error(t *testing.T) {
interceptor, err := NewFiberInterceptor(Options{})
assert.Error(t, err)
assert.Nil(t, interceptor)
}
func TestNewFiberInterceptor_Success(t *testing.T) {
jwks, _ := createJwksKeys(t, nil)
uri, _, close := createWellKnownMockServer(jwks)
defer close()
interceptor, err := NewFiberInterceptor(Options{BaseURI: uri})
assert.NoError(t, err)
assert.NotNil(t, interceptor)
}
func TestFiberInterceptor_VerifyTokenBySignature_NoToken(t *testing.T) {
jwks, _ := createJwksKeys(t, nil)
uri, _, close := createWellKnownMockServer(jwks)
defer close()
interceptor, err := NewFiberInterceptor(Options{BaseURI: uri})
assert.NoError(t, err)
assert.NotNil(t, interceptor)
res := doFiberRequest(t, "", interceptor.VerifyTokenBySignature(SecurityOptions{Scopes: []string{"profile", "cidaas:compromissed_credentials"}}))
assert.Equal(t, http.StatusUnauthorized, res.StatusCode)
}
func TestFiberInterceptor_VerifyTokenBySignature_InvalidToken(t *testing.T) {
jwks, _ := createJwksKeys(t, nil)
uri, _, close := createWellKnownMockServer(jwks)
defer close()
token := createToken(t, nil, uri, false)
interceptor, err := NewFiberInterceptor(Options{BaseURI: uri})
assert.NoError(t, err)
assert.NotNil(t, interceptor)
res := doFiberRequest(t, token, interceptor.VerifyTokenBySignature(SecurityOptions{Scopes: []string{"profile", "cidaas:compromissed_credentials"}}))
assert.Equal(t, http.StatusUnauthorized, res.StatusCode)
}
func TestFiberInterceptor_VerifyTokenBySignature_Success(t *testing.T) {
jwks, pk := createJwksKeys(t, nil)
uri, _, close := createWellKnownMockServer(jwks)
defer close()
token := createToken(t, pk, uri, false)
interceptor, err := NewFiberInterceptor(Options{BaseURI: uri})
assert.NoError(t, err)
res := doFiberRequest(t, token, interceptor.VerifyTokenBySignature(SecurityOptions{Scopes: []string{"profile", "cidaas:compromissed_credentials"}}))
var tokenData TokenData
json.NewDecoder(res.Body).Decode(&tokenData)
assert.Equal(t, "clientTest", tokenData.Aud, "Aud in tokenData should be passed as context in request and be equal")
assert.Equal(t, "sub", tokenData.Sub, "Sub in tokenData should be passed as context in request and be equal")
assert.Equal(t, http.StatusOK, res.StatusCode, "handler should return 200 status code")
}
func TestFiberInterceptor_VerifyTokenBySignature_SuccessAfterKeyNotFound(t *testing.T) {
calls := 0
jwks, pk := createJwksKeys(t, nil)
var mockServer *httptest.Server
mockHandler := http.NewServeMux()
mockHandler.HandleFunc("/.well-known/openid-configuration", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(cidaasEndpoints{
JwksURI: fmt.Sprintf("%v/.well-known/jwks.json", mockServer.URL),
})
})
mockHandler.HandleFunc("/.well-known/jwks.json", func(w http.ResponseWriter, r *http.Request) {
calls++
if calls == 1 {
json.NewEncoder(w).Encode(jwks)
return
}
jwks.Keys[0].Kid = "newKey"
fmt.Println(jwks)
json.NewEncoder(w).Encode(jwks)
})
mockServer = httptest.NewServer(mockHandler)
defer mockServer.Close()
token := createToken(t, pk, mockServer.URL, false, "newKey")
interceptor, err := NewFiberInterceptor(Options{BaseURI: mockServer.URL})
assert.NoError(t, err)
var tokenData TokenData
// first call
res := doFiberRequest(t, token, interceptor.VerifyTokenBySignature(SecurityOptions{Scopes: []string{"profile", "cidaas:compromissed_credentials"}}))
json.NewDecoder(res.Body).Decode(&tokenData)
assert.Equal(t, "clientTest", tokenData.Aud, "Aud in tokenData should be passed as context in request and be equal")
assert.Equal(t, "sub", tokenData.Sub, "Sub in tokenData should be passed as context in request and be equal")
assert.Equal(t, http.StatusOK, res.StatusCode, "handler should return 200 status code")
// second call ensures that the cache of the keys has been updated correctly
res = doFiberRequest(t, token, interceptor.VerifyTokenBySignature(SecurityOptions{Scopes: []string{"profile", "cidaas:compromissed_credentials"}}))
json.NewDecoder(res.Body).Decode(&tokenData)
assert.Equal(t, "clientTest", tokenData.Aud, "Aud in tokenData should be passed as context in request and be equal")
assert.Equal(t, "sub", tokenData.Sub, "Sub in tokenData should be passed as context in request and be equal")
assert.Equal(t, http.StatusOK, res.StatusCode, "handler should return 200 status code")
assert.Equal(t, 2, calls)
}
func TestFiberInterceptor_VerifyTokenByIntrospect_NoToken(t *testing.T) {
jwks, _ := createJwksKeys(t, nil)
uri, _, close := createWellKnownMockServer(jwks)
defer close()
interceptor, err := NewFiberInterceptor(Options{BaseURI: uri, Debug: true})
assert.NoError(t, err)
res := doFiberRequest(t, "", interceptor.VerifyTokenByIntrospect(SecurityOptions{Scopes: []string{"profile", "cidaas:compromissed_credentials"}}))
assert.Equal(t, http.StatusUnauthorized, res.StatusCode)
}
func TestFiberInterceptor_VerifyTokenByIntrospect_InvalidToken(t *testing.T) {
jwks, _ := createJwksKeys(t, nil)
uri, _, close := createWellKnownMockServer(jwks)
defer close()
token := createToken(t, nil, uri, false)
interceptor, err := NewFiberInterceptor(Options{BaseURI: uri, Debug: true})
assert.NoError(t, err)
introspectURI, closeIntrospectSrv := createIntrospectMockServer(introspectResponse{Active: false})
defer closeIntrospectSrv()
interceptor.endpoints = cidaasEndpoints{IntrospectionEndpoint: fmt.Sprintf("%v/introspect", introspectURI)}
res := doFiberRequest(t, token, interceptor.VerifyTokenByIntrospect(SecurityOptions{Scopes: []string{"profile", "cidaas:compromissed_credentials"}}))
assert.Equal(t, http.StatusUnauthorized, res.StatusCode)
}
func TestFiberInterceptor_VerifyTokenByIntrospect_Success(t *testing.T) {
jwks, pk := createJwksKeys(t, nil)
uri, _, close := createWellKnownMockServer(jwks)
defer close()
token := createToken(t, pk, uri, false)
interceptor, err := NewFiberInterceptor(Options{BaseURI: uri, Debug: true})
assert.NoError(t, err)
introspectURI, closeIntrospectSrv := createIntrospectMockServer(introspectResponse{Active: true, Iss: uri})
defer closeIntrospectSrv()
interceptor.endpoints = cidaasEndpoints{IntrospectionEndpoint: fmt.Sprintf("%v/introspect", introspectURI)}
res := doFiberRequest(t, token, interceptor.VerifyTokenByIntrospect(SecurityOptions{Scopes: []string{"profile", "cidaas:compromissed_credentials"}}))
var tokenData TokenData
json.NewDecoder(res.Body).Decode(&tokenData)
assert.Equal(t, http.StatusOK, res.StatusCode, "handler should return 200 status code")
}
func doFiberRequest(t *testing.T, token string, interceptorHandler func(*fiber.Ctx) error) *http.Response {
app := fiber.New()
app.Get("/check", interceptorHandler, checkHandler)
req, err := http.NewRequest(http.MethodGet, "/check", nil)
req.Header.Set("Authorization", fmt.Sprintf("bearer %v", token))
if err != nil {
t.Fatal(err)
}
res, err := app.Test(req, 60*1000)
if err != nil {
t.Fatal(err)
}
assert.Nil(t, err, "expected no error while calling endpoint")
return res
}
func checkHandler(ctx *fiber.Ctx) error {
token := ctx.Locals(FiberTokenDataKey)
tokenData := token.(TokenData)
_ = ctx.Status(fiber.StatusOK).JSON(tokenData)
return nil
}