Giter Site home page Giter Site logo

alexjmoya / go-jwt-tools Goto Github PK

View Code? Open in Web Editor NEW

This project forked from travelgatex/go-jwt-tools

0.0 0.0 0.0 53 KB

Golang authorization middleware for JWT tokens. JWT tools

Home Page: http://www.travelgatex.com

License: GNU General Public License v3.0

Go 100.00%

go-jwt-tools's Introduction

go-jwt-tools

Golang authorization middleware for JWT tokens. JWT tools (auth0 or other)

There are two important features on this package:

  • authorization.go contains a middleware that processes a token and checks its validity (authorizes).
  • permissions.go handles the "PermissionsTable" struct which contains the information of the JWT token conveniently adapted, and a set of functions to use it.

MiddleWare

How to use

We just need to add a call to the function Authorize on all the calls that must be authorized (in this case, we use a Route struct that contains the HandlerFunc and a bool indicating if that Route must be authorized). Authorize expects the handler function to wrap and a configuration object of type Config (defined on authorization.go file).

IMPORTANT: The middleware stores the PermissionTable item on the context, under the key defined on the ContextKey constant.

Example of use

func NewRouter() *mux.Router {
	router := mux.NewRouter().StrictSlash(true)

	// Prepare Authorization configuration
        c := authorization.Config{
		PublicKeyStr: "myKey",
		AdminGroup: "admin",
		IgnoreExpiration: false,
		TokenDummy: "TokenDummy",
	}

	for _, route := range routes {
		var handler http.Handler

		// Add Authorization or not
		if route.Authorization {
			handler = authorization.Authorize(route.HandlerFunc(), c)
		
		} else {
			handler = route.HandlerFunc()
		}

		handler = handlers.CompressHandler(util.CompressGzip(handler, route.GzipMandatory))

		router.
			Methods(route.Method).
			Path(route.Pattern).
			Name(route.Name).
			Handler(handler)
	}

	return router
}

After this, out PermissionTable will be stored on the ContextKey key of the context:

permissions := ctx.Value(authorization.ContextKey).(*authorization.PermissionTable)

Permissions

type Permissions interface {
	// CheckPermission returns the given permissions for a given product and object. Returns the special permissions applied on that object if any, and a boolean indicating if the user has the requested permission. NOTE: Special permissions returned can be filtered by the specials argument).
	CheckPermission(product string, object string, per string, specials ...string) ([]string, bool)
	// ValidGroups returns all the groups and its permissions that have any permission for the given product and object.
	ValidGroups(product string, object string, per string) map[string]bool
	// Returns all groups of a given type
	GetGroups(groupType string) []string
	// GetAllGroups returns the group hierarchy
	GetAllGroups() map[string]struct{}
	// GetGroupsByTypes returns a map indexed by group types, containing the list of groups of that type
	GetGroupsByTypes() map[string][]string
	// GetParents returns all the parent groups of a given group.
	GetParents(group string) map[string]interface{}
}

go-jwt-tools's People

Contributors

arquio avatar nicogonmu avatar mikimartin88 avatar guzmanthegood avatar francescjaume avatar alexjmoya avatar zechao 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.