2019-01-08 00:37:16 +00:00
|
|
|
package influxdb
|
2018-05-14 16:26:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-01-10 21:21:59 +00:00
|
|
|
"fmt"
|
2018-05-14 16:26:38 +00:00
|
|
|
)
|
|
|
|
|
2019-02-07 01:34:54 +00:00
|
|
|
// AuthorizationKind is returned by (*Authorization).Kind().
|
|
|
|
const AuthorizationKind = "authorization"
|
|
|
|
|
2019-09-27 11:12:41 +00:00
|
|
|
// ErrUnableToCreateToken sanitized error message for all errors when a user cannot create a token
|
|
|
|
var ErrUnableToCreateToken = &Error{
|
|
|
|
Msg: "unable to create token",
|
|
|
|
Code: EInvalid,
|
|
|
|
}
|
2018-12-28 23:02:19 +00:00
|
|
|
|
|
|
|
// Authorization is an authorization. 🎉
|
2018-05-14 16:26:38 +00:00
|
|
|
type Authorization struct {
|
2018-12-28 23:02:19 +00:00
|
|
|
ID ID `json:"id"`
|
2018-05-14 16:26:38 +00:00
|
|
|
Token string `json:"token"`
|
2018-08-28 17:58:38 +00:00
|
|
|
Status Status `json:"status"`
|
2018-12-07 22:22:23 +00:00
|
|
|
Description string `json:"description"`
|
2018-12-28 23:02:19 +00:00
|
|
|
OrgID ID `json:"orgID"`
|
2019-01-14 15:20:20 +00:00
|
|
|
UserID ID `json:"userID,omitempty"`
|
2018-12-28 23:02:19 +00:00
|
|
|
Permissions []Permission `json:"permissions"`
|
2019-11-07 14:46:30 +00:00
|
|
|
CRUDLog
|
2018-05-14 16:26:38 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 19:02:45 +00:00
|
|
|
// AuthorizationUpdate is the authorization update request.
|
|
|
|
type AuthorizationUpdate struct {
|
|
|
|
Status *Status `json:"status,omitempty"`
|
|
|
|
Description *string `json:"description,omitempty"`
|
|
|
|
}
|
|
|
|
|
2019-01-10 21:21:59 +00:00
|
|
|
// Valid ensures that the authorization is valid.
|
|
|
|
func (a *Authorization) Valid() error {
|
|
|
|
for _, p := range a.Permissions {
|
2019-01-15 16:09:58 +00:00
|
|
|
if p.Resource.OrgID != nil && *p.Resource.OrgID != a.OrgID {
|
2019-01-10 21:21:59 +00:00
|
|
|
return &Error{
|
2019-04-17 20:30:22 +00:00
|
|
|
Msg: fmt.Sprintf("permission %s is not for org id %s", p, a.OrgID),
|
2019-01-10 21:21:59 +00:00
|
|
|
Code: EInvalid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-13 11:27:46 +00:00
|
|
|
// PermissionSet returns the set of permissions associated with the Authorization.
|
|
|
|
func (a *Authorization) PermissionSet() (PermissionSet, error) {
|
2018-09-28 17:02:34 +00:00
|
|
|
if !a.IsActive() {
|
2020-05-13 11:27:46 +00:00
|
|
|
return nil, &Error{
|
|
|
|
Code: EUnauthorized,
|
|
|
|
Msg: "token is inactive",
|
|
|
|
}
|
2018-08-28 17:58:38 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 11:27:46 +00:00
|
|
|
return a.Permissions, nil
|
2018-08-28 17:58:38 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 17:41:46 +00:00
|
|
|
// IsActive is a stub for idpe.
|
|
|
|
func IsActive(a *Authorization) bool {
|
|
|
|
return a.IsActive()
|
|
|
|
}
|
|
|
|
|
2018-08-28 17:58:38 +00:00
|
|
|
// IsActive returns true if the authorization active.
|
2018-09-28 17:02:34 +00:00
|
|
|
func (a *Authorization) IsActive() bool {
|
|
|
|
return a.Status == Active
|
2018-08-28 17:58:38 +00:00
|
|
|
}
|
|
|
|
|
2018-10-24 15:13:30 +00:00
|
|
|
// GetUserID returns the user id.
|
|
|
|
func (a *Authorization) GetUserID() ID {
|
|
|
|
return a.UserID
|
|
|
|
}
|
|
|
|
|
2018-09-28 18:33:35 +00:00
|
|
|
// Kind returns session and is used for auditing.
|
2019-02-07 01:34:54 +00:00
|
|
|
func (a *Authorization) Kind() string { return AuthorizationKind }
|
2018-09-28 18:33:35 +00:00
|
|
|
|
2018-10-01 20:04:43 +00:00
|
|
|
// Identifier returns the authorizations ID and is used for auditing.
|
2018-09-28 18:33:35 +00:00
|
|
|
func (a *Authorization) Identifier() ID { return a.ID }
|
|
|
|
|
2018-11-07 18:55:52 +00:00
|
|
|
// auth service op
|
|
|
|
const (
|
|
|
|
OpFindAuthorizationByID = "FindAuthorizationByID"
|
|
|
|
OpFindAuthorizationByToken = "FindAuthorizationByToken"
|
|
|
|
OpFindAuthorizations = "FindAuthorizations"
|
|
|
|
OpCreateAuthorization = "CreateAuthorization"
|
2019-03-27 19:02:45 +00:00
|
|
|
OpUpdateAuthorization = "UpdateAuthorization"
|
2018-11-07 18:55:52 +00:00
|
|
|
OpDeleteAuthorization = "DeleteAuthorization"
|
|
|
|
)
|
|
|
|
|
2018-05-14 16:26:38 +00:00
|
|
|
// AuthorizationService represents a service for managing authorization data.
|
|
|
|
type AuthorizationService interface {
|
2018-05-16 18:59:35 +00:00
|
|
|
// Returns a single authorization by ID.
|
|
|
|
FindAuthorizationByID(ctx context.Context, id ID) (*Authorization, error)
|
|
|
|
|
2018-05-14 16:26:38 +00:00
|
|
|
// Returns a single authorization by Token.
|
|
|
|
FindAuthorizationByToken(ctx context.Context, t string) (*Authorization, error)
|
|
|
|
|
|
|
|
// Returns a list of authorizations that match filter and the total count of matching authorizations.
|
|
|
|
// Additional options provide pagination & sorting.
|
|
|
|
FindAuthorizations(ctx context.Context, filter AuthorizationFilter, opt ...FindOptions) ([]*Authorization, int, error)
|
|
|
|
|
2018-05-16 18:59:35 +00:00
|
|
|
// Creates a new authorization and sets a.Token and a.UserID with the new identifier.
|
2018-05-14 16:26:38 +00:00
|
|
|
CreateAuthorization(ctx context.Context, a *Authorization) error
|
|
|
|
|
2019-03-27 19:02:45 +00:00
|
|
|
// UpdateAuthorization updates the status and description if available.
|
2020-04-30 14:52:21 +00:00
|
|
|
UpdateAuthorization(ctx context.Context, id ID, upd *AuthorizationUpdate) (*Authorization, error)
|
2018-08-27 19:18:11 +00:00
|
|
|
|
2018-05-14 16:26:38 +00:00
|
|
|
// Removes a authorization by token.
|
2018-07-25 15:26:45 +00:00
|
|
|
DeleteAuthorization(ctx context.Context, id ID) error
|
2018-05-14 16:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AuthorizationFilter represents a set of filter that restrict the returned results.
|
|
|
|
type AuthorizationFilter struct {
|
2018-05-16 18:59:35 +00:00
|
|
|
Token *string
|
|
|
|
ID *ID
|
|
|
|
|
2018-05-14 16:26:38 +00:00
|
|
|
UserID *ID
|
2018-05-16 18:59:35 +00:00
|
|
|
User *string
|
2019-04-14 08:42:46 +00:00
|
|
|
|
|
|
|
OrgID *ID
|
|
|
|
Org *string
|
2018-05-14 16:26:38 +00:00
|
|
|
}
|