jwt

package module
v0.0.0-...-df8131a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2016 License: MIT Imports: 8 Imported by: 0

README

JSON Web Token wrapper package for GoLang

Installation
go get github.com/ausrasul/jwt
Encryption key

On a linux based system, create a key pair to use for token encryption.

$ openssl genrsa -out privkey.pem 2048
$ openssl rsa -in privkey.pem -pubout -out pubkey.pem

Then use "privkey.pem" and "pubkey.pem" in the package configuration, see "Usage example"

Usage:

This package can be used as stand alone to create, parse and refresh json web tokens Note that the refresh happens automatically when a new Parse instruction comes in.

Usage example:
package main

import (
   "github.com/ausrasul/jwt"
)

func main(){
	// Configure the package
	
	jwt.Configure(
		map[string]interface{}{
			"privateKeyFile":         "privkey.pem",
			"publicKeyFile":          "pubkey.pem",
			"algorithm":              "RS256",
			"sessionName":            "My_application_name_no_spaces",
			"sessionTimeout":         3000, // seconds
			"sessionRefreshInterval": 300, //seconds
		},
	)
	
	userAttributes := make (map[string]interface{})
	// populate the map with user attributes.
	
	// Now use the JWT part
	token, err := jwt.CreateToken(userAttributes, res, req)
	/* the resulting token is saved directly to the http responseWriter. */
	
	// Now you can parse a token into a user
	userAttributes = goJwt.ParseToken(token, res, req)
	/* the result is a user attributes map[string]interface.
		if the session has reached the sessionRefreshInterval, it will be refereshed
		automatically, otherwise if it passes the sessionTimeout, you'll get
		an error */
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Configure

func Configure(cnf map[string]interface{}) error

func CreateToken

func CreateToken(user map[string]interface{}, w http.ResponseWriter, r *http.Request) (string, error)

func ParseToken

func ParseToken(w http.ResponseWriter, r *http.Request) (map[string]interface{}, error)

func RefreshToken

func RefreshToken(user map[string]interface{}, w http.ResponseWriter, r *http.Request) error

Types

type Jwt

type Jwt struct {
	// contains filtered or unexported fields
}

func (*Jwt) Configure

func (j *Jwt) Configure(cnf map[string]interface{}) error

type JwtClaims

type JwtClaims struct {
	*gojwt.StandardClaims
	User map[string]interface{}
}

type JwtConf

type JwtConf struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL