mirror of
https://github.com/grafana/grafana.git
synced 2025-02-11 16:15:42 -06:00
* API: Add reqSignedIn to router groups * AuthN: Add fall through in context handler * AuthN: Add IsAnonymous field * AuthN: add priority to context aware clients * ContextHandler: Add comment * AuthN: Add a simple priority queue * AuthN: Add Name to client interface * AuthN: register clients with function * AuthN: update mock and fake to implement interface * AuthN: rewrite test without reflection * AuthN: add comment * AuthN: fix queue insert * AuthN: rewrite tests * AuthN: make the queue generic so we can reuse it for hooks * ContextHandler: Add fixme for auth headers * AuthN: remove unused variable * AuthN: use multierror * AuthN: write proper tests for queue * AuthN: Add queue item that can store the value and priority Co-authored-by: Jo <joao.guerreiro@grafana.com>
56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
package clients
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
"github.com/grafana/grafana/pkg/services/authn"
|
|
"github.com/grafana/grafana/pkg/services/org"
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
)
|
|
|
|
var _ authn.ContextAwareClient = new(Anonymous)
|
|
|
|
func ProvideAnonymous(cfg *setting.Cfg, orgService org.Service) *Anonymous {
|
|
return &Anonymous{
|
|
cfg: cfg,
|
|
log: log.New("authn.anonymous"),
|
|
orgService: orgService,
|
|
}
|
|
}
|
|
|
|
type Anonymous struct {
|
|
cfg *setting.Cfg
|
|
log log.Logger
|
|
orgService org.Service
|
|
}
|
|
|
|
func (a *Anonymous) Name() string {
|
|
return authn.ClientAnonymous
|
|
}
|
|
|
|
func (a *Anonymous) Authenticate(ctx context.Context, r *authn.Request) (*authn.Identity, error) {
|
|
o, err := a.orgService.GetByName(ctx, &org.GetOrgByNameQuery{Name: a.cfg.AnonymousOrgName})
|
|
if err != nil {
|
|
a.log.FromContext(ctx).Error("failed to find organization", "name", a.cfg.AnonymousOrgName, "error", err)
|
|
return nil, err
|
|
}
|
|
|
|
return &authn.Identity{
|
|
IsAnonymous: true,
|
|
OrgID: o.ID,
|
|
OrgName: o.Name,
|
|
OrgRoles: map[int64]org.RoleType{o.ID: org.RoleType(a.cfg.AnonymousOrgRole)},
|
|
ClientParams: authn.ClientParams{},
|
|
}, nil
|
|
}
|
|
|
|
func (a *Anonymous) Test(ctx context.Context, r *authn.Request) bool {
|
|
// If anonymous client is register it can always be used for authentication
|
|
return true
|
|
}
|
|
|
|
func (a *Anonymous) Priority() uint {
|
|
return 100
|
|
}
|