2023-08-02 03:43:56 -05:00
|
|
|
package identity
|
|
|
|
|
2023-08-10 07:20:58 -05:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2024-07-30 00:27:23 -05:00
|
|
|
|
2024-08-09 11:46:56 -05:00
|
|
|
"github.com/grafana/authlib/claims"
|
2024-07-30 00:27:23 -05:00
|
|
|
"k8s.io/apiserver/pkg/authentication/user"
|
2023-08-10 07:20:58 -05:00
|
|
|
)
|
2023-08-09 02:35:50 -05:00
|
|
|
|
2023-08-02 03:43:56 -05:00
|
|
|
type Requester interface {
|
2024-07-30 00:27:23 -05:00
|
|
|
user.Info
|
2024-08-09 11:46:56 -05:00
|
|
|
claims.AuthInfo
|
2024-07-30 00:27:23 -05:00
|
|
|
|
|
|
|
// GetIdentityType returns the type for the requester
|
2024-08-12 01:26:53 -05:00
|
|
|
GetIdentityType() claims.IdentityType
|
2024-08-13 03:18:28 -05:00
|
|
|
// IsIdentityType returns true if identity type for requester matches any expected identity type
|
|
|
|
IsIdentityType(expected ...claims.IdentityType) bool
|
2024-07-30 00:27:23 -05:00
|
|
|
// GetRawIdentifier returns only the identifier part of the UID, excluding the type
|
|
|
|
GetRawIdentifier() string
|
2024-08-13 03:18:28 -05:00
|
|
|
// GetInternalID returns only the identifier part of the ID, excluding the type
|
2024-07-30 00:27:23 -05:00
|
|
|
GetInternalID() (int64, error)
|
|
|
|
// GetID returns namespaced internalID for the entity
|
|
|
|
// Deprecated: use GetUID instead
|
2024-08-13 03:18:28 -05:00
|
|
|
GetID() string
|
2023-08-09 05:33:35 -05:00
|
|
|
// GetDisplayName returns the display name of the active entity.
|
|
|
|
// The display name is the name if it is set, otherwise the login or email.
|
|
|
|
GetDisplayName() string
|
|
|
|
// GetEmail returns the email of the active entity.
|
|
|
|
// Can be empty.
|
|
|
|
GetEmail() string
|
2024-04-05 05:05:46 -05:00
|
|
|
// IsEmailVerified returns if email is verified for entity.
|
|
|
|
IsEmailVerified() bool
|
2023-08-09 05:33:35 -05:00
|
|
|
// GetIsGrafanaAdmin returns true if the user is a server admin
|
2023-08-02 03:43:56 -05:00
|
|
|
GetIsGrafanaAdmin() bool
|
2023-08-09 05:33:35 -05:00
|
|
|
// GetLogin returns the login of the active entity
|
|
|
|
// Can be empty.
|
2023-08-02 03:43:56 -05:00
|
|
|
GetLogin() string
|
2023-08-09 05:33:35 -05:00
|
|
|
// GetOrgID returns the ID of the active organization
|
2023-08-02 03:43:56 -05:00
|
|
|
GetOrgID() int64
|
2023-08-09 05:33:35 -05:00
|
|
|
// GetOrgRole returns the role of the active entity in the active organization.
|
2024-06-12 23:11:35 -05:00
|
|
|
GetOrgRole() RoleType
|
2023-08-09 05:33:35 -05:00
|
|
|
// GetPermissions returns the permissions of the active entity.
|
2023-08-09 02:35:50 -05:00
|
|
|
GetPermissions() map[string][]string
|
2024-02-01 05:37:01 -06:00
|
|
|
// GetGlobalPermissions returns the permissions of the active entity that are available across all organizations.
|
|
|
|
GetGlobalPermissions() map[string][]string
|
2023-08-09 05:33:35 -05:00
|
|
|
// DEPRECATED: GetTeams returns the teams the entity is a member of.
|
|
|
|
// Retrieve the teams from the team service instead of using this method.
|
2023-08-09 02:35:50 -05:00
|
|
|
GetTeams() []int64
|
2023-08-09 05:33:35 -05:00
|
|
|
// DEPRECATED: GetOrgName returns the name of the active organization.
|
|
|
|
// Retrieve the organization name from the organization service instead of using this method.
|
|
|
|
GetOrgName() string
|
2024-04-11 03:25:29 -05:00
|
|
|
// GetAuthID returns external id for entity.
|
|
|
|
GetAuthID() string
|
2024-07-08 14:22:10 -05:00
|
|
|
// GetAllowedKubernetesNamespace returns either "*" or the single namespace this requester has access to
|
|
|
|
// An empty value means the implementation has not specified a kubernetes namespace.
|
|
|
|
GetAllowedKubernetesNamespace() string
|
2024-04-11 03:25:29 -05:00
|
|
|
// GetAuthenticatedBy returns the authentication method used to authenticate the entity.
|
|
|
|
GetAuthenticatedBy() string
|
2024-03-27 09:22:13 -05:00
|
|
|
// IsAuthenticatedBy returns true if entity was authenticated by any of supplied providers.
|
|
|
|
IsAuthenticatedBy(providers ...string) bool
|
2023-08-09 05:33:35 -05:00
|
|
|
// IsNil returns true if the identity is nil
|
|
|
|
// FIXME: remove this method once all services are using an interface
|
2023-08-02 03:43:56 -05:00
|
|
|
IsNil() bool
|
2023-08-09 02:35:50 -05:00
|
|
|
|
|
|
|
// Legacy
|
2023-08-09 05:33:35 -05:00
|
|
|
|
2023-08-18 05:42:18 -05:00
|
|
|
// HasRole returns true if the active entity has the given role in the active organization.
|
2024-06-12 23:11:35 -05:00
|
|
|
HasRole(role RoleType) bool
|
2023-08-09 05:33:35 -05:00
|
|
|
// GetCacheKey returns a unique key for the entity.
|
|
|
|
// Add an extra prefix to avoid collisions with other caches
|
2023-09-14 02:19:33 -05:00
|
|
|
GetCacheKey() string
|
2023-08-09 05:33:35 -05:00
|
|
|
// HasUniqueId returns true if the entity has a unique id
|
2023-08-09 02:35:50 -05:00
|
|
|
HasUniqueId() bool
|
2023-09-28 02:22:05 -05:00
|
|
|
// GetIDToken returns a signed token representing the identity that can be forwarded to plugins and external services.
|
|
|
|
GetIDToken() string
|
2023-08-02 03:43:56 -05:00
|
|
|
}
|
2023-08-10 07:20:58 -05:00
|
|
|
|
2024-08-09 10:20:24 -05:00
|
|
|
// IntIdentifier converts a typeID to an int64.
|
2023-08-10 07:20:58 -05:00
|
|
|
// Applicable for users, service accounts, api keys and renderer service.
|
2024-08-09 10:20:24 -05:00
|
|
|
// Errors if the identifier is not initialized or if type is not recognized.
|
2024-08-13 03:18:28 -05:00
|
|
|
func IntIdentifier(typedID string) (int64, error) {
|
|
|
|
typ, id, err := ParseTypeAndID(typedID)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2023-08-10 07:20:58 -05:00
|
|
|
}
|
|
|
|
|
2024-08-13 03:18:28 -05:00
|
|
|
return intIdentifier(typ, id, claims.TypeUser, claims.TypeAPIKey, claims.TypeServiceAccount, claims.TypeRenderService)
|
2023-08-10 07:20:58 -05:00
|
|
|
}
|
2023-10-09 09:07:28 -05:00
|
|
|
|
2024-08-09 10:20:24 -05:00
|
|
|
// UserIdentifier converts a typeID to an int64.
|
2023-10-09 09:07:28 -05:00
|
|
|
// Errors if the identifier is not initialized or if namespace is not recognized.
|
2024-08-09 10:20:24 -05:00
|
|
|
// Returns 0 if the type is not user or service account
|
2024-08-13 03:18:28 -05:00
|
|
|
func UserIdentifier(typedID string) (int64, error) {
|
|
|
|
typ, id, err := ParseTypeAndID(typedID)
|
2023-10-09 09:07:28 -05:00
|
|
|
if err != nil {
|
2024-08-09 10:20:24 -05:00
|
|
|
return 0, err
|
2023-10-09 09:07:28 -05:00
|
|
|
}
|
|
|
|
|
2024-08-13 03:18:28 -05:00
|
|
|
return intIdentifier(typ, id, claims.TypeUser, claims.TypeServiceAccount)
|
|
|
|
}
|
|
|
|
|
|
|
|
func intIdentifier(typ claims.IdentityType, id string, expected ...claims.IdentityType) (int64, error) {
|
|
|
|
if claims.IsIdentityType(typ, expected...) {
|
|
|
|
id, err := strconv.ParseInt(id, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("unrecognized format for valid type %s: %w", typ, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if id < 1 {
|
|
|
|
return 0, ErrIdentifierNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, nil
|
2023-10-09 09:07:28 -05:00
|
|
|
}
|
|
|
|
|
2024-08-13 03:18:28 -05:00
|
|
|
return 0, ErrNotIntIdentifier
|
2023-10-09 09:07:28 -05:00
|
|
|
}
|