-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(service): add Nonced middleware and add IsNonced
The Nonced middleware wraps a handler into NoncedHandlerFunc requiring any request to have a valid nonce or any condition that makes the service IsNonced false. The IsNonced method added to the service is a way to unblock requests from a handler or function to be allowed to proceed without a nonce validation. This method was added to allow a handler to have a request that will provide a new nonce. IsNonced can be used for requests where nonce is not necessary after a validation. Refs: #2
- Loading branch information
Showing
5 changed files
with
211 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package peasant | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
// Nonced is a middleware that verifies the presence of a valid nonce in a | ||
// request. | ||
// If the nonce is not provided or is invalid, it prevents the request from | ||
// proceeding. | ||
func Nonced(next http.Handler, s NonceService) http.Handler { | ||
return http.HandlerFunc(NoncedHandlerFunc(s, | ||
func(w http.ResponseWriter, r *http.Request) { | ||
next.ServeHTTP(w, r) | ||
}), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package peasant | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/candango/gopeasant/testrunner" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func NewNoncedServeMux(t *testing.T) http.Handler { | ||
s := &InMemoryNonceService{ | ||
nonceMap: make(map[string]*interface{}), | ||
t: t, | ||
} | ||
nonced := &NoncedHandler{ | ||
service: s, | ||
} | ||
h := http.NewServeMux() | ||
h.HandleFunc("/new-nonce", nonced.getNonce) | ||
h.HandleFunc("/do-nonced-something", nonced.doNoncedFunc) | ||
return Nonced(h, s) | ||
} | ||
|
||
func TestNoncedServer(t *testing.T) { | ||
handler := NewNoncedServeMux(t) | ||
runner := testrunner.NewHttpTestRunner(t).WithHandler(handler) | ||
|
||
t.Run("Retrieve a new nonce", func(t *testing.T) { | ||
t.Run("Request OK", func(t *testing.T) { | ||
res, err := runner.WithPath("/new-nonce").Head() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
assert.Equal(t, "200 OK", res.Status) | ||
assert.Equal(t, http.NoBody, res.Body) | ||
assert.Equal(t, 32, len(res.Header.Get("nonce"))) | ||
}) | ||
}) | ||
|
||
t.Run("Run a nonced function", func(t *testing.T) { | ||
t.Run("Request OK", func(t *testing.T) { | ||
res, err := runner.WithPath("/new-nonce").Head() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
nonce := res.Header.Get("nonce") | ||
|
||
res, err = runner.WithPath( | ||
"/do-nonced-something").WithHeader("nonce", nonce).Get() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
assert.Equal(t, "200 OK", res.Status) | ||
assert.Equal(t, "Func done with nonce "+nonce, | ||
testrunner.BodyAsString(t, res)) | ||
assert.Equal(t, 32, len(res.Header.Get("nonce"))) | ||
}) | ||
t.Run("Expired nonce", func(t *testing.T) { | ||
res, err := runner.WithPath("/new-nonce").Head() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
nonce := res.Header.Get("nonce") | ||
|
||
time.Sleep(250 * time.Millisecond) | ||
res, err = runner.WithPath( | ||
"/do-nonced-something").WithHeader("nonce", nonce).Get() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
assert.Equal(t, "403 Forbidden", res.Status) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.