Git Product home page Git Product logo

cjwt's Introduction

cjwt

A C JWT Implementation

Build Status codecov.io Coverity Quality Gate Status Language Grade: C/C++ Apache V2 License GitHub release JWT.io

cjwt is a small JWT handler designed to allow consumers of JWTs of the JWS variant the ability to securely and easily get claims and data from a JWT. This particular JWT implementation uses cJSON and is designed to support multiple different crypto libraries in the future.

API

The API is meant to be fairly small & leverage what cJSON already provides nicely.

Here are the details

There are 3 function:

  • cjwt_decode() that decodes successfully or fails with a more detailed reason
  • cjwt_destroy() that destroys the cjwt_t object cleanly
  • cjwt_print() that prints the cjwt_t object to a stream (generally for debugging)

Otherwise you get a simple C struct to work with in your code.

Dependencies

Opinionated Default Secure

To help adopters not make costly security mistakes, cjwt tries to default to secure wherever possible. If you must use an insecure feature there are option flags that let you do so, but use them sparingly and with care.

Examples:

Inline

Using the decoder:

#include <stdint.h>
#include <stddef.h>
#include <string.h>

#include <cjwt/cjwt.h>

int main( int argc, char *argv[] )
{
    cjwt_t *jwt = NULL;
    cjwt_code_t rv;

    const char *hs_text = 
        /* header */
        "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
        /* payload */
        "eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaGVsbG8i"
        "OiJ3b3JsZCIsImJvYiI6WyJkb2ciLDEyM10sImNhdCI6eyJtb3VzZSI6eyJj"
        "aGVlc2UiOiJsb3RzIn19LCJpYXQiOjE1MTYyMzkwMjJ9."
        /* signature */
        "mJYSucD6RRg6zdPcSKvb5-LKFDJzRvdKqTlqAvDBknU";

    const char *hs_key = "hs256-secret";

    rv = cjwt_decode( hs_text, strlen(hs_text), 0, (uint8_t*) hs_key, strlen(hs_key), 0, 0, &jwt );
    if( CJWTE_OK != rv ) {
        printf( "There was an error processing the text: %d\n", rv );
        return -1;
    }

    cjwt_print( stdout, jwt );

    cjwt_destroy( jwt );

    return 0;
}

Gives you this output:

=====================
header
---------------------
   alg: HS256

payload
---------------------
   iat: 1516239022

   exp: NULL
   nbf: NULL

   iss: NULL
   sub: 1234567890
   jti: NULL
   aud: NULL

private claims
---------------------
{
     "name":     "John Doe",
     "hello":    "world",
     "bob":      ["dog", 123],
     "cat": {
         "mouse": {
             "cheese":   "lots"
         }
     }
}

Building and Testing Instructions

meson setup --warnlevel 3 --werror build
cd build
ninja all test coverage
firefox ./meson-logs/coveragereport/index.html

cjwt's People

Contributors

bill1600 avatar gbuddappagari avatar kraj avatar rgalga001c avatar roopaligalgali avatar schmidtw avatar shilpa24balaji avatar walkerstop avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.