From 6e61945c2c80f613e36c514f5d84a1b2ae88a7ea Mon Sep 17 00:00:00 2001 From: ston1th Date: Wed, 12 Sep 2018 23:14:09 +0200 Subject: [PATCH] added claims type --- claims.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ jwt.go | 16 ++++++++++------ jwt_test.go | 4 ++-- 3 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 claims.go diff --git a/claims.go b/claims.go new file mode 100644 index 0000000..74e26c9 --- /dev/null +++ b/claims.go @@ -0,0 +1,52 @@ +// Copyright (C) 2018 Marius Schellenberger + +package jwt + +// Claims is the claim type of the token +type Claims map[string]interface{} + +// Get returns a value from the claims map +func (c Claims) Get(key string) (v interface{}, ok bool) { + v, ok = c[key] + return +} + +// GetString returns a string from the claims map +func (c Claims) GetString(key string) (s string) { + if v, ok := c.Get(key); ok && v != nil { + s, _ = v.(string) + } + return +} + +// GetBool returns a bool from the claims map +func (c Claims) GetBool(key string) (b bool) { + if v, ok := c.Get(key); ok && v != nil { + b, _ = v.(bool) + } + return +} + +// GetInt returns an int from the claims map +func (c Claims) GetInt(key string) (i int) { + if v, ok := c.Get(key); ok && v != nil { + i, _ = v.(int) + } + return +} + +// GetFloat returns a float from the claims map +func (c Claims) GetFloat(key string) (f float64) { + if v, ok := c.Get(key); ok && v != nil { + f, _ = v.(float64) + } + return +} + +// Set sets the value of key in the claims map, if not nil +func (c Claims) Set(key string, v interface{}) { + if c == nil { + return + } + c[key] = v +} diff --git a/jwt.go b/jwt.go index 2106f13..1268cb2 100644 --- a/jwt.go +++ b/jwt.go @@ -323,7 +323,7 @@ type Token struct { signature string rawSignature []byte header map[string]interface{} - Claims map[string]interface{} + Claims Claims } // String returns the tokens encoded string @@ -352,14 +352,18 @@ func (t *Token) Data() string { } // NewToken returns a new *Token using the provided hash algorithm and claims -// If h is nil HS256 will be used -func NewToken(claims map[string]interface{}, h Hash) *Token { - if h == nil { - h = NewHS256() +// If claims is nil, an empty map is used +// If hash is nil, HS256 is used +func NewToken(claims Claims, hash Hash) *Token { + if claims == nil { + claims = make(Claims) + } + if hash == nil { + hash = NewHS256() } return &Token{ header: map[string]interface{}{ - "alg": h.Alg(), + "alg": hash.Alg(), "typ": typ, }, Claims: claims, diff --git a/jwt_test.go b/jwt_test.go index 30cfbfe..17a519b 100644 --- a/jwt_test.go +++ b/jwt_test.go @@ -14,7 +14,7 @@ func TestValidate(t *testing.T) { if err != nil { t.Error(err) } - token := NewToken(map[string]interface{}{ + token := NewToken(Claims{ "sub": "1234567890", "name": "John Doe", "admin": true, @@ -65,7 +65,7 @@ func TestNoBlacklist(t *testing.T) { if err != nil { t.Error(err) } - token := NewToken(map[string]interface{}{ + token := NewToken(Claims{ "sub": "1234567890", "name": "John Doe", "admin": true,