token

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2020 License: MIT Imports: 9 Imported by: 0

README

Token

JSON Web Token Interface built on top of jwt-go

PkgGoDev Go Report Card GitHub tag (latest by date) GitHub


Getting Started

Prerequisite

Create a private and public key pair in root directory

e.g. creating RSA key pair via openssl

openssl genrsa -out rsa_prv.key 2048 && openssl rsa -RSAPublicKey_out -in rsa_prv.key -out rsa_pub.key
Installing

go get it (pun intended 😸)

go get github.com/JuneKimDev/token

Usage

package main

import (
  "log"

  "github.com/JuneKimDev/token"
)

func init(){
  // For token issuer
  if err := token.InitPrvKey("rsa_prv.key"); err != nil {
    log.Fatal(err)
  }
  // For token verifier
  if err := token.InitPubKey("rsa_prv.key"); err != nil {
    log.Fatal(err)
  }
}

func issue() (string, error){
  subIP := "127.0.0.1"
  subID := "clientId"

  sub := token.GetSubject(subIP, subID)
  aud := "test.aud"
  expIn := "1h"
  return token.CreateToken(sub, aud, expIn)
}

func main() {
  // tokenstring from http request
  subject, err = token.VerifyToken(tokenstring)
  if err != nil {
    log.Println(err)
  }

  userIP, userID := token.ParseSubject(subject)
  // Do something with it
}

License

This project is licensed under the MIT License - see the LICENSE file for details

Documentation

Index

Constants

View Source
const (
	TokenIssuer           string = "github.com/JuneKimDev/token"
	TokenSubjectDelimiter string = "#"
)

Constants

Variables

View Source
var (
	ErrNoFilepath         = errors.New("Filepath is empty")
	ErrPrvKeyNotInitiated = errors.New("Private key is not initiated")
	ErrPubKeyNotInitiated = errors.New("Public key is not initiated")
	ErrInvalidToken       = errors.New("Token is invalid")
)

Errors

Functions

func Create added in v0.0.1

func Create(sub, aud, expIn string) (string, error)

Create creates JWT signed token string

"expIn" is a string as in time.ParseDuration: e.g. 1h, 10m, 1.5s, -300ms, 1h20m

func GetSubject

func GetSubject(ip string, userID string) string

GetSubject returns properly formatted subject that perme micro-services can understand

func InitPrvKey

func InitPrvKey(filepath string) error

InitPrvKey initializes private key

func InitPubKey

func InitPubKey(filepath string) error

InitPubKey initializes public key

func ParseSubject

func ParseSubject(sub string) (ip string, userID string)

ParseSubject parses subject and returns ip and user id

func Verify added in v0.0.1

func Verify(tokenString string, aud string) (sub string, err error)

Verify parses/verifies JWT and returns subject

Types

This section is empty.

Jump to

Keyboard shortcuts

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