added empty token check and copyright

This commit is contained in:
ston1th 2018-01-03 23:04:17 +01:00
commit 339d6de8bd
4 changed files with 15 additions and 1 deletions

View file

@ -1,4 +1,4 @@
Copyright (C) 2016 Marius Schellenberger
Copyright (C) 2018 Marius Schellenberger
All rights reserved.
Redistribution and use in source and binary forms, with or without

View file

@ -1,3 +1,5 @@
// Copyright (C) 2018 Marius Schellenberger
package jwt
import (

6
jwt.go
View file

@ -1,3 +1,5 @@
// Copyright (C) 2018 Marius Schellenberger
// Package jwt provides a easy to use JSON Web Token and blacklisting library
package jwt
@ -24,6 +26,7 @@ const (
var (
ErrNoJWT = errors.New("not a json web token")
ErrEmptyToken = errors.New("token is empty")
ErrUnsupportedAlg = errors.New("unsupported algorithm")
ErrInvalid = errors.New("token validation failed")
ErrBlacklisted = errors.New("token blacklisted")
@ -274,6 +277,9 @@ func (jwt *JWT) Stop() error {
// DecodeToken decodes a raw string token into a *Token object
func DecodeToken(token string) (t *Token, err error) {
if token == "" {
return nil, ErrEmptyToken
}
parts := strings.Split(token, ".")
if len(parts) < 3 {
return nil, ErrMissingTokenParts

View file

@ -1,3 +1,5 @@
// Copyright (C) 2018 Marius Schellenberger
package jwt
import (
@ -18,6 +20,10 @@ func TestValidate(t *testing.T) {
if err != nil {
t.Error(err)
}
_, err = DecodeToken("")
if err == nil {
t.Error(errors.New("token is empty"))
}
nt, err := DecodeToken(token.String())
if err != nil {
t.Error(err)