Skip to content

Commit

Permalink
Merge pull request #2 from domsolutions/use-generics
Browse files Browse the repository at this point in the history
Use generics and fix bug of jwt count
  • Loading branch information
domsolutions authored Apr 28, 2023
2 parents 461428d + 7d37c3f commit ace241f
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 198 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ server_*
*.qlog
gopayloader.exe
gopayloader.zip
gopayloader-darwin-amd64
gopayloader-linux-amd64
gopayloader-windows-amd64.exe
5 changes: 5 additions & 0 deletions build-release.sh
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 ./
14 changes: 8 additions & 6 deletions pkgs/jwt-generator/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ func newCache(f *os.File) (*cache, error) {
c.scanner = bufio.NewScanner(c.f)
c.scanner.Split(bufio.ScanLines)
if c.scanner.Scan() {
meta := c.scanner.Bytes()
if len(meta) < 8 {
return nil, fmt.Errorf("jwt_generator: corrupt jwt cache, wanted 8 bytes got %d", len(meta))
bb := make([]byte, 8)
_, err := f.ReadAt(bb, 0)
if err != nil {
return nil, err
}
c.count = int64(binary.LittleEndian.Uint64(meta[0:8]))

c.count = int64(binary.LittleEndian.Uint64(bb))
return &c, nil
}
return &c, nil
Expand Down Expand Up @@ -115,11 +115,13 @@ func (c *cache) save(tokens []string) error {
}

b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, uint64(int64(add)+c.count))
newCount := uint64(int64(add) + c.count)
binary.LittleEndian.PutUint64(b, newCount)
_, err = c.f.WriteAt(b, 0)
if err != nil {
return err
}

_, err = c.f.WriteAt([]byte{byte('\n')}, 9)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkgs/jwt-generator/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (j *JWTGenerator) Generate(reqJwtCount int64, dir string, retrying bool) er
return err
}
f.Close()
pterm.Error.Printf("jwt cache %s file corrupt, attempting to delete and recreate", fname)
pterm.Debug.Printf("jwt cache %s file corrupt, attempting to delete and recreate; got error; %v \n", fname, err)
if err := os.Remove(fname); err != nil {
pterm.Error.Printf("Couldn't remove cache file %s; %v", fname, err)
return err
Expand Down
44 changes: 0 additions & 44 deletions pkgs/jwt-signer/ecdsa/ecdsa.go

This file was deleted.

44 changes: 0 additions & 44 deletions pkgs/jwt-signer/ed25519/ed25519.go

This file was deleted.

44 changes: 0 additions & 44 deletions pkgs/jwt-signer/rsa256/rsa256.go

This file was deleted.

44 changes: 0 additions & 44 deletions pkgs/jwt-signer/rsa512/rsa512.go

This file was deleted.

105 changes: 90 additions & 15 deletions pkgs/jwt-signer/signer.go
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
}
Loading

0 comments on commit ace241f

Please sign in to comment.