-
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.
- Loading branch information
Showing
5 changed files
with
109 additions
and
0 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,57 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/golang-jwt/jwt" | ||
"github.com/zjregee/anet/ahttp" | ||
) | ||
|
||
var ErrJWTMissing = ahttp.NewHTTPError(http.StatusBadRequest, "missing or malformed jwt") | ||
var ErrJWTInvalid = ahttp.NewHTTPError(http.StatusUnauthorized, "invalid or expired jwt") | ||
|
||
type JWTConfig struct { | ||
SigningKey interface{} | ||
SigningMethod string | ||
ContextKey string | ||
Claims jwt.Claims | ||
TokenLookup string | ||
AuthScheme string | ||
} | ||
|
||
var DefaultJWTConfig = JWTConfig{ | ||
SigningMethod: "HS256", | ||
ContextKey: "user", | ||
Claims: jwt.MapClaims{}, | ||
TokenLookup: "header:Authorization", | ||
AuthScheme: "Bearer", | ||
} | ||
|
||
func JWT(key interface{}) ahttp.MiddlewareFunc { | ||
if key == nil { | ||
panic("shouldn't failed here") | ||
} | ||
config := DefaultJWTConfig | ||
config.SigningKey = key | ||
return func(next ahttp.HandlerFunc) ahttp.HandlerFunc { | ||
return func(c *ahttp.Context) error { | ||
extractor := valuesFromHeader("Authorization", config.AuthScheme) | ||
auths, err := extractor(c) | ||
if err != nil { | ||
return ErrJWTMissing.SetInternal(err) | ||
} | ||
auth := auths[0] | ||
token, err := jwt.Parse(auth, func(token *jwt.Token) (interface{}, error) { | ||
return config.SigningKey, nil | ||
}) | ||
if err != nil { | ||
return ErrJWTInvalid.SetInternal(err) | ||
} | ||
if !token.Valid { | ||
return ErrJWTInvalid | ||
} | ||
c.Set(config.ContextKey, token) | ||
return next(c) | ||
} | ||
} | ||
} |
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,36 @@ | ||
package middleware | ||
|
||
import ( | ||
"errors" | ||
"net/textproto" | ||
"strings" | ||
|
||
"github.com/zjregee/anet/ahttp" | ||
) | ||
|
||
type valuesExtractor func(c *ahttp.Context) ([]string, error) | ||
|
||
func valuesFromHeader(header string, valuePrefix string) valuesExtractor { | ||
prefixLen := len(valuePrefix) | ||
header = textproto.CanonicalMIMEHeaderKey(header) | ||
return func(c *ahttp.Context) ([]string, error) { | ||
values := c.Request().Header.Values(header) | ||
if len(values) == 0 { | ||
return nil, errors.New("header not found") | ||
} | ||
result := make([]string, 0) | ||
for _, value := range values { | ||
if prefixLen == 0 { | ||
result = append(result, value) | ||
continue | ||
} | ||
if len(value) > prefixLen && strings.EqualFold(value[:prefixLen], valuePrefix) { | ||
result = append(result, value[prefixLen:]) | ||
} | ||
} | ||
if len(result) == 0 { | ||
return nil, errors.New("value not found") | ||
} | ||
return result, 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
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