Major refactor

This commit is contained in:
Melon 2023-10-29 12:28:21 +00:00
parent d1feee2ec3
commit 9a859eb2d7
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
13 changed files with 66 additions and 20 deletions

View File

@ -1,8 +1,8 @@
package auth
import (
"github.com/MrMelon54/mjwt"
"github.com/MrMelon54/mjwt/claims"
"github.com/1f349/mjwt"
"github.com/1f349/mjwt/claims"
"github.com/golang-jwt/jwt/v4"
"time"
)

View File

@ -3,13 +3,14 @@ package auth
import (
"crypto/rand"
"crypto/rsa"
"github.com/MrMelon54/mjwt"
"github.com/MrMelon54/mjwt/claims"
"github.com/1f349/mjwt"
"github.com/1f349/mjwt/claims"
"github.com/stretchr/testify/assert"
"testing"
)
func TestCreateAccessToken(t *testing.T) {
t.Parallel()
key, err := rsa.GenerateKey(rand.Reader, 2048)
assert.NoError(t, err)

View File

@ -1,8 +1,8 @@
package auth
import (
"github.com/MrMelon54/mjwt"
"github.com/MrMelon54/mjwt/claims"
"github.com/1f349/mjwt"
"github.com/1f349/mjwt/claims"
"github.com/golang-jwt/jwt/v4"
"time"
)

View File

@ -3,13 +3,14 @@ package auth
import (
"crypto/rand"
"crypto/rsa"
"github.com/MrMelon54/mjwt"
"github.com/MrMelon54/mjwt/claims"
"github.com/1f349/mjwt"
"github.com/1f349/mjwt/claims"
"github.com/stretchr/testify/assert"
"testing"
)
func TestCreateTokenPair(t *testing.T) {
t.Parallel()
key, err := rsa.GenerateKey(rand.Reader, 2048)
assert.NoError(t, err)

View File

@ -1,7 +1,7 @@
package auth
import (
"github.com/MrMelon54/mjwt"
"github.com/1f349/mjwt"
"github.com/golang-jwt/jwt/v4"
"time"
)

View File

@ -3,12 +3,13 @@ package auth
import (
"crypto/rand"
"crypto/rsa"
"github.com/MrMelon54/mjwt"
"github.com/1f349/mjwt"
"github.com/stretchr/testify/assert"
"testing"
)
func TestCreateRefreshToken(t *testing.T) {
t.Parallel()
key, err := rsa.GenerateKey(rand.Reader, 2048)
assert.NoError(t, err)

View File

@ -7,6 +7,7 @@ import (
)
func TestPermStorage_Set(t *testing.T) {
t.Parallel()
ps := NewPermStorage()
ps.Set("mjwt:test")
if _, ok := ps.values["mjwt:test"]; !ok {
@ -15,6 +16,7 @@ func TestPermStorage_Set(t *testing.T) {
}
func TestPermStorage_Clear(t *testing.T) {
t.Parallel()
ps := NewPermStorage()
ps.values["mjwt:test"] = struct{}{}
ps.Clear("mjwt:test")
@ -24,6 +26,7 @@ func TestPermStorage_Clear(t *testing.T) {
}
func TestPermStorage_Has(t *testing.T) {
t.Parallel()
ps := NewPermStorage()
assert.False(t, ps.Has("mjwt:test"))
ps.values["mjwt:test"] = struct{}{}
@ -31,6 +34,7 @@ func TestPermStorage_Has(t *testing.T) {
}
func TestPermStorage_OneOf(t *testing.T) {
t.Parallel()
o := NewPermStorage()
o.Set("mjwt:test")
o.Set("mjwt:test2")
@ -48,6 +52,7 @@ func TestPermStorage_OneOf(t *testing.T) {
}
func TestPermStorage_MarshalJSON(t *testing.T) {
t.Parallel()
ps := NewPermStorage()
ps.Set("mjwt:test")
ps.Set("mjwt:test2")
@ -57,6 +62,7 @@ func TestPermStorage_MarshalJSON(t *testing.T) {
}
func TestPermStorage_MarshalYAML(t *testing.T) {
t.Parallel()
ps := NewPermStorage()
ps.Set("mjwt:test")
ps.Set("mjwt:test2")

View File

@ -7,9 +7,9 @@ import (
"encoding/pem"
"flag"
"fmt"
"github.com/MrMelon54/mjwt"
"github.com/MrMelon54/mjwt/auth"
"github.com/MrMelon54/mjwt/claims"
"github.com/1f349/mjwt"
"github.com/1f349/mjwt/auth"
"github.com/1f349/mjwt/claims"
"github.com/golang-jwt/jwt/v4"
"github.com/google/subcommands"
"os"

2
go.mod
View File

@ -1,4 +1,4 @@
module github.com/MrMelon54/mjwt
module github.com/1f349/mjwt
go 1.19

View File

@ -32,6 +32,7 @@ func (t testClaims2) Valid() error {
func (t testClaims2) Type() string { return "testClaims2" }
func TestExtractClaims(t *testing.T) {
t.Parallel()
key, err := rsa.GenerateKey(rand.Reader, 2048)
assert.NoError(t, err)
@ -45,6 +46,7 @@ func TestExtractClaims(t *testing.T) {
}
func TestExtractClaimsFail(t *testing.T) {
t.Parallel()
key, err := rsa.GenerateKey(rand.Reader, 2048)
assert.NoError(t, err)

View File

@ -3,6 +3,8 @@ package mjwt
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"github.com/golang-jwt/jwt/v4"
"io"
"os"
@ -43,14 +45,25 @@ func NewMJwtSignerFromFileOrCreate(issuer, file string, random io.Reader, bits i
// NewMJwtSignerFromFile creates a new defaultMJwtSigner using the path of a
// rsa.PrivateKey file.
func NewMJwtSignerFromFile(issuer, file string) (Signer, error) {
// read file
raw, err := os.ReadFile(file)
if err != nil {
return nil, err
}
key, err := x509.ParsePKCS1PrivateKey(raw)
// decode pem block
block, _ := pem.Decode(raw)
if block == nil || block.Type != "RSA PRIVATE KEY" {
return nil, fmt.Errorf("invalid rsa private key pem block")
}
// parse private key from pem block
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
// create signer using rsa.PrivateKey
return NewMJwtSigner(issuer, key), nil
}
@ -93,12 +106,26 @@ func readOrCreatePrivateKey(file string, random io.Reader, bits int) (*rsa.Priva
return nil, err
}
keyBytes := pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
})
if err != nil {
return nil, err
}
// write the key to the file
err = os.WriteFile(file, x509.MarshalPKCS1PrivateKey(key), 0600)
err = os.WriteFile(file, keyBytes, 0600)
return key, err
} else {
// decode pem block
block, _ := pem.Decode(f)
if block == nil || block.Type != "RSA PRIVATE KEY" {
return nil, fmt.Errorf("invalid rsa private key pem block")
}
// try to parse the private key
return x509.ParsePKCS1PrivateKey(f)
return x509.ParsePKCS1PrivateKey(block.Bytes)
}
}

View File

@ -4,23 +4,27 @@ import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"github.com/stretchr/testify/assert"
"os"
"testing"
)
func TestNewMJwtSigner(t *testing.T) {
t.Parallel()
key, err := rsa.GenerateKey(rand.Reader, 2048)
assert.NoError(t, err)
NewMJwtSigner("Test", key)
}
func TestNewMJwtSignerFromFile(t *testing.T) {
t.Parallel()
tempKey, err := os.CreateTemp("", "key-test-*.pem")
assert.NoError(t, err)
key, err := rsa.GenerateKey(rand.Reader, 2048)
assert.NoError(t, err)
_, err = tempKey.Write(x509.MarshalPKCS1PrivateKey(key))
b := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
_, err = tempKey.Write(b)
assert.NoError(t, err)
assert.NoError(t, tempKey.Close())
signer, err := NewMJwtSignerFromFile("Test", tempKey.Name())
@ -33,6 +37,7 @@ func TestNewMJwtSignerFromFile(t *testing.T) {
}
func TestNewMJwtSignerFromFileOrCreate(t *testing.T) {
t.Parallel()
tempKey, err := os.CreateTemp("", "key-test-*.pem")
assert.NoError(t, err)
assert.NoError(t, tempKey.Close())
@ -41,15 +46,17 @@ func TestNewMJwtSignerFromFileOrCreate(t *testing.T) {
assert.NoError(t, err)
signer2, err := NewMJwtSignerFromFileOrCreate("Test", tempKey.Name(), rand.Reader, 2048)
assert.NoError(t, err)
assert.True(t, signer.(*defaultMJwtSigner).key.Equal(signer2.(*defaultMJwtSigner).key))
assert.True(t, signer.PrivateKey().Equal(signer2.PrivateKey()))
}
func TestReadOrCreatePrivateKey(t *testing.T) {
t.Parallel()
tempKey, err := os.CreateTemp("", "key-test-*.pem")
assert.NoError(t, err)
key, err := rsa.GenerateKey(rand.Reader, 2048)
assert.NoError(t, err)
_, err = tempKey.Write(x509.MarshalPKCS1PrivateKey(key))
b := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
_, err = tempKey.Write(b)
assert.NoError(t, err)
assert.NoError(t, tempKey.Close())
key2, err := readOrCreatePrivateKey(tempKey.Name(), rand.Reader, 2048)

View File

@ -12,6 +12,7 @@ import (
)
func TestNewMJwtVerifierFromFile(t *testing.T) {
t.Parallel()
key, err := rsa.GenerateKey(rand.Reader, 2048)
assert.NoError(t, err)