Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ken109 authored Feb 2, 2021
1 parent b2aa6a6 commit 4aa5e6c
Showing 1 changed file with 99 additions and 2 deletions.
101 changes: 99 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,104 @@
# Example
Click here for an implementation example.

https://github.com/ken109/gin-jwt-example

秘密鍵の発行
```
# Overview
1. Issuance of private key
```bash
openssl genrsa -out private.key 2048
```

3. Add Import
```go
import (
"github.com/ken109/gin-jwt"
)
```

2. Set private key, Issuer, etc.
```go
func main() {
pemBytes, err := ioutil.ReadFile("private.key")
if err != nil {
panic(err)
}

// here
err := jwt.SetUp(pemBytes, jwt.Option{
Issuer: "[email protected]",
Subject: "[email protected]",
KeyId: "test",
Expiration: time.Hour * 1,
})

if err != nil {
panic(err)
}

r := gin.New()

:
:
}
```

3. Issue a signed token
```go
func Login(c *gin.Context) {
user := "user"
password := "password"

if user == "user" && password == "password" {
// here
token, err := jwt.GetToken(jwt.Claims{
"admin": true,
})

if err != nil {
c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed"})
return
}

c.JSON(http.StatusOK, map[string]interface{}{"token": string(token)})
return
}

c.JSON(http.StatusForbidden, map[string]string{"error": "login failed"})
return
}
```

4. Set the middleware on the route you want to authenticate
```go
func main() {
:

auth := r.Group("/api")

// here
auth.Use(jwt.Verify)

:
}
```

5. Receive private claims
```go
func main() {
:

auth.Use(jwt.Verify)

auth.GET("/hello", func(c *gin.Context) {
// here
claims := jwt.GetClaims(c)

fmt.Println(claims["admin"].(bool)) // true

c.JSON(http.StatusOK, claims)
})

:
}
```

0 comments on commit 4aa5e6c

Please sign in to comment.