mirror of
https://github.com/jech/galene.git
synced 2024-11-22 16:45:58 +01:00
Export getStateful, simplify interface.
We used to return nil, nil if the token didn't exist. We now return os.ErrNotExist.
This commit is contained in:
parent
0c3616642d
commit
8a94f4a716
3 changed files with 18 additions and 20 deletions
|
@ -126,6 +126,8 @@ func toStringArray(a interface{}) ([]string, bool) {
|
||||||
return b, true
|
return b, true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parseJWT tries to parse a string as a JWT.
|
||||||
|
// It returns (nil, nil) if the string does not look like a JWT.
|
||||||
func parseJWT(token string, keys []map[string]interface{}) (*JWT, error) {
|
func parseJWT(token string, keys []map[string]interface{}) (*JWT, error) {
|
||||||
t, err := jwt.Parse(
|
t, err := jwt.Parse(
|
||||||
token,
|
token,
|
||||||
|
|
|
@ -57,20 +57,23 @@ func SetStatefulFilename(filename string) {
|
||||||
tokens.modTime = time.Time{}
|
tokens.modTime = time.Time{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getStateful(token string) (*Stateful, error) {
|
// Get fetches a stateful token.
|
||||||
|
// It returns os.ErrNotExist if the token doesn't exist.
|
||||||
|
func Get(token string) (*Stateful, error) {
|
||||||
tokens.mu.Lock()
|
tokens.mu.Lock()
|
||||||
defer tokens.mu.Unlock()
|
defer tokens.mu.Unlock()
|
||||||
err := tokens.load()
|
err := tokens.load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if tokens.tokens == nil {
|
if tokens.tokens == nil {
|
||||||
return nil, nil
|
return nil, os.ErrNotExist
|
||||||
}
|
}
|
||||||
return tokens.tokens[token], nil
|
t := tokens.tokens[token]
|
||||||
|
if t == nil {
|
||||||
|
return nil, os.ErrNotExist
|
||||||
|
}
|
||||||
|
return t, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (token *Stateful) Check(host, group string, username *string) (string, []string, error) {
|
func (token *Stateful) Check(host, group string, username *string) (string, []string, error) {
|
||||||
|
|
|
@ -2,7 +2,6 @@ package token
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrUsernameRequired = errors.New("username required")
|
var ErrUsernameRequired = errors.New("username required")
|
||||||
|
@ -13,21 +12,15 @@ type Token interface {
|
||||||
|
|
||||||
func Parse(token string, keys []map[string]interface{}) (Token, error) {
|
func Parse(token string, keys []map[string]interface{}) (Token, error) {
|
||||||
// both getStateful and parseJWT may return nil, which we
|
// both getStateful and parseJWT may return nil, which we
|
||||||
// shouldn't cast into an interface. Be very careful.
|
// shouldn't cast into an interface before testing for nil.
|
||||||
s, err1 := getStateful(token)
|
jwt, err := parseJWT(token, keys)
|
||||||
if err1 == nil && s != nil {
|
if err != nil {
|
||||||
return s, nil
|
// parses correctly but doesn't validate
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if jwt != nil {
|
||||||
jwt, err2 := parseJWT(token, keys)
|
|
||||||
if err2 == nil && jwt != nil {
|
|
||||||
return jwt, nil
|
return jwt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err1 != nil {
|
return Get(token)
|
||||||
return nil, err1
|
|
||||||
} else if err2 != nil {
|
|
||||||
return nil, err2
|
|
||||||
}
|
|
||||||
return nil, os.ErrNotExist
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue