2019-05-22 21:23:23 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-10-01 07:47:55 +00:00
|
|
|
"net/http"
|
2019-05-22 21:23:23 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Token contains settings for request processing
|
|
|
|
type Token struct {
|
|
|
|
AccountID uint32
|
2022-08-08 11:46:24 +00:00
|
|
|
ProjectID uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns string representation of t.
|
|
|
|
func (t *Token) String() string {
|
2022-09-30 14:28:35 +00:00
|
|
|
if t == nil {
|
|
|
|
return "multitenant"
|
|
|
|
}
|
2022-08-08 11:46:24 +00:00
|
|
|
if t.ProjectID == 0 {
|
|
|
|
return fmt.Sprintf("%d", t.AccountID)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%d:%d", t.AccountID, t.ProjectID)
|
2019-05-22 21:23:23 +00:00
|
|
|
}
|
|
|
|
|
2022-09-30 14:28:35 +00:00
|
|
|
// NewToken returns new Token for the given authToken.
|
2019-05-22 21:23:23 +00:00
|
|
|
func NewToken(authToken string) (*Token, error) {
|
2022-09-30 14:28:35 +00:00
|
|
|
var t Token
|
|
|
|
if err := t.Init(authToken); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &t, nil
|
|
|
|
}
|
|
|
|
|
2023-08-30 12:08:47 +00:00
|
|
|
// NewTokenPossibleMultitenant returns new Token for the given authToken.
|
|
|
|
//
|
2024-10-01 07:47:55 +00:00
|
|
|
// If authToken == "multitenant" and TenantID header is not set, then nil Token is returned.
|
|
|
|
func NewTokenPossibleMultitenant(authToken string, headers http.Header) (*Token, error) {
|
2023-08-30 12:08:47 +00:00
|
|
|
if authToken == "multitenant" {
|
2024-10-01 07:47:55 +00:00
|
|
|
if tenantID := headers.Get("TenantID"); tenantID != "" {
|
|
|
|
return NewToken(tenantID)
|
|
|
|
}
|
2023-08-30 12:08:47 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return NewToken(authToken)
|
|
|
|
}
|
|
|
|
|
2022-09-30 14:28:35 +00:00
|
|
|
// Init initializes t from authToken.
|
|
|
|
func (t *Token) Init(authToken string) error {
|
2024-10-01 14:37:18 +00:00
|
|
|
accountID, projectID, err := ParseToken(authToken)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot parse authToken %q: %w", authToken, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Set(accountID, projectID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseToken parses authToken and returns accountID and projectID from it.
|
|
|
|
func ParseToken(authToken string) (uint32, uint32, error) {
|
2019-05-22 21:23:23 +00:00
|
|
|
tmp := strings.Split(authToken, ":")
|
|
|
|
if len(tmp) > 2 {
|
2024-10-01 14:37:18 +00:00
|
|
|
return 0, 0, fmt.Errorf("unexpected number of items in authToken %q; got %d; want 1 or 2", authToken, len(tmp))
|
2019-05-22 21:23:23 +00:00
|
|
|
}
|
2022-09-30 14:28:35 +00:00
|
|
|
n, err := strconv.ParseUint(tmp[0], 10, 32)
|
2019-05-22 21:23:23 +00:00
|
|
|
if err != nil {
|
2024-10-01 14:37:18 +00:00
|
|
|
return 0, 0, fmt.Errorf("cannot parse accountID from %q: %w", tmp[0], err)
|
2019-05-22 21:23:23 +00:00
|
|
|
}
|
2022-09-30 14:28:35 +00:00
|
|
|
accountID := uint32(n)
|
|
|
|
projectID := uint32(0)
|
2019-05-22 21:23:23 +00:00
|
|
|
if len(tmp) > 1 {
|
2022-09-30 14:28:35 +00:00
|
|
|
n, err := strconv.ParseUint(tmp[1], 10, 32)
|
2019-05-22 21:23:23 +00:00
|
|
|
if err != nil {
|
2024-10-01 14:37:18 +00:00
|
|
|
return 0, 0, fmt.Errorf("cannot parse projectID from %q: %w", tmp[1], err)
|
2019-05-22 21:23:23 +00:00
|
|
|
}
|
2022-09-30 14:28:35 +00:00
|
|
|
projectID = uint32(n)
|
2019-05-22 21:23:23 +00:00
|
|
|
}
|
2024-10-01 14:37:18 +00:00
|
|
|
return accountID, projectID, nil
|
2022-09-30 14:28:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets accountID and projectID for the t.
|
|
|
|
func (t *Token) Set(accountID, projectID uint32) {
|
|
|
|
t.AccountID = accountID
|
|
|
|
t.ProjectID = projectID
|
2019-05-22 21:23:23 +00:00
|
|
|
}
|