Auth: use IdentityType from authlib (#91763)

This commit is contained in:
Ryan McKinley
2024-08-12 09:26:53 +03:00
committed by GitHub
parent faf7cb9312
commit 21d4a4f49e
70 changed files with 230 additions and 212 deletions
+3 -3
View File
@@ -14,7 +14,7 @@ type Requester interface {
claims.AuthInfo
// GetIdentityType returns the type for the requester
GetIdentityType() IdentityType
GetIdentityType() claims.IdentityType
// GetRawIdentifier returns only the identifier part of the UID, excluding the type
GetRawIdentifier() string
// Deprecated: use GetUID instead
@@ -82,7 +82,7 @@ type Requester interface {
// Applicable for users, service accounts, api keys and renderer service.
// Errors if the identifier is not initialized or if type is not recognized.
func IntIdentifier(typedID TypedID) (int64, error) {
if IsIdentityType(typedID, TypeUser, TypeAPIKey, TypeServiceAccount, TypeRenderService) {
if claims.IsIdentityType(typedID.t, claims.TypeUser, claims.TypeAPIKey, claims.TypeServiceAccount, claims.TypeRenderService) {
id, err := strconv.ParseInt(typedID.ID(), 10, 64)
if err != nil {
return 0, fmt.Errorf("unrecognized format for valid type %s: %w", typedID.Type(), err)
@@ -107,7 +107,7 @@ func UserIdentifier(typedID TypedID) (int64, error) {
return 0, err
}
if IsIdentityType(typedID, TypeUser, TypeServiceAccount) {
if claims.IsIdentityType(typedID.t, claims.TypeUser, claims.TypeServiceAccount) {
return userID, nil
}
+2 -2
View File
@@ -14,7 +14,7 @@ var _ Requester = &StaticRequester{}
// This is mostly copied from:
// https://github.com/grafana/grafana/blob/v11.0.0/pkg/services/user/identity.go#L16
type StaticRequester struct {
Type IdentityType
Type claims.IdentityType
UserID int64
UserUID string
OrgID int64
@@ -65,7 +65,7 @@ func (u *StaticRequester) GetInternalID() (int64, error) {
}
// GetIdentityType implements Requester.
func (u *StaticRequester) GetIdentityType() IdentityType {
func (u *StaticRequester) GetIdentityType() claims.IdentityType {
return u.Type
}
+11 -45
View File
@@ -4,46 +4,12 @@ import (
"fmt"
"strconv"
"strings"
"github.com/grafana/authlib/claims"
)
type IdentityType string
const (
TypeUser IdentityType = "user"
TypeAPIKey IdentityType = "api-key"
TypeServiceAccount IdentityType = "service-account"
TypeAnonymous IdentityType = "anonymous"
TypeRenderService IdentityType = "render"
TypeAccessPolicy IdentityType = "access-policy"
TypeProvisioning IdentityType = "provisioning"
TypeEmpty IdentityType = ""
)
func (n IdentityType) String() string {
return string(n)
}
func ParseType(str string) (IdentityType, error) {
switch str {
case string(TypeUser):
return TypeUser, nil
case string(TypeAPIKey):
return TypeAPIKey, nil
case string(TypeServiceAccount):
return TypeServiceAccount, nil
case string(TypeAnonymous):
return TypeAnonymous, nil
case string(TypeRenderService):
return TypeRenderService, nil
case string(TypeAccessPolicy):
return TypeAccessPolicy, nil
default:
return "", ErrInvalidTypedID.Errorf("got invalid identity type %s", str)
}
}
// IsIdentityType returns true if typedID matches any expected identity type
func IsIdentityType(typedID TypedID, expected ...IdentityType) bool {
func IsIdentityType(typedID TypedID, expected ...claims.IdentityType) bool {
for _, e := range expected {
if typedID.Type() == e {
return true
@@ -53,7 +19,7 @@ func IsIdentityType(typedID TypedID, expected ...IdentityType) bool {
return false
}
var AnonymousTypedID = NewTypedID(TypeAnonymous, 0)
var AnonymousTypedID = NewTypedID(claims.TypeAnonymous, 0)
func ParseTypedID(str string) (TypedID, error) {
var typeID TypedID
@@ -63,7 +29,7 @@ func ParseTypedID(str string) (TypedID, error) {
return typeID, ErrInvalidTypedID.Errorf("expected typed id to have 2 parts")
}
t, err := ParseType(parts[0])
t, err := claims.ParseType(parts[0])
if err != nil {
return typeID, err
}
@@ -84,7 +50,7 @@ func MustParseTypedID(str string) TypedID {
return typeID
}
func NewTypedID(t IdentityType, id int64) TypedID {
func NewTypedID(t claims.IdentityType, id int64) TypedID {
return TypedID{
id: strconv.FormatInt(id, 10),
t: t,
@@ -92,7 +58,7 @@ func NewTypedID(t IdentityType, id int64) TypedID {
}
// NewTypedIDString creates a new TypedID with a string id
func NewTypedIDString(t IdentityType, id string) TypedID {
func NewTypedIDString(t claims.IdentityType, id string) TypedID {
return TypedID{
id: id,
t: t,
@@ -102,7 +68,7 @@ func NewTypedIDString(t IdentityType, id string) TypedID {
// FIXME: use this instead of encoded string through the codebase
type TypedID struct {
id string
t IdentityType
t claims.IdentityType
}
func (ni TypedID) ID() string {
@@ -112,7 +78,7 @@ func (ni TypedID) ID() string {
// UserID will try to parse and int64 identifier if namespace is either user or service-account.
// For all other namespaces '0' will be returned.
func (ni TypedID) UserID() (int64, error) {
if ni.IsType(TypeUser, TypeServiceAccount) {
if ni.IsType(claims.TypeUser, claims.TypeServiceAccount) {
return ni.ParseInt()
}
return 0, nil
@@ -123,11 +89,11 @@ func (ni TypedID) ParseInt() (int64, error) {
return strconv.ParseInt(ni.id, 10, 64)
}
func (ni TypedID) Type() IdentityType {
func (ni TypedID) Type() claims.IdentityType {
return ni.t
}
func (ni TypedID) IsType(expected ...IdentityType) bool {
func (ni TypedID) IsType(expected ...claims.IdentityType) bool {
return IsIdentityType(ni, expected...)
}
+1 -1
View File
@@ -35,7 +35,7 @@ func (i *IDClaimsWrapper) EmailVerified() bool {
// GetIdentityType implements claims.IdentityClaims.
func (i *IDClaimsWrapper) IdentityType() claims.IdentityType {
return claims.IdentityType(i.Source.GetIdentityType())
return i.Source.GetIdentityType()
}
// GetInternalID implements claims.IdentityClaims.