package calls import ( "context" "net/http" "time" access "amadan.net/oficina/agent-access.git" "amadan.net/oficina/home/roster" "amadan.net/oficina/home/teammode" ) // Capture standing once, before the writer. Neither retries, final authority // checks nor response emission may refresh an expired decision. func (a *app) delegatedStanding(ctx context.Context, subject string, person Person, capture bool) error { if a.cfg.Team == nil { if a.isMember(person) { return nil } return access.Fail("forbidden", "This account cannot access Calls.") } state, _ := ctx.Value(delegatedResponseKey{}).(*delegatedResponseState) if a.team == nil || state == nil { return access.Fail("unavailable", "Team authority is unavailable.") } if state.subject == "" && capture { state.subject, state.standing = subject, a.team.Access(ctx, subject) } if state.subject != subject || state.standing == nil || state.standing.Subject() != subject { return access.Fail("forbidden", "The linked identity changed.") } switch state.standing.State() { case roster.Member: return nil case roster.NotMember: return access.Fail("forbidden", "This account cannot access Calls.") default: return access.Fail("unavailable", "Team membership cannot be verified now.") } } type delegatedLeaseWriter struct { http.ResponseWriter state *delegatedResponseState team bool } func (w *delegatedLeaseWriter) Unwrap() http.ResponseWriter { return w.ResponseWriter } func (w *delegatedLeaseWriter) SetWriteDeadline(deadline time.Time) error { if w.team && !deadline.IsZero() { if w.state == nil || w.state.standing == nil { return access.Fail("unavailable", "Team authority is unavailable.") } for _, standing := range []*teammode.Access{w.state.standing, w.state.host} { if standing == nil { continue } now := time.Now() remaining := standing.Remaining() if remaining <= 0 { return access.Fail("unavailable", "Team membership cannot be verified now.") } if until := now.Add(remaining); until.Before(deadline) { deadline = until } } } return http.NewResponseController(w.ResponseWriter).SetWriteDeadline(deadline) }