| 1 | package docs |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/http" |
| 6 | |
| 7 | "amadan.net/rastrillo/rastrillo/assertion" |
| 8 | "amadan.net/rastrillo/rastrillo/sessions" |
| 9 | "github.com/carlosframework/docs/internal/docs/principalstore" |
| 10 | ) |
| 11 | |
| 12 | // existingPrincipal is read-only; an existing browser session cannot cause an |
| 13 | // unknown incoming identity to allocate a second account during completion. |
| 14 | func (b *homeBridge) existingPrincipal(r *http.Request, session sessions.Session) (principalstore.Principal, error) { |
| 15 | t := b.store.team |
| 16 | if t == nil { |
| 17 | return principalstore.Principal{}, errIdentityRefused |
| 18 | } |
| 19 | if session.Method == homeAssertionMethod { |
| 20 | return t.principals.Resolve(r.Context(), t.bindings.Issuer, session.Subject) |
| 21 | } |
| 22 | person, e := b.existingPerson(r, session) |
| 23 | if e != nil { |
| 24 | return principalstore.Principal{}, e |
| 25 | } |
| 26 | p, e := t.principals.Person(r.Context(), person.ID) |
| 27 | if e == nil && p.Kind != principalstore.Local { |
| 28 | return principalstore.Principal{}, errIdentityRefused |
| 29 | } |
| 30 | return p, e |
| 31 | } |
| 32 | func (b *homeBridge) completeTeamPrincipal(r *http.Request, claims assertion.Claims, current *sessions.Session) error { |
| 33 | t := b.store.team |
| 34 | p, e := t.principals.Resolve(r.Context(), claims.Issuer, claims.Subject) |
| 35 | if e != nil && !errors.Is(e, principalstore.ErrRefused) { |
| 36 | return rejectHome(r, "failed", http.StatusServiceUnavailable) |
| 37 | } |
| 38 | if current != nil { |
| 39 | if e != nil { |
| 40 | return rejectHome(r, "failed", http.StatusForbidden) |
| 41 | } |
| 42 | existing, err := b.existingPrincipal(r, *current) |
| 43 | if errors.Is(err, principalstore.ErrUnavailable) || errors.Is(err, errIdentityStorage) { |
| 44 | return rejectHome(r, "failed", http.StatusServiceUnavailable) |
| 45 | } |
| 46 | if err != nil || existing.ID != p.ID { |
| 47 | return rejectHome(r, "failed", http.StatusForbidden) |
| 48 | } |
| 49 | return nil |
| 50 | } |
| 51 | if e == nil { |
| 52 | return nil |
| 53 | } |
| 54 | _, e = t.principals.EnsureHome(r.Context(), principalstore.HomeIdentity{Issuer: claims.Issuer, Subject: claims.Subject}) |
| 55 | if errors.Is(e, principalstore.ErrRefused) { |
| 56 | return rejectHome(r, "unknown", http.StatusForbidden) |
| 57 | } |
| 58 | if e != nil { |
| 59 | return rejectHome(r, "failed", http.StatusServiceUnavailable) |
| 60 | } |
| 61 | return nil |
| 62 | } |
| 63 | |