github.com/openshift/installer@v1.4.17/pkg/asset/agent/gencrypto/auth_utils.go (about)

     1  package gencrypto
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/go-openapi/runtime"
     7  	"github.com/go-openapi/strfmt"
     8  	"github.com/golang-jwt/jwt/v4"
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  // UserAuthHeaderWriter sets the JWT authorization token.
    13  func UserAuthHeaderWriter(token string) runtime.ClientAuthInfoWriter {
    14  	return runtime.ClientAuthInfoWriterFunc(func(r runtime.ClientRequest, _ strfmt.Registry) error {
    15  		return r.SetHeaderParam("Authorization", token)
    16  	})
    17  }
    18  
    19  // ParseExpirationFromToken checks if the token is expired or not.
    20  func ParseExpirationFromToken(tokenString string) (time.Time, error) {
    21  	token, _, err := new(jwt.Parser).ParseUnverified(tokenString, jwt.MapClaims{})
    22  	if err != nil {
    23  		return time.Time{}, err
    24  	}
    25  	claims, ok := token.Claims.(jwt.MapClaims)
    26  	if !ok {
    27  		return time.Time{}, errors.Errorf("malformed token claims in url")
    28  	}
    29  	exp, ok := claims["exp"].(float64)
    30  	if !ok {
    31  		return time.Time{}, errors.Errorf("token missing 'exp' claim")
    32  	}
    33  	expTime := time.Unix(int64(exp), 0)
    34  	expiresAt := strfmt.DateTime(expTime)
    35  	expiryTime := time.Time(expiresAt)
    36  
    37  	return expiryTime, nil
    38  }