mirror of
https://github.com/stefanprodan/podinfo.git
synced 2026-05-23 09:52:46 +00:00
Migrate JWT to registered claims
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
This commit is contained in:
@@ -22,19 +22,19 @@ type TokenServer struct {
|
||||
|
||||
type jwtCustomClaims struct {
|
||||
Name string `json:"name"`
|
||||
jwt.StandardClaims
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
func (s *TokenServer) TokenGenerate(ctx context.Context, req *pb.TokenRequest) (*pb.TokenResponse, error) {
|
||||
|
||||
user := "anonymous"
|
||||
expiresAt := time.Now().Add(time.Minute * 1).Unix()
|
||||
expiresAt := time.Now().Add(time.Minute * 1)
|
||||
|
||||
claims := &jwtCustomClaims{
|
||||
user,
|
||||
jwt.StandardClaims{
|
||||
jwt.RegisteredClaims{
|
||||
Issuer: "podinfo",
|
||||
ExpiresAt: expiresAt,
|
||||
ExpiresAt: jwt.NewNumericDate(expiresAt),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ func (s *TokenServer) TokenGenerate(ctx context.Context, req *pb.TokenRequest) (
|
||||
|
||||
var result = pb.TokenResponse{
|
||||
Token: t,
|
||||
ExpiresAt: time.Unix(claims.StandardClaims.ExpiresAt, 0).String(),
|
||||
ExpiresAt: claims.ExpiresAt.Time.String(),
|
||||
Message: "Token generated successfully",
|
||||
}
|
||||
|
||||
@@ -88,12 +88,12 @@ func (s *TokenServer) TokenValidate(ctx context.Context, req *pb.TokenRequest) (
|
||||
}
|
||||
|
||||
if parsed_token.Valid {
|
||||
if claims.StandardClaims.Issuer != "podinfo" {
|
||||
if claims.Issuer != "podinfo" {
|
||||
return nil, status.Errorf(codes.OK, "Invalid issuer")
|
||||
} else {
|
||||
var result = pb.TokenResponse{
|
||||
Token: claims.Name,
|
||||
ExpiresAt: time.Unix(claims.StandardClaims.ExpiresAt, 0).String(),
|
||||
ExpiresAt: claims.ExpiresAt.Time.String(),
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
|
||||
type jwtCustomClaims struct {
|
||||
Name string `json:"name"`
|
||||
jwt.StandardClaims
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
// Token godoc
|
||||
@@ -44,9 +44,9 @@ func (s *Server) tokenGenerateHandler(w http.ResponseWriter, r *http.Request) {
|
||||
expiresAt := time.Now().Add(time.Minute * 1)
|
||||
claims := &jwtCustomClaims{
|
||||
user,
|
||||
jwt.StandardClaims{
|
||||
jwt.RegisteredClaims{
|
||||
Issuer: "podinfo",
|
||||
ExpiresAt: expiresAt.Unix(),
|
||||
ExpiresAt: jwt.NewNumericDate(expiresAt),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ func (s *Server) tokenGenerateHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var result = TokenResponse{
|
||||
Token: t,
|
||||
ExpiresAt: time.Unix(claims.StandardClaims.ExpiresAt, 0),
|
||||
ExpiresAt: claims.ExpiresAt.Time,
|
||||
}
|
||||
|
||||
s.JSONResponse(w, r, result)
|
||||
@@ -104,12 +104,12 @@ func (s *Server) tokenValidateHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if token.Valid {
|
||||
if claims.StandardClaims.Issuer != "podinfo" {
|
||||
if claims.Issuer != "podinfo" {
|
||||
s.ErrorResponse(w, r, span, "invalid issuer", http.StatusUnauthorized)
|
||||
} else {
|
||||
var result = TokenValidationResponse{
|
||||
TokenName: claims.Name,
|
||||
ExpiresAt: time.Unix(claims.StandardClaims.ExpiresAt, 0),
|
||||
ExpiresAt: claims.ExpiresAt.Time,
|
||||
}
|
||||
s.JSONResponse(w, r, result)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user