added claims type
This commit is contained in:
parent
8e361f3156
commit
6e61945c2c
3 changed files with 64 additions and 8 deletions
52
claims.go
Normal file
52
claims.go
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
16
jwt.go
16
jwt.go
|
|
@ -323,7 +323,7 @@ type Token struct {
|
||||||
signature string
|
signature string
|
||||||
rawSignature []byte
|
rawSignature []byte
|
||||||
header map[string]interface{}
|
header map[string]interface{}
|
||||||
Claims map[string]interface{}
|
Claims Claims
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns the tokens encoded string
|
// 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
|
// NewToken returns a new *Token using the provided hash algorithm and claims
|
||||||
// If h is nil HS256 will be used
|
// If claims is nil, an empty map is used
|
||||||
func NewToken(claims map[string]interface{}, h Hash) *Token {
|
// If hash is nil, HS256 is used
|
||||||
if h == nil {
|
func NewToken(claims Claims, hash Hash) *Token {
|
||||||
h = NewHS256()
|
if claims == nil {
|
||||||
|
claims = make(Claims)
|
||||||
|
}
|
||||||
|
if hash == nil {
|
||||||
|
hash = NewHS256()
|
||||||
}
|
}
|
||||||
return &Token{
|
return &Token{
|
||||||
header: map[string]interface{}{
|
header: map[string]interface{}{
|
||||||
"alg": h.Alg(),
|
"alg": hash.Alg(),
|
||||||
"typ": typ,
|
"typ": typ,
|
||||||
},
|
},
|
||||||
Claims: claims,
|
Claims: claims,
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ func TestValidate(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
token := NewToken(map[string]interface{}{
|
token := NewToken(Claims{
|
||||||
"sub": "1234567890",
|
"sub": "1234567890",
|
||||||
"name": "John Doe",
|
"name": "John Doe",
|
||||||
"admin": true,
|
"admin": true,
|
||||||
|
|
@ -65,7 +65,7 @@ func TestNoBlacklist(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
token := NewToken(map[string]interface{}{
|
token := NewToken(Claims{
|
||||||
"sub": "1234567890",
|
"sub": "1234567890",
|
||||||
"name": "John Doe",
|
"name": "John Doe",
|
||||||
"admin": true,
|
"admin": true,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue