-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from domsolutions/use-generics
Use generics and fix bug of jwt count
- Loading branch information
Showing
11 changed files
with
161 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash | ||
|
||
GOOS=windows go build -o gopayloader-windows-amd64.exe ./ | ||
GOOS=linux go build -o gopayloader-linux-amd64 ./ | ||
GOOS=darwin go build -o gopayloader-darwin-amd64 ./ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,105 @@ | ||
package jwt_signer | ||
|
||
import ( | ||
"crypto" | ||
"crypto/ecdsa" | ||
"crypto/rsa" | ||
"errors" | ||
"github.com/domsolutions/gopayloader/pkgs/jwt-signer/definition" | ||
"github.com/domsolutions/gopayloader/pkgs/jwt-signer/ecdsa" | ||
ed25519 "github.com/domsolutions/gopayloader/pkgs/jwt-signer/ed25519" | ||
"github.com/domsolutions/gopayloader/pkgs/jwt-signer/rsa256" | ||
rsa512 "github.com/domsolutions/gopayloader/pkgs/jwt-signer/rsa512" | ||
"github.com/golang-jwt/jwt" | ||
"github.com/pterm/pterm" | ||
) | ||
|
||
type Signer func(privKey []byte, kid string) (definition.Signer, error) | ||
func CreateSigner(privKey []byte, kid string) (definition.Signer, error) { | ||
var signer definition.Signer | ||
|
||
// TODO add more signers, use generics? | ||
signer, err := createSigner[*ecdsa.PrivateKey](privKey, kid, jwt.ParseECPrivateKeyFromPEM, jwt.SigningMethodES256) | ||
if err == nil { | ||
return signer, nil | ||
} | ||
signer, err = createSigner[*ecdsa.PrivateKey](privKey, kid, jwt.ParseECPrivateKeyFromPEM, jwt.SigningMethodES384) | ||
if err == nil { | ||
return signer, nil | ||
} | ||
signer, err = createSigner[*ecdsa.PrivateKey](privKey, kid, jwt.ParseECPrivateKeyFromPEM, jwt.SigningMethodES512) | ||
if err == nil { | ||
return signer, nil | ||
} | ||
signer, err = createSigner[crypto.PrivateKey](privKey, kid, jwt.ParseEdPrivateKeyFromPEM, jwt.SigningMethodEdDSA) | ||
if err == nil { | ||
return signer, nil | ||
} | ||
signer, err = createSigner[*rsa.PrivateKey](privKey, kid, jwt.ParseRSAPrivateKeyFromPEM, jwt.SigningMethodRS512) | ||
if err == nil { | ||
return signer, nil | ||
} | ||
signer, err = createSigner[*rsa.PrivateKey](privKey, kid, jwt.ParseRSAPrivateKeyFromPEM, jwt.SigningMethodRS256) | ||
if err == nil { | ||
return signer, nil | ||
} | ||
signer, err = createSigner[*rsa.PrivateKey](privKey, kid, jwt.ParseRSAPrivateKeyFromPEM, jwt.SigningMethodRS384) | ||
if err == nil { | ||
return signer, nil | ||
} | ||
|
||
var signers = []Signer{ecdsa.Signer, ed25519.Signer, rsa256.Signer, rsa512.Signer} | ||
signer, err = createSigner[[]byte](privKey, kid, func(key []byte) ([]byte, error) { | ||
return key, nil | ||
}, jwt.SigningMethodHS256) | ||
if err == nil { | ||
return signer, nil | ||
} | ||
|
||
func CreateSigner(privKey []byte, kid string) (definition.Signer, error) { | ||
var signer definition.Signer | ||
var err error | ||
for _, s := range signers { | ||
signer, err = s(privKey, kid) | ||
if err != nil { | ||
continue | ||
} | ||
signer, err = createSigner[[]byte](privKey, kid, func(key []byte) ([]byte, error) { | ||
return key, nil | ||
}, jwt.SigningMethodHS384) | ||
if err == nil { | ||
return signer, nil | ||
} | ||
|
||
signer, err = createSigner[[]byte](privKey, kid, func(key []byte) ([]byte, error) { | ||
return key, nil | ||
}, jwt.SigningMethodHS512) | ||
if err == nil { | ||
return signer, nil | ||
} | ||
|
||
return nil, errors.New("no supported jwt signer") | ||
} | ||
|
||
type signer struct { | ||
kid string | ||
privKey any | ||
method jwt.SigningMethod | ||
} | ||
|
||
func (e *signer) Generate(claims jwt.MapClaims) (string, error) { | ||
token := jwt.NewWithClaims(e.method, claims) | ||
token.Header["kid"] = e.kid | ||
|
||
t, err := token.SignedString(e.privKey) | ||
if err != nil { | ||
return "", err | ||
} | ||
return t, nil | ||
} | ||
|
||
func createSigner[k any](privKey []byte, kid string, genPrivKey func(key []byte) (k, error), method jwt.SigningMethod) (definition.Signer, error) { | ||
key, err := genPrivKey(privKey) | ||
if err != nil { | ||
pterm.Debug.Printf("Failed to parse private key %v", err) | ||
return nil, err | ||
} | ||
|
||
s := &signer{ | ||
kid: kid, | ||
privKey: key, | ||
method: method, | ||
} | ||
claim := make(jwt.MapClaims) | ||
claim["test"] = true | ||
if _, err := s.Generate(claim); err != nil { | ||
pterm.Debug.Printf("Failed to generate jwt %v", err) | ||
return nil, err | ||
} | ||
return s, nil | ||
} |
Oops, something went wrong.