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. All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without

View file

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

View file

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