forked from tsuru/tsuru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
48 lines (41 loc) · 962 Bytes
/
token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright 2015 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package auth
import (
"errors"
"strings"
"github.com/tsuru/tsuru/permission"
)
type Token interface {
GetValue() string
GetAppName() string
GetUserName() string
IsAppToken() bool
User() (*User, error)
Permissions() ([]permission.Permission, error)
}
var ErrInvalidToken = errors.New("Invalid token")
// ParseToken extracts token from a header:
// 'type token' or 'token'
func ParseToken(header string) (string, error) {
s := strings.Split(header, " ")
var value string
if len(s) < 3 {
value = s[len(s)-1]
}
if value != "" {
return value, nil
}
return value, ErrInvalidToken
}
func BaseTokenPermission(t Token) ([]permission.Permission, error) {
if t.IsAppToken() {
return nil, nil
}
user, err := t.User()
if err != nil {
return nil, err
}
return user.Permissions()
}