-
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.
Signed-off-by: Sarah Funkhouser <[email protected]>
- Loading branch information
1 parent
433987e
commit daf522b
Showing
11 changed files
with
152 additions
and
11 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
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,53 @@ | ||
package echocontext | ||
|
||
import ( | ||
"context" | ||
|
||
echo "github.com/theopenlane/echox" | ||
) | ||
|
||
// EchoContextKey is the context key for the echo.Context | ||
var EchoContextKey = &ContextKey{"EchoContextKey"} | ||
|
||
// ContextKey is the key name for the additional context | ||
type ContextKey struct { | ||
name string | ||
} | ||
|
||
// CustomContext contains the echo.Context and request context.Context | ||
type CustomContext struct { | ||
echo.Context | ||
ctx context.Context | ||
} | ||
|
||
// EchoContextToContextMiddleware is the middleware that adds the echo.Context to the parent context | ||
func EchoContextToContextMiddleware() echo.MiddlewareFunc { | ||
return func(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(c echo.Context) error { | ||
ctx := context.WithValue(c.Request().Context(), EchoContextKey, c) | ||
|
||
c.SetRequest(c.Request().WithContext(ctx)) | ||
|
||
cc := &CustomContext{c, ctx} | ||
|
||
return next(cc) | ||
} | ||
} | ||
} | ||
|
||
// EchoContextFromContext gets the echo.Context from the parent context | ||
func EchoContextFromContext(ctx context.Context) (echo.Context, error) { | ||
// retrieve echo.Context from provided context | ||
echoContext := ctx.Value(EchoContextKey) | ||
if echoContext == nil { | ||
return nil, ErrUnableToRetrieveEchoContext | ||
} | ||
|
||
// type cast the context to ensure echo.Context | ||
ec, ok := echoContext.(echo.Context) | ||
if !ok { | ||
return ec, ErrUnableToRetrieveEchoContext | ||
} | ||
|
||
return ec, nil | ||
} |
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,2 @@ | ||
// Package echocontext adds the echo context to the parent context | ||
package echocontext |
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,10 @@ | ||
package echocontext | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
var ( | ||
// ErrUnableToRetrieveEchoContext is returned when the echo context is unable to be parsed from parent context | ||
ErrUnableToRetrieveEchoContext = errors.New("unable to retrieve echo context") | ||
) |
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,68 @@ | ||
package echocontext | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"time" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
|
||
echo "github.com/theopenlane/echox" | ||
) | ||
|
||
// NewTestEchoContext used for testing purposes ONLY | ||
func NewTestEchoContext() echo.Context { | ||
// create echo context | ||
e := echo.New() | ||
req := &http.Request{ | ||
Header: http.Header{}, | ||
} | ||
|
||
// Set writer for tests that write on the response | ||
recorder := httptest.NewRecorder() | ||
|
||
return e.NewContext(req, recorder) | ||
} | ||
|
||
func NewTestContext() context.Context { | ||
c := NewTestEchoContext() | ||
ctx := context.WithValue(c.Request().Context(), EchoContextKey, c) | ||
|
||
c.SetRequest(c.Request().WithContext(ctx)) | ||
|
||
return ctx | ||
} | ||
|
||
// newValidSignedJWT creates a jwt with a fake subject for testing purposes ONLY | ||
func newValidSignedJWT(subject string) (*jwt.Token, error) { | ||
iat := time.Now().Unix() | ||
nbf := time.Now().Unix() | ||
exp := time.Now().Add(time.Hour).Unix() | ||
|
||
claims := jwt.MapClaims{ | ||
"sub": subject, | ||
"issuer": "test suite", | ||
"iat": iat, | ||
"nbf": nbf, | ||
"exp": exp, | ||
} | ||
|
||
jwt := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) | ||
|
||
return jwt, nil | ||
} | ||
|
||
// NewTestContextWithValidUser creates an echo context with a fake subject for testing purposes ONLY | ||
func NewTestContextWithValidUser(subject string) (*echo.Context, error) { | ||
ec := NewTestEchoContext() | ||
|
||
j, err := newValidSignedJWT(subject) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ec.Set("user", j) | ||
|
||
return &ec, nil | ||
} |
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