0

I am new to go. I am trying to set up a secure webserver using go and jwt, but I cannot get this to compile:

mw := jwtmiddleware.New(jwtmiddleware.Options{
        ValidationKeyGetter: func (token *jwt.Token) (interface{}, error) {
            return mySigningKey, nil
        },
        SigningMethod: jwt.SigningMethodHS256,
    })

but I only get the following error (from the compiler and the analyzer):

cannot use func literal (type func(*"webserver/vendor/github.com/dgrijalva/jwt-go".Token) (interface {}, error)) as type "github.com/dgrijalva/jwt-go".Keyfunc in field value   

I have tried making it a separate named func but that didn't help. The jwt library shows

type Keyfunc func(*Token) (interface{}, error)

so, it should match the signature. The code is the same in several online examples including auth0.com.

I have had success running simpler go programs, including serving a simple web page, so the compiler is working correctly.

Running on ubuntu 18.04 with the newest stable version of go and packages (downloaded and installed a few days ago). Thanks

Grokify
  • 15,092
  • 6
  • 60
  • 81
normcf
  • 21
  • 4
  • Your code is using a version of the jwt import from the vendor directory but it is expecting it to come from the un-vendored directory (note the difference in the error message type). This suggests to me that jwtmiddleware is not in your vendor directory. What do you use for dependency management? – Iain Duncan Oct 07 '18 at 06:19

1 Answers1

0

It seems the package was not in vendor/github.com. I did the following:

cp -R ../github.com/auth0 vendor/github.com/auth0

and it now compiles.

normcf
  • 21
  • 4
  • Do not do this, instead do a `dep ensure`, that will get the code to your vendor, and its usually a release type. If you've copied the code from go home, its usually master branch and isn't recommended for any stable codebase. – ishaan Oct 07 '18 at 06:42
  • I ran dep ensure and got no errors or, for that matter, output of any kind. I presume it saw the directory I copied and thought it was OK. I'm sure I didn't set things up perfectly, but it is working now. Thanks. – normcf Oct 08 '18 at 18:08