Files
archie/internal/daemon/identity.go
T

59 lines
2.0 KiB
Go

package daemon
import (
"context"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/taskstate"
)
// identityOrg resolves an identity's org. No PrincipalSource means the
// default org; a store error is returned.
// orgResolver is the identity-to-org surface the identity checks read through.
type orgResolver interface {
identityOrg(ctx context.Context, id identity.IdentityID) (org.OrgID, error)
identityMayAct(ctx context.Context, name string) (bool, string, taskstate.ParkClass)
identityActive(ctx context.Context, id identity.IdentityID) bool
}
var _ orgResolver = (*Daemon)(nil)
func (d *Daemon) identityOrg(ctx context.Context, id identity.IdentityID) (org.OrgID, error) {
principal, err := d.Principals.PrincipalFor(ctx, id)
if err != nil {
return "", err
}
return principal.Org, nil
}
func (d *Daemon) identityActive(ctx context.Context, id identity.IdentityID) bool {
if id == "" {
d.Log.ErrorContext(ctx, "identity lifecycle check failed", "err", "identity ID is empty")
return false
}
value, err := d.IdentityRepository.Get(ctx, id)
if err != nil {
d.Log.ErrorContext(ctx, "identity lifecycle check failed", "identity", id, "err", err)
return false
}
return value.Lifecycle == identity.LifecycleActive
}
// identityMayAct reports whether a task's identity may act, and the park
// class if not. An empty, unknown or inactive identity never falls back to
// the System forge.
func (d *Daemon) identityMayAct(ctx context.Context, name string) (bool, string, taskstate.ParkClass) {
if name == "" {
return false, "task has no identity", taskstate.ParkNeedsDecision
}
value, err := d.IdentityRepository.Get(ctx, identity.IdentityID(name))
if err != nil {
return false, "execution identity unavailable: " + err.Error(), taskstate.ParkTransient
}
if value.Lifecycle != identity.LifecycleActive {
return false, "identity may not act", taskstate.ParkNeedsDecision
}
return true, "", ""
}