README ¶
Token
JSON Web Token Interface built on top of golang-jwt
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
- Variables
- func Create(sub, aud, expIn string) (string, int64, error)
- func GetSubject(userID, deviceID string) string
- func InitPrvKey(filepath string) error
- func InitPubKey(filepath string) error
- func ParseSubject(sub string) (userID, deviceID string)
- func Verify(tokenString string, aud string) (sub string, err error)
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
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 ¶
GetSubject returns properly formatted subject that perme micro-services can understand
func ParseSubject ¶
ParseSubject parses subject and returns user id and device id
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.