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

Start the addition of JWE feature #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
"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 @@
// 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 @@
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 @@
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
Loading