| 1 | package calls |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "time" |
| 7 | |
| 8 | access "amadan.net/oficina/agent-access.git" |
| 9 | "amadan.net/oficina/home/roster" |
| 10 | "amadan.net/oficina/home/teammode" |
| 11 | ) |
| 12 | |
| 13 | // Capture standing once, before the writer. Neither retries, final authority |
| 14 | // checks nor response emission may refresh an expired decision. |
| 15 | func (a *app) delegatedStanding(ctx context.Context, subject string, person Person, capture bool) error { |
| 16 | if a.cfg.Team == nil { |
| 17 | if a.isMember(person) { |
| 18 | return nil |
| 19 | } |
| 20 | return access.Fail("forbidden", "This account cannot access Calls.") |
| 21 | } |
| 22 | state, _ := ctx.Value(delegatedResponseKey{}).(*delegatedResponseState) |
| 23 | if a.team == nil || state == nil { |
| 24 | return access.Fail("unavailable", "Team authority is unavailable.") |
| 25 | } |
| 26 | if state.subject == "" && capture { |
| 27 | state.subject, state.standing = subject, a.team.Access(ctx, subject) |
| 28 | } |
| 29 | if state.subject != subject || state.standing == nil || state.standing.Subject() != subject { |
| 30 | return access.Fail("forbidden", "The linked identity changed.") |
| 31 | } |
| 32 | switch state.standing.State() { |
| 33 | case roster.Member: |
| 34 | return nil |
| 35 | case roster.NotMember: |
| 36 | return access.Fail("forbidden", "This account cannot access Calls.") |
| 37 | default: |
| 38 | return access.Fail("unavailable", "Team membership cannot be verified now.") |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | type delegatedLeaseWriter struct { |
| 43 | http.ResponseWriter |
| 44 | state *delegatedResponseState |
| 45 | team bool |
| 46 | } |
| 47 | |
| 48 | func (w *delegatedLeaseWriter) Unwrap() http.ResponseWriter { return w.ResponseWriter } |
| 49 | func (w *delegatedLeaseWriter) SetWriteDeadline(deadline time.Time) error { |
| 50 | if w.team && !deadline.IsZero() { |
| 51 | if w.state == nil || w.state.standing == nil { |
| 52 | return access.Fail("unavailable", "Team authority is unavailable.") |
| 53 | } |
| 54 | for _, standing := range []*teammode.Access{w.state.standing, w.state.host} { |
| 55 | if standing == nil { |
| 56 | continue |
| 57 | } |
| 58 | now := time.Now() |
| 59 | remaining := standing.Remaining() |
| 60 | if remaining <= 0 { |
| 61 | return access.Fail("unavailable", "Team membership cannot be verified now.") |
| 62 | } |
| 63 | if until := now.Add(remaining); until.Before(deadline) { |
| 64 | deadline = until |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | return http.NewResponseController(w.ResponseWriter).SetWriteDeadline(deadline) |
| 69 | } |
| 70 | |