2019-05-16 10:39:25 -04:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2019-07-31 13:47:30 -08:00
|
|
|
"context"
|
2019-05-16 10:39:25 -04:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
jwt "github.com/dgrijalva/jwt-go"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// These are the expected values for Claims.Roles.
|
|
|
|
const (
|
2019-05-29 03:35:08 -05:00
|
|
|
RoleAdmin = "admin"
|
|
|
|
RoleUser = "user"
|
2019-05-16 10:39:25 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ctxKey represents the type of value for the context key.
|
|
|
|
type ctxKey int
|
|
|
|
|
|
|
|
// Key is used to store/retrieve a Claims value from a context.Context.
|
|
|
|
const Key ctxKey = 1
|
|
|
|
|
|
|
|
// Claims represents the authorization claims transmitted via a JWT.
|
|
|
|
type Claims struct {
|
2019-08-04 21:28:02 -08:00
|
|
|
RootUserID string `json:"root_user_id"`
|
|
|
|
RootAccountID string `json:"root_account_id"`
|
|
|
|
AccountIDs []string `json:"accounts"`
|
|
|
|
Roles []string `json:"roles"`
|
|
|
|
Preferences ClaimPreferences `json:"prefs"`
|
2019-05-16 10:39:25 -04:00
|
|
|
jwt.StandardClaims
|
|
|
|
}
|
|
|
|
|
2019-08-04 14:48:43 -08:00
|
|
|
// ClaimPreferences defines preferences for the user.
|
|
|
|
type ClaimPreferences struct {
|
|
|
|
Timezone string `json:"timezone"`
|
|
|
|
DatetimeFormat string `json:"pref_datetime_format"`
|
|
|
|
DateFormat string `json:"pref_date_format"`
|
|
|
|
TimeFormat string `json:"pref_time_format"`
|
|
|
|
tz *time.Location
|
|
|
|
}
|
|
|
|
|
2019-05-16 10:39:25 -04:00
|
|
|
// NewClaims constructs a Claims value for the identified user. The Claims
|
|
|
|
// expire within a specified duration of the provided time. Additional fields
|
|
|
|
// of the Claims can be set after calling NewClaims is desired.
|
2019-08-04 21:28:02 -08:00
|
|
|
func NewClaims(userID, accountID string, accountIDs []string, roles []string, prefs ClaimPreferences, now time.Time, expires time.Duration) Claims {
|
2019-05-16 10:39:25 -04:00
|
|
|
c := Claims{
|
2019-08-04 21:28:02 -08:00
|
|
|
AccountIDs: accountIDs,
|
|
|
|
RootAccountID: accountID,
|
|
|
|
RootUserID: userID,
|
|
|
|
Roles: roles,
|
|
|
|
Preferences: prefs,
|
2019-05-16 10:39:25 -04:00
|
|
|
StandardClaims: jwt.StandardClaims{
|
2019-08-04 21:28:02 -08:00
|
|
|
Subject: userID,
|
|
|
|
Audience: accountID,
|
2019-05-16 10:39:25 -04:00
|
|
|
IssuedAt: now.Unix(),
|
|
|
|
ExpiresAt: now.Add(expires).Unix(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-08-04 14:48:43 -08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClaimPreferences constructs ClaimPreferences for the user/account.
|
|
|
|
func NewClaimPreferences(timezone *time.Location, datetimeFormat, dateFormat, timeFormat string) ClaimPreferences {
|
|
|
|
p := ClaimPreferences{
|
|
|
|
DatetimeFormat: datetimeFormat,
|
|
|
|
DateFormat: dateFormat,
|
|
|
|
TimeFormat: timeFormat,
|
2019-06-25 22:31:54 -08:00
|
|
|
}
|
|
|
|
|
2019-08-04 14:48:43 -08:00
|
|
|
if timezone != nil {
|
|
|
|
p.Timezone = timezone.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
2019-05-16 10:39:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Valid is called during the parsing of a token.
|
|
|
|
func (c Claims) Valid() error {
|
|
|
|
for _, r := range c.Roles {
|
|
|
|
switch r {
|
|
|
|
case RoleAdmin, RoleUser: // Role is valid.
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid role %q", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := c.StandardClaims.Valid(); err != nil {
|
|
|
|
return errors.Wrap(err, "validating standard claims")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:47:30 -08:00
|
|
|
// HasAuth returns true the user is authenticated.
|
|
|
|
func (c Claims) HasAuth() bool {
|
|
|
|
if c.Subject != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-05-16 10:39:25 -04:00
|
|
|
// HasRole returns true if the claims has at least one of the provided roles.
|
|
|
|
func (c Claims) HasRole(roles ...string) bool {
|
|
|
|
for _, has := range c.Roles {
|
|
|
|
for _, want := range roles {
|
|
|
|
if has == want {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2019-06-25 22:31:54 -08:00
|
|
|
|
2019-07-31 13:47:30 -08:00
|
|
|
// TimeLocation returns the timezone used to format datetimes for the user.
|
2019-08-04 14:48:43 -08:00
|
|
|
func (c ClaimPreferences) TimeLocation() *time.Location {
|
2019-06-25 22:31:54 -08:00
|
|
|
if c.tz == nil && c.Timezone != "" {
|
|
|
|
c.tz, _ = time.LoadLocation(c.Timezone)
|
|
|
|
}
|
|
|
|
return c.tz
|
|
|
|
}
|
2019-07-31 13:47:30 -08:00
|
|
|
|
2019-08-04 14:48:43 -08:00
|
|
|
// TimeLocation returns the timezone used to format datetimes for the user.
|
|
|
|
func (c Claims) TimeLocation() *time.Location {
|
|
|
|
return c.Preferences.TimeLocation()
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:47:30 -08:00
|
|
|
// ClaimsFromContext loads the claims from context.
|
|
|
|
func ClaimsFromContext(ctx context.Context) (Claims, error) {
|
|
|
|
claims, ok := ctx.Value(Key).(Claims)
|
|
|
|
if !ok {
|
|
|
|
// TODO(jlw) should this be a web.Shutdown?
|
|
|
|
return Claims{}, errors.New("claims missing from context: HasRole called without/before Authenticate")
|
|
|
|
}
|
|
|
|
|
|
|
|
return claims, nil
|
|
|
|
}
|