Auth: Move Service Account service to SignedInUser Interface (#73142)

* move service account service to identity interface

* Update pkg/services/auth/identity/requester.go
This commit is contained in:
Jo
2023-08-10 14:20:58 +02:00
committed by GitHub
parent 8c2f439cd7
commit 67de18ff06
5 changed files with 66 additions and 27 deletions
+31 -1
View File
@@ -1,6 +1,12 @@
package identity
import "github.com/grafana/grafana/pkg/models/roletype"
import (
"errors"
"fmt"
"strconv"
"github.com/grafana/grafana/pkg/models/roletype"
)
const (
NamespaceUser = "user"
@@ -10,6 +16,9 @@ const (
NamespaceRenderService = "render"
)
var ErrNotIntIdentifier = errors.New("identifier is not an int64")
var ErrIdentifierNotInitialized = errors.New("identifier is not initialized")
type Requester interface {
// GetDisplayName returns the display name of the active entity.
// The display name is the name if it is set, otherwise the login or email.
@@ -50,3 +59,24 @@ type Requester interface {
// HasUniqueId returns true if the entity has a unique id
HasUniqueId() bool
}
// IntIdentifier converts a string identifier to an int64.
// Applicable for users, service accounts, api keys and renderer service.
// Errors if the identifier is not initialized or if namespace is not recognized.
func IntIdentifier(namespace, identifier string) (int64, error) {
switch namespace {
case NamespaceUser, NamespaceAPIKey, NamespaceServiceAccount, NamespaceRenderService:
id, err := strconv.ParseInt(identifier, 10, 64)
if err != nil {
return 0, fmt.Errorf("unrecognized format for valid namespace %s: %w", namespace, err)
}
if id < 1 {
return 0, ErrIdentifierNotInitialized
}
return id, nil
}
return 0, ErrNotIntIdentifier
}