SermoDigital / jose

A comprehensive set of JWT, JWS, and JWE libraries.
MIT License
914 stars 87 forks source link

claims.SetExpiration undefined (type jws.Claims has no field or method SetExpiration) #56

Open NeleOb opened 1 year ago

NeleOb commented 1 year ago

Hi, I am trying to set exp. I used this code as inpsitarion: https://github.com/SermoDigital/jose/blob/master/jwt/claims_test.go I did:

go get github.com/SermoDigital/jose
package main

import (
    "fmt"
    "time"

    "github.com/SermoDigital/jose/jws"
)

func main() {
    // expires in 10 seconds
    expires := time.Now().Add(time.Duration(10) * time.Second)

         // set exp
    claims := jws.Claims{}
    claims.SetExpiration(expires)

    fmt.Println(claims)
}

But i get the following erros messages when i run the code.

claims.SetExpiration undefined (type jws.Claims has no field or method SetExpiration)

I am new to Go. Help is highly appreciated.

udayakumarvdm commented 1 year ago

Hi Team,

When I update the recent version I'm also get the same issue.

type jws.Claims has no field or method SetIssuedAt    
type jws.Claims has no field or method SetExpiration
type jwt.JWT has no field or method Validate
type jws.Claims has no field or method SetIssuedAt   
type jws.Claims has no field or method SetExpiration

Methods are available in the clone, But in go get not available. How can we resolve it please let us know ASAP.

 github.com/SermoDigital/jose v0.9.1

github.com/!sermo!digital/jose@v0.9.1/jws/jwt.go

  package jws

  import (
      "time"

      "github.com/SermoDigital/jose/crypto"
      "github.com/SermoDigital/jose/jwt"
  )

  // Claims represents a set of JOSE Claims.
  type Claims jwt.Claims

  // NewJWT creates a new JWT with the given claims.
  func NewJWT(claims Claims, method crypto.SigningMethod) jwt.JWT {
      j := New(claims, method)
      j.isJWT = true
      return j
  }

  // Serialize helps implements jwt.JWT.
  func (j *JWS) Serialize(key interface{}) ([]byte, error) {
      if j.isJWT {
          return j.Compact(key)
      }
      return nil, ErrIsNotJWT
  }

  // Claims helps implements jwt.JWT.
  func (j *JWS) Claims() jwt.Claims {
      if j.isJWT {
          if c, ok := j.payload.v.(Claims); ok {
              return jwt.Claims(c)
          }
      }
      return nil
  }

  // ParseJWT parses a serialized jwt.JWT into a physical jwt.JWT.
  // If its payload isn't a set of claims (or able to be coerced into
  // a set of claims) it'll return an error stating the
  // JWT isn't a JWT.
  func ParseJWT(encoded []byte) (jwt.JWT, error) {
      t, err := ParseCompact(encoded)
      if err != nil {
          return nil, err
      }
      c, ok := t.payload.v.(map[string]interface{})
      if !ok {
          return nil, ErrIsNotJWT
      }
      t.payload.v = Claims(c)
      t.isJWT = true
      return t, nil
  }

  // IsJWT returns true if the JWS is a JWT.
  func (j *JWS) IsJWT() bool { return j.isJWT }

  // Verify helps implement jwt.JWT.
  func (j *JWS) Verify(key interface{}, m crypto.SigningMethod, o ...jwt.Opts) error {
      if j.isJWT {
          if err := j.Validate(key, m); err != nil {
              return err
          }
          c, ok := j.payload.v.(Claims)
          if ok {
              var p jwt.Opts
              if len(o) > 0 {
                  p = o[0]
              }

              if p.Fn != nil {
                  if err := p.Fn(jwt.Claims(c)); err != nil {
                      return err
                  }
              }
              return jwt.Claims(c).Validate(time.Now().Unix(), p.EXP, p.NBF)
          }
      }
      return ErrIsNotJWT
  }

  // Opts represents some of the validation options.
  // It mimics jwt.Opts.
  type Opts struct {
      EXP int64 // EXPLeeway
      NBF int64 // NBFLeeway
      Fn  func(Claims) error
      _   struct{}
  }

  // C is shorthand for Convert(fn).
  func (o Opts) C() jwt.Opts { return o.Convert() }

  // Convert converts Opts into jwt.Opts.
  func (o Opts) Convert() jwt.Opts {
      p := jwt.Opts{
          EXP: o.EXP,
          NBF: o.NBF,
      }
      if o.Fn != nil {
          p.Fn = func(c jwt.Claims) error {
              return o.Fn(Claims(c))
          }
      }
      return p
  }

  var _ jwt.JWT = (*JWS)(nil)

Thanks

udayakumarvdm commented 1 year ago

Finally, I used the 1.1 version, Temporarily Problem resolved.

      go get github.com/SermoDigital/jose@1.1