token

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2021 License: MIT Imports: 9 Imported by: 0

README

Token

JSON Web Token Interface built on top of golang-jwt

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, int64, error){
  subIP := "127.0.0.1"
  subID := "clientId"

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

func main() {
  // tokenstring from http request
  aud := "test.aud"
  subject, err = token.Verify(tokenstring, aud)
  if err != nil {
    log.Println(err)
  }

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

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, int64, error)

Create outputs JWT signed token string, expiration timestamp, and errors

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

func GetSubject

func GetSubject(userID, deviceID 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) (userID, deviceID string)

ParseSubject parses subject and returns user id and device 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