mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 02:17:05 +00:00
OK, I think we are done with the new JWT package
This commit is contained in:
@@ -2,4 +2,6 @@ module github.com/kataras/iris/_examples/dependency-injection/jwt/contrib
|
||||
|
||||
go 1.15
|
||||
|
||||
require github.com/iris-contrib/middleware/jwt v0.0.0-20200810001613-32cf668f999f
|
||||
require (
|
||||
github.com/iris-contrib/middleware/jwt v0.0.0-20201017024110-39b50ffeb885
|
||||
)
|
||||
|
||||
@@ -11,40 +11,59 @@ func main() {
|
||||
app := iris.New()
|
||||
app.ConfigureContainer(register)
|
||||
|
||||
// http://localhost:8080/authenticate
|
||||
// http://localhost:8080/restricted
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
func register(api *iris.APIContainer) {
|
||||
j := jwt.HMAC(15*time.Minute, "secret", "secretforencrypt")
|
||||
var (
|
||||
secret = []byte("secret")
|
||||
signer = jwt.NewSigner(jwt.HS256, secret, 15*time.Minute)
|
||||
verify = jwt.NewVerifier(jwt.HS256, secret, jwt.Expected{Issuer: "myapp"}).Verify(func() interface{} {
|
||||
return new(userClaims)
|
||||
})
|
||||
)
|
||||
|
||||
api.RegisterDependency(func(ctx iris.Context) (claims userClaims) {
|
||||
if err := j.VerifyToken(ctx, &claims); err != nil {
|
||||
ctx.StopWithError(iris.StatusUnauthorized, err)
|
||||
return
|
||||
func register(api *iris.APIContainer) {
|
||||
// To register the middleware in the whole api container:
|
||||
// api.Use(verify)
|
||||
// Otherwise, protect routes when userClaims is expected on the functions input
|
||||
// by calling the middleware manually, see below.
|
||||
api.RegisterDependency(func(ctx iris.Context) (claims *userClaims) {
|
||||
if ctx.Proceed(verify) { // the "verify" middleware will stop the execution if it's failed to verify the request.
|
||||
// Map the input parameter of "restricted" function with the claims.
|
||||
return jwt.Get(ctx).(*userClaims)
|
||||
}
|
||||
|
||||
return
|
||||
return nil
|
||||
})
|
||||
|
||||
api.Get("/authenticate", writeToken(j))
|
||||
api.Get("/authenticate", writeToken)
|
||||
api.Get("/restricted", restrictedPage)
|
||||
}
|
||||
|
||||
type userClaims struct {
|
||||
jwt.Claims
|
||||
Username string
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
func writeToken(j *jwt.JWT) iris.Handler {
|
||||
return func(ctx iris.Context) {
|
||||
j.WriteToken(ctx, userClaims{
|
||||
Claims: j.Expiry(jwt.Claims{Issuer: "an-issuer"}),
|
||||
Username: "kataras",
|
||||
})
|
||||
func writeToken(ctx iris.Context) {
|
||||
claims := userClaims{
|
||||
Username: "kataras",
|
||||
}
|
||||
standardClaims := jwt.Claims{
|
||||
Issuer: "myapp",
|
||||
}
|
||||
|
||||
token, err := signer.Sign(claims, standardClaims)
|
||||
if err != nil {
|
||||
ctx.StopWithError(iris.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Write(token)
|
||||
}
|
||||
|
||||
func restrictedPage(claims userClaims) string {
|
||||
func restrictedPage(claims *userClaims) string {
|
||||
// userClaims.Username: kataras
|
||||
return "userClaims.Username: " + claims.Username
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user