Giter Site home page Giter Site logo

jwtkit's Introduction

JWTKit

Go Report Card

JWTKit implements a code generator which automatically generates go packages for jwt implementations for annotated structs. It focuses around generating API code for a giving struct which would contain all data to be attached to a giving jwt.Claims.

It generates all necessary code to issue jwt access and refresh tokens for use in your application.

Install

go get -u github.com/gokit/jwtkit

Examples

See Examples for demonstrations of jwtkit code generation.

Usage

Running the following commands instantly generates all necessary files and packages for giving code gen.

> jwit generate

How It works

Struct Annotation

You annotate any giving struct with @mongoapi which marks giving struct has a target for code generation.

Sample below:

// UserClaim embodies data to be attached with a jwt claim.
// @jwt(Contract => UserContract)
type UserClaim struct {
	User User `json:"user"`
	Roles map[string]string `json:"roles"`
	Permissions map[string]string `json:"permissions"`
}

// UserContract embodies the data needed for authenticating a user.
type UserContract struct{
	Username string `json:"user_name"`
	Password string `json:"password"`
}

JWTKit generates specific interfaces to allow extensibility and also what underline backend technology would be used for either storing jwt records and blacklist/active refresh tokens and associated records.

The JWTKit expects that the provided annotation must have a Contract attribute pointing to a local exported struct which contains expected data to be received to authenticate a login session by the user to authenticate itself for access to the jwt authorization system.

blacklist := mock.TokenBackend()
whitelist := mock.TokenBackend()
idb := mock.IdentityBackend()

jwter := userclaimjwt.NewJWTIdentity(userclaimjwt.JWTConfig{
	Maker:               noSecureUser,
	Signer:              "wellington",
	SigningSecret:       "All we want is to sign this",
	Method:              jwt.SigningMethodHS256,
	AccessTokenExpires:  700 * time.Millisecond,
	RefreshTokenExpires: 4 * time.Second,
}, blacklist, whitelist, idb)

var cred pkg.CreateUserSession
if jsnerr := json.Unmarshal([]byte(contractDataJSON), &cred); jsnerr != nil {
	panic(jsnerr)
}

auth, err := jwter.Grant(context.Background(), cred)
if err != nil {
	panic(jsnerr)
}

Where auth is:

{
	"expires": 323231,
	"refresh_token":"",
	"access_token":"",
	"token_type": "Bearer",
	"refresh_expires": 323231,
}

jwtkit's People

Contributors

influx6 avatar

Watchers

 avatar

Forkers

eric-greencomb

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.