Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a specific return type / type constraint to Keyfunc and SignedString #337

Open
Tracked by #335
oxisto opened this issue Aug 14, 2023 · 0 comments
Open
Tracked by #335
Labels
next The next iteration of development, currently `v6`

Comments

@oxisto
Copy link
Collaborator

oxisto commented Aug 14, 2023

Currently, Keyfunc expects to return an interface{} and SignedString expects a interface{} parameter. This often lead to confusing, what exactly these functions expect. We should therefore constrain the types of these functions. A possible solution could look like this:

diff --git a/token.go b/token.go
index c8ad7c7..644b15e 100644
--- a/token.go
+++ b/token.go
@@ -1,15 +1,24 @@
 package jwt

 import (
+	"crypto"
 	"encoding/base64"
 	"encoding/json"
 )

+type ParsingKey interface {
+	crypto.PublicKey | []uint8 | unsafeNoneMagicConstant
+}
+
+type SigningKey interface {
+	crypto.PrivateKey | []uint8 | unsafeNoneMagicConstant
+}
+
 // Keyfunc will be used by the Parse methods as a callback function to supply
 // the key for verification.  The function receives the parsed, but unverified
 // Token.  This allows you to use properties in the Header of the token (such as
 // `kid`) to identify which key to use.
-type Keyfunc func(*Token) (interface{}, error)
+type Keyfunc func(*Token) (ParsingKey, error)

 // Token represents a JWT Token.  Different fields will be used depending on
 // whether you're creating or parsing/verifying a token.
@@ -46,7 +55,7 @@ func NewWithClaims(method SigningMethod, claims Claims, opts ...TokenOption) *To
 // https://golang-jwt.github.io/jwt/usage/signing_methods/#signing-methods-and-key-types
 // for an overview of the different signing methods and their respective key
 // types.
-func (t *Token) SignedString(key interface{}) (string, error) {
+func (t *Token) SignedString(key SigningKey) (string, error) {
 	sstr, err := t.SigningString()
 	if err != nil {
 		return "", err
@oxisto oxisto mentioned this issue Aug 14, 2023
10 tasks
@oxisto oxisto added the next The next iteration of development, currently `v6` label Sep 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
next The next iteration of development, currently `v6`
Projects
None yet
Development

No branches or pull requests

1 participant