Code Monkey home page Code Monkey logo

gwt's Introduction

README

What is this repository for?

This is Golang lbrary to manage JWT tokens, with this you can generate a token, validate a token and check if the token has a specific prmission

How do I get set up?

Then run go get command to download your library

go get -u github.com/mudphilo/gwt
go mod vendor

Setup environment variables (Secret key)

This library will read secret key from the environment variable name

JWT_SECRET=SEcret Key

Setup the below envronment variables if you want to generate a token

JWT_ISSUER=Token issuer
JWT_DURATION_HOURS=token-lifetime-in-hour-must-be-int

This needs to be setup in your application, the key must be the same as the one used to generate the token

jwtSecret := os.Getenv("JWT_SECRET")

Generate a token

The function needs

  • user ID
  • user name
  • Assigned roles and permissions
package mytest

import (
	tokenutils "github.com/mudphilo/gwt"
	"fmt"
)

func GenerateToken() {

	// create all permissions the user has
	var permissions []tokenutils.Permission
	permissions = append(permissions, tokenutils.Permission{
		Module:  "user",
		Actions: []string{"create", "read"},
	})

	permissions = append(permissions, tokenutils.Permission{
		Module:  "driver",
		Actions: []string{"read"},
	})
	username := "jondoe"
	userID := 1
	userStatus := 1

	token, err := tokenutils.CreateToken(userID, userStatus, username, tokenutils.Role{
		Name:       "admin",
		Permission: permissions,
	})

	if err != nil {

		fmt.Printf("invalid token got error %s", err.Error())
		return
	}

	if len(token) == 0 {

		fmt.Printf("invalid token")

	} else {

		fmt.Printf("Got token" + token)
	}

}

Decoding a token

package mytest

import (
	tokenutils "github.com/mudphilo/gwt"
	"fmt"
)

func DecodeToken(token string) {

	claim, err := tokenutils.TokenValidation(token)
	if err != nil {
		fmt.Printf("invalid token")

	} else {
		fmt.Println(claim.UserId)
	}
}

Check if a token has the required permission

To check permissions we supply the token, module, action and scope

package mytest

import (
	tokenutils "github.com/mudphilo/gwt"
	"fmt"
)

func CheckPermission(token string) {

	statusCheck := tokenutils.HasPermission(token, "location", "read", "own")
	if statusCheck {

		fmt.Printf("Token does not have permission to read own location")

	} else {
		fmt.Printf("Token has permission to read own location")
	}
}

gwt's People

Contributors

philmudenyo avatar mudphilo avatar

Watchers

 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.