mirror of
https://github.com/kataras/iris.git
synced 2026-01-08 20:41:57 +00:00
add a very simple example on JWT and move the previous to the 'overview' sub folder
This commit is contained in:
@@ -198,7 +198,9 @@
|
||||
* Authentication, Authorization & Bot Detection
|
||||
* [Basic Authentication](auth/basicauth/main.go)
|
||||
* [CORS](auth/cors)
|
||||
* [JWT](auth/jwt/main.go)
|
||||
* JSON Web Tokens
|
||||
* [Overview](auth/jwt/overview/main.go)
|
||||
* [Basic](auth/jwt/basic/main.go)
|
||||
* [Refresh Token](auth/jwt/refresh-token/main.go)
|
||||
* [JWT (community edition)](https://github.com/iris-contrib/middleware/tree/v12/jwt/_example/main.go)
|
||||
* [OAUth2](auth/goth/main.go)
|
||||
|
||||
44
_examples/auth/jwt/basic/main.go
Normal file
44
_examples/auth/jwt/basic/main.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/middleware/jwt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
// With AES-GCM (128) encryption:
|
||||
// j := jwt.HMAC(15*time.Minute, "secret", "itsa16bytesecret")
|
||||
// Without extra encryption, just the sign key:
|
||||
j := jwt.HMAC(15*time.Minute, "secret")
|
||||
|
||||
app.Get("/", generateToken(j))
|
||||
app.Get("/protected", j.VerifyMap(), protected)
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
func generateToken(j *jwt.JWT) iris.Handler {
|
||||
return func(ctx iris.Context) {
|
||||
token, err := j.Token(iris.Map{
|
||||
"foo": "bar",
|
||||
})
|
||||
if err != nil {
|
||||
ctx.StopWithStatus(iris.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.HTML(`Token: ` + token + `<br/><br/>
|
||||
<a href="/protected?token=` + token + `">/secured?token=` + token + `</a>`)
|
||||
}
|
||||
}
|
||||
|
||||
func protected(ctx iris.Context) {
|
||||
ctx.Writef("This is an authenticated request.\n\n")
|
||||
|
||||
claims := jwt.Get(ctx).(iris.Map)
|
||||
|
||||
ctx.Writef("foo=%s\n", claims["foo"])
|
||||
}
|
||||
Reference in New Issue
Block a user