-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from tgunsch/jwt-handler
Jwt handler
- Loading branch information
Showing
11 changed files
with
239 additions
and
49 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
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 |
---|---|---|
@@ -1,18 +1,24 @@ | ||
module github.com/tgunsch/httpod | ||
|
||
go 1.15 | ||
go 1.16 | ||
|
||
require ( | ||
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 | ||
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect | ||
github.com/go-openapi/spec v0.20.2 // indirect | ||
github.com/labstack/echo/v4 v4.1.17 | ||
github.com/go-openapi/spec v0.20.3 // indirect | ||
github.com/go-openapi/swag v0.19.15 // indirect | ||
github.com/goccy/go-json v0.4.13 // indirect | ||
github.com/labstack/echo/v4 v4.2.2 | ||
github.com/lestrrat-go/backoff/v2 v2.0.8 // indirect | ||
github.com/lestrrat-go/jwx v1.1.7 | ||
github.com/mailru/easyjson v0.7.7 // indirect | ||
github.com/mattn/go-colorable v0.1.8 // indirect | ||
github.com/nxadm/tail v1.4.6 // indirect | ||
github.com/onsi/ginkgo v1.14.2 | ||
github.com/onsi/gomega v1.10.1 | ||
github.com/russross/blackfriday/v2 v2.1.0 // indirect | ||
github.com/swaggo/echo-swagger v1.1.0 | ||
github.com/swaggo/swag v1.7.0 | ||
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // indirect | ||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect | ||
golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6 // indirect | ||
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7 // indirect | ||
golang.org/x/tools v0.1.0 // indirect | ||
) |
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
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
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,42 @@ | ||
package jwt_test | ||
|
||
import ( | ||
"fmt" | ||
"github.com/labstack/echo/v4" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"github.com/tgunsch/httpod/internal/jwt" | ||
"net/http" | ||
"net/http/httptest" | ||
) | ||
|
||
var _ = Describe("GetHandler", func() { | ||
It("return a jwt", func() { | ||
|
||
ctx, _, responseRecorder := mockGetContext("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c") | ||
|
||
err := jwt.GetHandler(ctx) | ||
Expect(err).Should(BeNil()) | ||
|
||
// return 200 | ||
Expect(responseRecorder.Code).Should(Equal(200)) | ||
|
||
// response body contains json cookie | ||
Expect(responseRecorder.Body.String()).To(MatchJSON(`{ | ||
"iat": 1516239022, | ||
"name": "John Doe", | ||
"sub": "1234567890" | ||
}`)) | ||
}) | ||
|
||
}) | ||
|
||
func mockGetContext(token string) (echo.Context, *http.Request, *httptest.ResponseRecorder) { | ||
e := echo.New() | ||
req := httptest.NewRequest(http.MethodGet, "http://myapp.com/api/jwt", nil) | ||
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) | ||
req.Header.Set(echo.HeaderAuthorization, fmt.Sprintf("Bearer %s", token)) | ||
res := httptest.NewRecorder() | ||
c := e.NewContext(req, res) | ||
return c, req, res | ||
} |
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,38 @@ | ||
package jwt | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/labstack/echo/v4" | ||
"github.com/lestrrat-go/jwx/jwt" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
// @Summary Get jwt passed as authorization bearer token of the request. | ||
// @Tags JWT | ||
// @Description Requests using GET should only retrieve data. | ||
// @Accept json | ||
// @Produce json | ||
// @Success 200 {array} jwt.Token | ||
// @Router /jwt [get] | ||
func GetHandler(context echo.Context) error { | ||
|
||
auth := context.Request().Header.Get(echo.HeaderAuthorization) | ||
l := len("Bearer") | ||
if auth[:l] == "Bearer" { | ||
rawToken := auth[l+1:] | ||
token, err := jwt.ParseReader(strings.NewReader(rawToken)) | ||
if err != nil { | ||
return context.String(http.StatusBadRequest, fmt.Sprintf("failed to parse payload: %s\n", err)) | ||
} | ||
|
||
prettyJSON, err := json.MarshalIndent(token, "", " ") | ||
if err != nil { | ||
return context.String(http.StatusBadRequest, fmt.Sprintf("Error parsing cookies: %v", err.Error())) | ||
} | ||
return context.String(http.StatusOK, string(prettyJSON)) | ||
} | ||
return context.String(http.StatusBadRequest, "No JWT in request header") | ||
|
||
} |
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,13 @@ | ||
package jwt_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestJWT(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "JWT Suite") | ||
} |