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

Rs256 jwt signature verification #640

Merged

Conversation

ashutosh-narkar
Copy link
Member

@ashutosh-narkar ashutosh-narkar commented Mar 6, 2018

A builtin to verify RS256 JWT signatures
Issue - #421
Fixes #421

Copy link
Member

@tsandall tsandall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great. Just a few minor comments on the code.

var exp interface{}
exp = ast.String(p.result)
if p.err != "" {
exp = errors.New(p.err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to exercise this above?

}
token := string(astToken)
if !strings.Contains(token, ".") {
return nil, errors.New("encoded JWT has no period separators")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if this error case is useful since we split the string and check below. I would probably remove it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

err = rsa.VerifyPKCS1v15(publicKey, crypto.SHA256, getInputSHA(headerPayload), []byte(signature))

if err != nil {
return ast.String("RS256 JWT signature verification failed"), nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we're not returning the error here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning an error now


cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse PEM certificate %v", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See errors.Wrapf for returning wrapped errors. This way the embedded error formatting is more consistent. For example, I would use errors.Wrapf(err, "PEM parse error").

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used errors.Wrap

return base64.StdEncoding.DecodeString(data)
}

// encode encodes given byte array to base64url string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be replaced with base64.URLEncoding.EncodeToString?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it can. Do we want to ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, let's use the standard library implementation instead.

return hasher.Sum(nil)
}

// decode decodes base64url string to byte array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be replaced with base64.URLEncoding.Decode?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need conversion from string to []byte . So "Decode" won't help.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think base64.URLEncoding.DecodeString will work.

@ashutosh-narkar ashutosh-narkar changed the title Rs256 jwt signature verifcation Rs256 jwt signature verification. Fixes #421 Mar 6, 2018
@ashutosh-narkar ashutosh-narkar changed the title Rs256 jwt signature verification. Fixes #421 Rs256 jwt signature verification Mar 6, 2018
Fixes open-policy-agent#421

removed blank line

updated test

added command info documentation

wrap the error messages

used buitin URL decode method

moved verify token code in tokens module
@tsandall tsandall merged commit b4e7d08 into open-policy-agent:master Mar 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants