Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
62 lines (51 sloc) 1.53 KB
package main
import (
"fmt"
"time"
jwt "github.com/dgrijalva/jwt-go"
"github.com/dgrijalva/jwt-go/request"
"github.com/gin-gonic/gin"
)
var secretKey = "75c92a074c341e9964329c0550c2673730ed8479c885c43122c90a2843177d5ef21cb50cfadcccb20aeb730487c11e09ee4dbbb02387242ef264e74cbee97213"
func main() {
r := gin.Default()
r.GET("/api/", func(c *gin.Context) {
/*
アルゴリズムの指定
*/
token := jwt.New(jwt.GetSigningMethod("HS256"))
//token := jwt.New(jwt.GetSigningMethod("none")) // https://github.com/dgrijalva/jwt-go/pull/79/files
token.Claims = jwt.MapClaims{
"user": "ゲスト",
"exp": time.Now().Add(time.Hour * 1).Unix(),
}
/*
トークンに対して署名の付与
*/
tokenString, err := token.SignedString([]byte(secretKey))
//tokenString, err := token.SignedString(jwt.UnsafeAllowNoneSignatureType)
if err == nil {
c.JSON(200, gin.H{"token": tokenString})
} else {
c.JSON(500, gin.H{"message": "Could not generate token"})
}
})
r.GET("/api/private/", func(c *gin.Context) {
/*
署名の検証
*/
token, err := request.ParseFromRequest(c.Request, request.OAuth2Extractor, func(token *jwt.Token) (interface{}, error) {
b := []byte(secretKey)
//b := jwt.UnsafeAllowNoneSignatureType
return b, nil
})
if err == nil {
claims := token.Claims.(jwt.MapClaims)
msg := fmt.Sprintf("こんにちは、「 %s 」さん", claims["user"])
c.JSON(200, gin.H{"message": msg})
} else {
c.JSON(401, gin.H{"error": fmt.Sprint(err)})
}
})
r.Run(":8080")
}