mirror of
https://forgejo.merr.is/annika/jwtauth.git
synced 2025-12-11 13:47:41 -05:00
Update formatting in README
This commit is contained in:
parent
d62dd5080d
commit
464e20f997
1 changed files with 25 additions and 23 deletions
48
README.md
48
README.md
|
|
@ -33,7 +33,8 @@ all unverified tokens, see jwtauth.Authenticator.
|
||||||
package myservice
|
package myservice
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/dgrijalva/jwt-go"
|
"net/http"
|
||||||
|
|
||||||
"github.com/goware/jwtauth"
|
"github.com/goware/jwtauth"
|
||||||
"github.com/pressly/chi"
|
"github.com/pressly/chi"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
|
|
@ -42,37 +43,38 @@ import (
|
||||||
var TokenAuth *jwtauth.JwtAuth
|
var TokenAuth *jwtauth.JwtAuth
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
TokenAuth = jwtauth.New("HS256", []byte("secret"), nil)
|
TokenAuth = jwtauth.New("HS256", []byte("secret"), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func router() http.Handler {
|
func router() http.Handler {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
|
|
||||||
// Protected routes
|
// Protected routes
|
||||||
r.Group(func(r chi.Router) {
|
r.Group(func(r chi.Router) {
|
||||||
// Seek, verify and validate JWT tokens
|
// Seek, verify and validate JWT tokens
|
||||||
r.Use(TokenAuth.Verifier)
|
r.Use(TokenAuth.Verifier)
|
||||||
|
|
||||||
// Handle valid / invalid tokens. In this example, we use
|
// Handle valid / invalid tokens. In this example, we use
|
||||||
// the provided authenticator middleware, but you can write your
|
// the provided authenticator middleware, but you can write your
|
||||||
// own very easily, look at the Authenticator method in jwtauth.go
|
// own very easily, look at the Authenticator method in jwtauth.go
|
||||||
// and tweak it, its not scary.
|
// and tweak it, its not scary.
|
||||||
r.Use(jwtauth.Authenticator)
|
r.Use(jwtauth.Authenticator)
|
||||||
|
|
||||||
r.Get("/admin", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
|
r.Get("/admin", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("protected"))
|
w.Write([]byte("protected"))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// Public routes
|
// Public routes
|
||||||
r.Group(func(r chi.Router) {
|
r.Group(func(r chi.Router) {
|
||||||
r.Get("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
|
r.Get("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("welcome"))
|
w.Write([]byte("welcome"))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
# LICENSE
|
# LICENSE
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue