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

Add HMAC signature support for JWT #307

Merged
merged 1 commit into from
Aug 9, 2021

Conversation

ldclakmal
Copy link
Member

@ldclakmal ldclakmal commented Aug 2, 2021

Purpose

This PR adds the HMAC signature support for JWT according to the Section-3.2 of RFC 7518 [1].

The following "alg" (algorithm) Header Parameter values are used to
indicate that the JWS Signature is an HMAC value computed using the
corresponding algorithm:

             +-------------------+--------------------+
             | "alg" Param Value | MAC Algorithm      |
             +-------------------+--------------------+
             | HS256             | HMAC using SHA-256 |
             | HS384             | HMAC using SHA-384 |
             | HS512             | HMAC using SHA-512 |
             +-------------------+--------------------+

[1] https://datatracker.ietf.org/doc/html/rfc7518#section-3.2

Examples

Sample Issuer Config

jwt:IssuerConfig issuerConfig = {
    username: "John",
    issuer: "wso2",
    audience: ["ballerina", "ballerinaSamples"],
    expTime: 600,
    signatureConfig: {
        algorithm: jwt:HS256,
        config: "s3cr3t"
    }
};

Sample Validator Config

jwt:ValidatorConfig validatorConfig = {
    issuer: "wso2",
    audience: ["ballerina", "ballerinaSamples"],
    clockSkew: 60,
    signatureConfig: {
        secret: "s3cr3t"
    }
};

Fixes ballerina-platform/ballerina-library#1645

Checklist

  • Linked to an issue
  • Updated the changelog
  • Added tests

@codecov
Copy link

codecov bot commented Aug 2, 2021

Codecov Report

Merging #307 (85a307f) into master (ced3626) will decrease coverage by 2.27%.
The diff coverage is 52.17%.

❗ Current head 85a307f differs from pull request most recent head 5f53d42. Consider uploading reports for the commit 5f53d42 to get more accurate results
Impacted file tree graph

@@             Coverage Diff              @@
##             master     #307      +/-   ##
============================================
- Coverage     82.19%   79.92%   -2.28%     
  Complexity       31       31              
============================================
  Files            11       11              
  Lines           691      752      +61     
  Branches        330      369      +39     
============================================
+ Hits            568      601      +33     
- Misses          116      144      +28     
  Partials          7        7              
Impacted Files Coverage Δ
ballerina/jwt_commons.bal 100.00% <ø> (ø)
ballerina/jwt_validator.bal 78.48% <44.73%> (-3.24%) ⬇️
ballerina/jwt_issuer.bal 81.45% <61.29%> (-4.12%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ced3626...5f53d42. Read the comment docs.

@shafreenAnfar shafreenAnfar merged commit 7035614 into ballerina-platform:master Aug 9, 2021
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.

[JWT] Add support for generating JWSs using HMAC based algorithms
3 participants