feat(identity): verify provider tokens and attribute task actions to their actor
Two halves of one boundary: a credential resolves to an identity, and an action records which identity acted and whose authority it used. resource server. internal/infrastructure/oidc verifies a provider-issued bearer token against the provider's published keys, using an OSS OIDC library: archie compares no signature, validates no claim and rotates no key itself, holds no client secret and issues no session, so a compromised archie cannot mint an identity. Audience is required rather than optional -- a token minted for another service must not authenticate here -- and discovery happens at construction, so an unreachable provider is a startup failure rather than a request-time surprise. identity.Authenticate is the single place a credential becomes an identity, and it refuses in four distinguishable ways: no credential presented, a credential that did not verify (wrong key, expired, wrong issuer, wrong audience, malformed), a verified subject with no identity bound to it, and an identity that may not act. The order is deliberate: verification happens before any field of the token is used, so nothing an unverified token carries can select an identity. A suspended identity is refused even though its credential verified, because suspension is archie's decision and must not depend on the provider withdrawing a token. The binding is the second bridge beside the legacy-name alias: identity_subjects keys (issuer, subject) so the same subject string from another issuer is a different caller, and rebinding moves a subject so two identities can never claim one provider subject. The contract Authenticate needs is SubjectResolver, narrower than Repository, so a consumer that only authenticates does not take the whole identity CRUD surface -- and staterpc.Client, which asserts Repository, does not need a new RPC to satisfy it. attribution. taskactions now records two identities per action: the acting identity and the authorising principal, both on the event row as new columns added through the existing migration table. The rule that the bug turned on is enforced by the event kind describing the actor: a person's approval is human_approved, an agent's is agent_approved, and an action with no verified actor is task_approved, a task-level event that credits nobody. An agent acting under a person's standing approval records itself as the actor and that person as the principal, so "approved by came from me" is answerable from the record rather than asserted. Scope and actor are separated: scope decides which task a caller may touch and is never promoted into an actor, because the dashboard's credential is a shared token and possession of it is not evidence that a person was at the keyboard. That last point is a deliberate fidelity trade, and the reason the transport is not wired: the dashboard action path passes no actor yet, so dashboard approvals are now recorded as task_approved rather than claimed as a human's. Recording nothing false is the requirement; naming the human verifiably needs the dashboard to authenticate a token and resolve it, which needs two new state-store RPCs (ResolveIdentitySubject, BindIdentitySubject) -- the next piece, not a rewrite: the verifier is a one-method interface, so introspection would be a second implementation of it. Verified: task check exit 0; focused runs recorded in the handoff. Opaque authelia_at_ tokens were inspected rather than assumed and cannot be verified against public keys; infra is configuring access_token_signed_response_alg RS256 on the archie client, with audience https://archie.catlow.cloud and issuer https://auth.catlow.cloud.
This commit is contained in:
19 files changed
+1146
-88
No files matched your search
@@ -0,0 +1,108 @@
|
||||
package identity
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Subject is what an identity provider asserts about a caller. Issuer is part
|
||||
// of the key because two providers can assert the same subject string for
|
||||
// different people, and a binding keyed on the subject alone would accept one
|
||||
// for the other.
|
||||
type Subject struct {
|
||||
Issuer string `json:"issuer"`
|
||||
Subject string `json:"subject"`
|
||||
}
|
||||
|
||||
func (s Subject) Validate() error {
|
||||
if strings.TrimSpace(s.Issuer) == "" || strings.TrimSpace(s.Subject) == "" {
|
||||
return fmt.Errorf("%w: issuer and subject are required", ErrInvalid)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Credential is what a provider-issued token proved. It is the result of
|
||||
// verification, never an assertion from the caller.
|
||||
type Credential struct {
|
||||
Subject Subject
|
||||
Scopes []string
|
||||
Expires time.Time
|
||||
}
|
||||
|
||||
// Verifier checks a presented bearer token against the identity provider's
|
||||
// published keys. Implementations hold no secret and mint nothing: they can
|
||||
// confirm an identity the provider issued, and can never produce one.
|
||||
type Verifier interface {
|
||||
Verify(ctx context.Context, rawToken string) (Credential, error)
|
||||
}
|
||||
|
||||
// SubjectResolver resolves a verified provider subject to the identity bound to
|
||||
// it. It is deliberately narrower than Repository: authenticating a request
|
||||
// needs to resolve a subject, not to administer identities, so a consumer that
|
||||
// only authenticates does not take a dependency on the whole identity CRUD
|
||||
// surface.
|
||||
type SubjectResolver interface {
|
||||
// ResolveSubject returns the identity a provider subject is bound to. It is
|
||||
// the second binding beside the legacy-name alias: a name archie was
|
||||
// configured with resolves to an identity, and a subject a provider asserts
|
||||
// resolves to the same identity.
|
||||
ResolveSubject(context.Context, Subject) (Identity, error)
|
||||
}
|
||||
|
||||
var (
|
||||
// ErrNoCredential means the request presented nothing to authenticate with.
|
||||
ErrNoCredential = errors.New("no credential presented")
|
||||
// ErrCredentialRejected means a credential was presented and did not
|
||||
// verify: wrong signature, expired, wrong issuer, wrong audience, or
|
||||
// malformed. It never means the credential was merely unrecognised.
|
||||
ErrCredentialRejected = errors.New("credential rejected")
|
||||
// ErrSubjectUnbound means a verified subject has no identity bound to it.
|
||||
// The provider authenticated someone archie does not know.
|
||||
ErrSubjectUnbound = errors.New("credential subject is not bound to an identity")
|
||||
// ErrIdentityInactive means the identity the credential belongs to may not
|
||||
// act: suspended or retired. Authentication succeeded and authorisation
|
||||
// did not, which is why it is a distinct refusal from a bad credential.
|
||||
ErrIdentityInactive = errors.New("identity may not act")
|
||||
)
|
||||
|
||||
// Authenticate resolves a presented credential to the identity that may act.
|
||||
//
|
||||
// The order is deliberate: the credential is verified by the provider's own
|
||||
// keys before anything it contains is used, so no field of an unverified token
|
||||
// can select an identity. A suspended identity is refused even though its
|
||||
// credential verified, because suspension is archie's decision and must not
|
||||
// depend on the provider withdrawing a token.
|
||||
func Authenticate(ctx context.Context, subjects SubjectResolver, verifier Verifier, rawToken string) (Identity, Credential, error) {
|
||||
if strings.TrimSpace(rawToken) == "" {
|
||||
return Identity{}, Credential{}, ErrNoCredential
|
||||
}
|
||||
if verifier == nil {
|
||||
return Identity{}, Credential{}, fmt.Errorf("%w: no identity provider is configured", ErrCredentialRejected)
|
||||
}
|
||||
credential, err := verifier.Verify(ctx, rawToken)
|
||||
if err != nil {
|
||||
return Identity{}, Credential{}, fmt.Errorf("%w: %w", ErrCredentialRejected, err)
|
||||
}
|
||||
if err := credential.Subject.Validate(); err != nil {
|
||||
return Identity{}, Credential{}, fmt.Errorf("%w: %w", ErrCredentialRejected, err)
|
||||
}
|
||||
value, err := subjects.ResolveSubject(ctx, credential.Subject)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
return Identity{}, Credential{}, fmt.Errorf("%w: %s", ErrSubjectUnbound, credential.Subject.Subject)
|
||||
}
|
||||
return Identity{}, Credential{}, err
|
||||
}
|
||||
if !value.CanAct() {
|
||||
return Identity{}, Credential{}, fmt.Errorf("%w: %s is %s", ErrIdentityInactive, value.ID, value.Lifecycle)
|
||||
}
|
||||
return value, credential, nil
|
||||
}
|
||||
|
||||
// CanAct reports whether an identity may perform actions. Only an active
|
||||
// identity may: a suspended identity is suspended from acting, and a retired
|
||||
// one is kept for attribution, not for use.
|
||||
func (i Identity) CanAct() bool { return i.Lifecycle == LifecycleActive }
|
||||
@@ -0,0 +1,161 @@
|
||||
package identity
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type stubVerifier struct {
|
||||
credential Credential
|
||||
err error
|
||||
}
|
||||
|
||||
func (s stubVerifier) Verify(context.Context, string) (Credential, error) {
|
||||
return s.credential, s.err
|
||||
}
|
||||
|
||||
type stubSubjects struct {
|
||||
value Identity
|
||||
err error
|
||||
}
|
||||
|
||||
func (s stubSubjects) ResolveSubject(context.Context, Subject) (Identity, error) {
|
||||
return s.value, s.err
|
||||
}
|
||||
|
||||
func testIdentity(kind Kind, lifecycle Lifecycle) Identity {
|
||||
return Identity{ID: "10000000-0000-5000-8000-000000000001", Kind: kind, DisplayName: "tester", Lifecycle: lifecycle, Version: 1}
|
||||
}
|
||||
|
||||
func TestAuthenticateRefusesEveryUnprovenCaller(t *testing.T) {
|
||||
verified := Credential{Subject: Subject{Issuer: "https://idp.example", Subject: "sam"}}
|
||||
rejected := errors.New("signature verification failed")
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
rawToken string
|
||||
verifier Verifier
|
||||
subjects SubjectResolver
|
||||
want error
|
||||
}{
|
||||
{
|
||||
name: "no credential presented",
|
||||
rawToken: "",
|
||||
verifier: stubVerifier{credential: verified},
|
||||
subjects: stubSubjects{value: testIdentity(KindUser, LifecycleActive)},
|
||||
want: ErrNoCredential,
|
||||
},
|
||||
{
|
||||
name: "whitespace is not a credential",
|
||||
rawToken: " ",
|
||||
verifier: stubVerifier{credential: verified},
|
||||
subjects: stubSubjects{value: testIdentity(KindUser, LifecycleActive)},
|
||||
want: ErrNoCredential,
|
||||
},
|
||||
{
|
||||
name: "no provider is configured, so nothing can be verified",
|
||||
rawToken: "token",
|
||||
verifier: nil,
|
||||
subjects: stubSubjects{value: testIdentity(KindUser, LifecycleActive)},
|
||||
want: ErrCredentialRejected,
|
||||
},
|
||||
{
|
||||
name: "token signed by the wrong key is rejected",
|
||||
rawToken: "token",
|
||||
verifier: stubVerifier{err: rejected},
|
||||
subjects: stubSubjects{value: testIdentity(KindUser, LifecycleActive)},
|
||||
want: ErrCredentialRejected,
|
||||
},
|
||||
{
|
||||
name: "verified credential with no identity bound to its subject",
|
||||
rawToken: "token",
|
||||
verifier: stubVerifier{credential: verified},
|
||||
subjects: stubSubjects{err: ErrNotFound},
|
||||
want: ErrSubjectUnbound,
|
||||
},
|
||||
{
|
||||
name: "verified credential for a suspended identity",
|
||||
rawToken: "token",
|
||||
verifier: stubVerifier{credential: verified},
|
||||
subjects: stubSubjects{value: testIdentity(KindBot, LifecycleSuspended)},
|
||||
want: ErrIdentityInactive,
|
||||
},
|
||||
{
|
||||
name: "verified credential for a retired identity",
|
||||
rawToken: "token",
|
||||
verifier: stubVerifier{credential: verified},
|
||||
subjects: stubSubjects{value: testIdentity(KindBot, LifecycleRetired)},
|
||||
want: ErrIdentityInactive,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got, _, err := Authenticate(context.Background(), tc.subjects, tc.verifier, tc.rawToken)
|
||||
if !errors.Is(err, tc.want) {
|
||||
t.Fatalf("Authenticate() error = %v, want %v", err, tc.want)
|
||||
}
|
||||
if got != (Identity{}) {
|
||||
t.Fatalf("Authenticate() returned identity %+v on a refusal", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthenticateResolvesAnActiveIdentity(t *testing.T) {
|
||||
want := testIdentity(KindBot, LifecycleActive)
|
||||
verified := Credential{
|
||||
Subject: Subject{Issuer: "https://idp.example", Subject: "archie-agent"},
|
||||
Scopes: []string{"tasks:write"},
|
||||
}
|
||||
|
||||
got, credential, err := Authenticate(context.Background(), stubSubjects{value: want}, stubVerifier{credential: verified}, "token")
|
||||
if err != nil {
|
||||
t.Fatalf("Authenticate() error = %v", err)
|
||||
}
|
||||
if got != want {
|
||||
t.Fatalf("Authenticate() identity = %+v, want %+v", got, want)
|
||||
}
|
||||
if credential.Subject != verified.Subject {
|
||||
t.Fatalf("Authenticate() credential subject = %+v, want %+v", credential.Subject, verified.Subject)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCanActOnlyForAnActiveIdentity(t *testing.T) {
|
||||
tests := []struct {
|
||||
lifecycle Lifecycle
|
||||
want bool
|
||||
}{
|
||||
{LifecycleActive, true},
|
||||
{LifecycleSuspended, false},
|
||||
{LifecycleRetired, false},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
value := testIdentity(KindUser, tc.lifecycle)
|
||||
if got := value.CanAct(); got != tc.want {
|
||||
t.Errorf("Identity{%s}.CanAct() = %t, want %t", tc.lifecycle, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubjectRequiresBothHalves(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
subject Subject
|
||||
wantErr bool
|
||||
}{
|
||||
{"complete", Subject{Issuer: "https://idp.example", Subject: "sam"}, false},
|
||||
{"no issuer", Subject{Subject: "sam"}, true},
|
||||
{"no subject", Subject{Issuer: "https://idp.example"}, true},
|
||||
{"blank", Subject{Issuer: " ", Subject: " "}, true},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := tc.subject.Validate()
|
||||
if tc.wantErr != (err != nil) {
|
||||
t.Fatalf("Subject.Validate() error = %v, wantErr %t", err, tc.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user