36 lines
642 B
Go
36 lines
642 B
Go
package serve
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt"
|
|
)
|
|
|
|
const jwtExpiresAt = time.Hour
|
|
|
|
type Claims struct {
|
|
Token string `json:"token"`
|
|
jwt.StandardClaims
|
|
}
|
|
|
|
var (
|
|
jwtKey = []byte("my-secret")
|
|
)
|
|
|
|
func ValidateJwtCookie(r *http.Request) (*jwt.Token, error) {
|
|
cookie, err := r.Cookie("Authorize")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
token, err := jwt.Parse(cookie.Value, func(token *jwt.Token) (interface{}, error) {
|
|
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
|
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
|
}
|
|
return jwtKey, nil
|
|
})
|
|
|
|
return token, err
|
|
}
|