Documentation ¶
Overview ¶
Package Gojwt or goJweto is a JSON Web Tokens library implemented in golang.
See Readme.md for more info.
Example (CreateGoJwtObjects) ¶
main function with hello world in goJwt
var ( privECDSAKeyPath = "asdsa" pubECDSAKeyPath = "" privRSAKeyPath = "" pubRSAKeyPath = "" ) GojwtObject, err := gojwt.NewGojwt() fmt.Println("Example with Default config GoJwt Object: ", GojwtObject, "with error: ", err) GojwtObject, err = gojwt.NewGojwtHMAC_SHA("JnzadsServer", "jnzads-rest", "Jnzads-rest-JWT", "512", 24) fmt.Println("Example with empty secret Key and HMAC method: ", GojwtObject, "with error: ", err) GojwtObject, err = gojwt.NewGojwtRSA("JnzadsServer", "Jnzads-rest-JWT", privKeyPath, pubKeyPath, "384", 24) fmt.Println("Example with empty secret Key and RSA/ECDSA method: ", GojwtObject, "with error: ", err) GojwtObject, err = gojwt.NewGojwtECDSA("JnzadsServer", "Jnzads-rest-JWT", privKeyPath, pubKeyPath, "256", 24) fmt.Println("Example with empty secret Key and RSA/ECDSA method: ", GojwtObject, "with error: ", err)
Output:
Index ¶
- Variables
- func FromJSON(data []byte) (map[string]interface{}, error)
- func JsonResponse(response interface{}, w http.ResponseWriter)
- func MiddlewareGojwtHeaders(pageHandler, noAuthHandler http.HandlerFunc, o *Gojwt) http.HandlerFunc
- func ToJSON(s interface{}) ([]byte, error)
- type Claims
- type CredentialsAuth
- type CredentialsNoAuth
- type Gojwt
- func NewGojwt() (*Gojwt, error)
- func NewGojwtECDSA(nameserver, headerkey, privKeyPath, pubKeyPath, lenbytes string, ...) (*Gojwt, error)
- func NewGojwtHMAC_SHA(nameserver, headerkey, secretkey, lenbytes string, hours time.Duration) (*Gojwt, error)
- func NewGojwtRSA(nameserver, headerkey, privKeyPath, pubKeyPath, lenbytes string, ...) (*Gojwt, error)
- func (o *Gojwt) CreateToken(username string) (tokenString string, err error)
- func (o *Gojwt) GetECDSAPrivKey() *ecdsa.PrivateKey
- func (o *Gojwt) GetECDSAPubKey() *ecdsa.PublicKey
- func (o *Gojwt) GetEncryptLenBytes() string
- func (o *Gojwt) GetEncryptMethod() string
- func (o *Gojwt) GetHeaderKey() string
- func (o *Gojwt) GetNameServer() string
- func (o *Gojwt) GetNumHoursDuration() time.Duration
- func (o *Gojwt) GetPrivKeyPath() string
- func (o *Gojwt) GetPubKeyPath() string
- func (o *Gojwt) GetRSAPrivKey() *rsa.PrivateKey
- func (o *Gojwt) GetRSAPubKey() *rsa.PublicKey
- func (o *Gojwt) GetSecretByte() []byte
- func (o *Gojwt) GetSecretKey() string
- func (o *Gojwt) SetEncryptLenBytes(lenBytes string)
- func (o *Gojwt) SetEncryptMethod(method string)
- func (o *Gojwt) SetHeaderKey(name string)
- func (o *Gojwt) SetNameServer(name string)
- func (o *Gojwt) SetNumHoursDuration(hours time.Duration)
- func (o *Gojwt) SetPrivKeyPath(path string)
- func (o *Gojwt) SetPubKeyPath(path string)
- func (o *Gojwt) SetSecretKey(name string)
- func (o *Gojwt) ValidateToken(tokenString string) (isValid bool, username string, err error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( GojwtErrInvalidEmptySecretKey = errors.New("Invalid Empty Secret Key.\n") GojwtErrInvalidEmptyPrivateKey = errors.New("Invalid Empty Path Private Key.\n") GojwtErrInvalidEmptyPublicKey = errors.New("Invalid Empty Path Public Key.\n") GojwtErrInvalidEmptyToken = errors.New("Invalid Empty Token") GojwtErrInvalidAlgorithm = errors.New("Invalid Algorithm to create token.\n") GojwtErrInvalidRSABytes = errors.New("Invalid RSA len bytes Algorithm.\n") GojwtErrInvalidECDSABytes = errors.New("Invalid ECDSA len bytes Algorithm.\n") GojwtErrInvalidHMACHSABytes = errors.New("Invalid HMAC-SHA len bytes Algorithm.\n") GojwtErrInvalidToken = errors.New("Invalid Token") GojwtErrBadFormatToken = errors.New("Invalid Format Token") GojwtErrTokenExpired = errors.New("Invalid Token is Expired.\n") GojwtErrNotWorkToken = errors.New("Invalid Token is not working.\n") GojwtErrIsNotPubECDSAKey = errors.New("Is not an ECDSA Public Key.\n") GojwtErrIsNotPrivECDSAKey = errors.New("Is not an ECDSA Private Key.\n") GojwtErrIsNotPubRSAKey = errors.New("Is not a RSA Public Key.\n") GojwtErrIsNotPrivRSAKey = errors.New("Is not a RSA Private Key.\n") GojwtErrTriedToMarshal = errors.New("Tried to Marshal Invalid Type.\n") GojwtErrInterfaceNotExist = errors.New("Interface passed does not exist.\n") )
Functions ¶
func JsonResponse ¶
func JsonResponse(response interface{}, w http.ResponseWriter)
Write in JSON Format
func MiddlewareGojwtHeaders ¶
func MiddlewareGojwtHeaders(pageHandler, noAuthHandler http.HandlerFunc, o *Gojwt) http.HandlerFunc
middleware gojwt
Types ¶
type Claims ¶
type Claims struct { NameServer string `json:"nameServer,omitempty"` jwt.StandardClaims }
JWT schema of the data it will store
type CredentialsAuth ¶
Struct to encapsulate if the username is not logged
type CredentialsNoAuth ¶
type CredentialsNoAuth struct {
Logged bool `json:"Logged"`
}
Struct to encapsulate if the username is not logged
type Gojwt ¶
type Gojwt struct {
// contains filtered or unexported fields
}
GoJwt object
func NewGojwtECDSA ¶
func NewGojwtECDSA(nameserver, headerkey, privKeyPath, pubKeyPath, lenbytes string, hours time.Duration) (*Gojwt, error)
Create a New GoJwt Instance with ECDSA method
func NewGojwtHMAC_SHA ¶
func NewGojwtHMAC_SHA(nameserver, headerkey, secretkey, lenbytes string, hours time.Duration) (*Gojwt, error)
Create a New GoJwt Instance with HMAC-SHA method
func NewGojwtRSA ¶
func NewGojwtRSA(nameserver, headerkey, privKeyPath, pubKeyPath, lenbytes string, hours time.Duration) (*Gojwt, error)
Create a New GoJwt Instance with RSA method
func (*Gojwt) CreateToken ¶
Create token with a specific encrypt method
func (*Gojwt) GetECDSAPrivKey ¶
func (o *Gojwt) GetECDSAPrivKey() *ecdsa.PrivateKey
just for method ECDSA get ECDSA private key
func (*Gojwt) GetECDSAPubKey ¶
get ECDSA public key
func (*Gojwt) GetEncryptLenBytes ¶
get bytes of encrypt method
func (*Gojwt) GetEncryptMethod ¶
get current encrypt method
func (*Gojwt) GetNumHoursDuration ¶
get hours of token duration
func (*Gojwt) GetRSAPrivKey ¶
func (o *Gojwt) GetRSAPrivKey() *rsa.PrivateKey
just for method RSA get RSA private key
func (*Gojwt) GetSecretByte ¶
get secret key word convert in bytes
func (*Gojwt) GetSecretKey ¶
get secret key word to encrypt using hmac-sha
func (*Gojwt) SetEncryptLenBytes ¶
set bytes of encrypt method
func (*Gojwt) SetEncryptMethod ¶
change encrypt method
func (*Gojwt) SetNumHoursDuration ¶
set hours of token duration
func (*Gojwt) SetPubKeyPath ¶
path of keys RSA/ECDSA set PATH of Public key
func (*Gojwt) SetSecretKey ¶
just for method HMACSHA set secret key word to encrypt using hmac-sha