Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add JWT bearer grant support for OAuth2 #287

Merged
merged 3 commits into from
Aug 17, 2021

Conversation

ldclakmal
Copy link
Member

@ldclakmal ldclakmal commented Aug 16, 2021

Purpose

This PR adds the JWT bearer grant support for OAuth2 according to the Section-2.1 of RFC 7523 [1].

   The following example demonstrates an access token request with a JWT
   as an authorization grant (with extra line breaks for display
   purposes only):

     POST /token.oauth2 HTTP/1.1
     Host: as.example.com
     Content-Type: application/x-www-form-urlencoded

     grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer
     &assertion=eyJhbGciOiJFUzI1NiIsImtpZCI6IjE2In0.
     eyJpc3Mi[...omitted for brevity...].
     J9l-ZhwP[...omitted for brevity...]

[1] https://datatracker.ietf.org/doc/html/rfc7523#section-2.1

Fixes ballerina-platform/ballerina-library#1716

Examples

import ballerina/oauth2;

public function main() {
    string jwt = "eyJhbGciOiJSUzI1NiIsICJ0eXAiOiJKV1QiLCAia2lkIjoiTXpZeE1tRmtPR1l3TVdJMFpXTm1ORGN4TkdZd1ltTTRaVEEzTV" +
                 "dJMk5EQXpaR1F6TkdNMFpHIn0.eyJpc3MiOiJodHRwczovL2xvY2FsaG9zdDo5NDQzL29hdXRoMi90b2tlbiIsICJzdWIiOiJh" +
                 "ZG1pbiIsICJhdWQiOiJodHRwczovL2xvY2FsaG9zdDo5NDQzL29hdXRoMi90b2tlbiIsICJleHAiOjE5NDQ0NzI2MjksICJuYm" +
                 "YiOjE2MjkxMTI2MjksICJpYXQiOjE2MjkxMTI2Mjl9.Qbi5kElPZlyViUUuYW9Ik4nXSeTIroacEDs4BoI0rAGAOBXfyWLW4Yx" +
                 "m6hAlb4GXtkPZ4YMO8c0mUgdXgvPVFqFYJuINNPu6Y_nExahAVD0VxCYRE59lEjRv7t_gqn5OxSu_jTGcgcHH8_j-tvL_-AHaq" +
                 "gflr5UljbTPtnQyXtLaPNeu3r7FoWs-LrewMPIm1aw5qc2gI2iYwI1jfIdpNlEjU6r_Mg6ou2D2AGqJa0QYN1FMqi4YJt2jHr6" +
                 "0tQMQIWJ7zhKU4ShZESxYOVKK_cBOeL6K-A07pNEZYaSxtCU3609MIZ8EOUJuQUJb7zHHxG4QziHM8eBwFo26yovBFw";
    oauth2:JwtBearerGrantConfig config = {
        tokenUrl: "https://localhost:9443/oauth2/token",
        assertion: jwt,
        clientId: "uDMwA4hKR9H3deeXxvNf4sSU0i4a",
        clientSecret: "8FOUOKUQfOp47pUfJCsPA5X4clga",
        scopes: ["view-order"],
        optionalParams: {
            "client": "ballerina"
        },
        clientConfig: {
            secureSocket: {
               cert: WSO2_PUBLIC_CERT_PATH
            }
        }
    };

    oauth2:ClientOAuth2Provider provider = new(config);
    string|oauth2:Error response = provider.generateToken();
}

Checklist

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

@codecov
Copy link

codecov bot commented Aug 16, 2021

Codecov Report

Merging #287 (6d8f982) into master (098da12) will decrease coverage by 3.44%.
The diff coverage is 56.33%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #287      +/-   ##
============================================
- Coverage     86.94%   83.49%   -3.45%     
  Complexity       34       34              
============================================
  Files             8        8              
  Lines           559      630      +71     
  Branches        204      233      +29     
============================================
+ Hits            486      526      +40     
- Misses           65       96      +31     
  Partials          8        8              
Impacted Files Coverage Δ
ballerina/client_oauth2_provider.bal 81.84% <56.33%> (-7.14%) ⬇️

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 098da12...6d8f982. Read the comment docs.

@ldclakmal ldclakmal marked this pull request as ready for review August 17, 2021 05:53
@ldclakmal ldclakmal merged commit d6dec5e into ballerina-platform:master Aug 17, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for JWT bearer grant in OAuth2
2 participants