2019-05-16 10:39:25 -04:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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-05-27 02:44:40 -05:00
|
|
|
AccountIds []string `json:"accounts"`
|
|
|
|
Roles []string `json:"roles"`
|
2019-05-16 10:39:25 -04:00
|
|
|
jwt.StandardClaims
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-05-27 02:44:40 -05:00
|
|
|
func NewClaims(userId, accountId string, accountIds []string, roles []string, now time.Time, expires time.Duration) Claims {
|
2019-05-16 10:39:25 -04:00
|
|
|
c := Claims{
|
2019-05-27 02:44:40 -05:00
|
|
|
AccountIds: accountIds,
|
|
|
|
Roles: roles,
|
2019-05-16 10:39:25 -04:00
|
|
|
StandardClaims: jwt.StandardClaims{
|
2019-05-27 02:44:40 -05:00
|
|
|
Subject: userId,
|
|
|
|
Audience: accountId,
|
2019-05-16 10:39:25 -04:00
|
|
|
IssuedAt: now.Unix(),
|
|
|
|
ExpiresAt: now.Add(expires).Unix(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|