Skip to content

Commit

Permalink
Start the addition of JWE feature
Browse files Browse the repository at this point in the history
  • Loading branch information
megablend committed Sep 15, 2023
1 parent c1296f8 commit 04f46e1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
32 changes: 29 additions & 3 deletions pkg/encrypt/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"gopkg.in/square/go-jose.v2/jwt"
)

// A Encrypt allows users to call the SignedToken method to provide token encryption
// or ParseToken to decrypt/parse a raw token base on chosen encryption type (JWT/JWE)
type Encrypt struct {
key *key.Key
}
Expand All @@ -29,6 +31,30 @@ type Param struct {
// It takes the parameter argument which provides details of the issuer, subject, headers and claims
// As part of the parameters you are required to provide the encryption type whihc is either JWT or JWE
func (e *Encrypt) SignedToken(param *Param) (string, error) {
switch param.EncyrptionType {
case key.JWT: // sign JWT related tokens
return e.signJWT(param)
case key.JWE: // sign JWE related tokens
panic("nothing exists for JWE")
default:
panic("invalid encryption type provided")

Check warning on line 40 in pkg/encrypt/encrypt.go

View check run for this annotation

Codecov / codecov/patch

pkg/encrypt/encrypt.go#L37-L40

Added lines #L37 - L40 were not covered by tests
}
}

// ParseToken decrypts/parse the provided raw token signed using the configured RSA keys
func (e *Encrypt) ParseToken(token string, encyrptionType key.SignerType) (map[string]interface{}, error) {
switch encyrptionType {
case key.JWT: // parses JWT related tokens
return e.parseJWT(token)
case key.JWE: // parses JWE related tokens
panic("nothing exists for JWE")
default:
panic("invalid encryption type provided")

Check warning on line 52 in pkg/encrypt/encrypt.go

View check run for this annotation

Codecov / codecov/patch

pkg/encrypt/encrypt.go#L49-L52

Added lines #L49 - L52 were not covered by tests
}
}

// signJWT signs JWT related tokens
func (e *Encrypt) signJWT(param *Param) (string, error) {
// valdiate the details for the provided params
if valid, err := e.isValidParams(param); !valid {
return "", err
Expand Down Expand Up @@ -61,8 +87,7 @@ func (e *Encrypt) isValidParams(params *Param) (bool, error) {
return true, nil
}

// ParseToken decrypts/parse the provided raw token signed using the configured RSA keys
func (e *Encrypt) ParseToken(token string) (map[string]interface{}, error) {
func (e *Encrypt) parseJWT(token string) (map[string]interface{}, error) {
parsedToken, err := jwt.ParseSigned(token)
if err != nil {
return nil, err
Expand Down Expand Up @@ -116,7 +141,8 @@ func (e *Encrypt) buildClaims(param *Param, signer jose.Signer) jwt.Builder {
return builder.Claims(builderClaims).Claims(param.Claims)
}

// New returns a new encryption object
// New returns a new encryption object when provided a key instance
// with details of the RSA private and public keys
func New(key *key.Key) *Encrypt {
return &Encrypt{
key: key,
Expand Down
2 changes: 1 addition & 1 deletion pkg/encrypt/encrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestParseToken_shouldReturnDecryptedToken(t *testing.T) {
}

rawToken, signerErr := encrypt.SignedToken(params)
claims, err := encrypt.ParseToken(rawToken)
claims, err := encrypt.ParseToken(rawToken, key.JWT)

require.NoError(t, signerErr)
require.NoError(t, configErr)
Expand Down

0 comments on commit 04f46e1

Please sign in to comment.