| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "encoding/base64" |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 13 | "gorm.io/gorm" |
| 14 | "gorm.io/plugin/dbresolver" |
| 15 | ) |
| 16 | |
| 17 | type RosterAuthority struct { |
| 18 | store *Store |
| 19 | clock team.Clock |
| 20 | } |
| 21 | |
| 22 | var _ team.Authority = (*RosterAuthority)(nil) |
| 23 | |
| 24 | func NewAuthority(store *Store, clock team.Clock) *RosterAuthority { |
| 25 | if clock == nil { |
| 26 | clock = team.RealClock{} |
| 27 | } |
| 28 | return &RosterAuthority{store: store, clock: clock} |
| 29 | } |
| 30 | |
| 31 | type grantReference struct { |
| 32 | Version int `json:"version"` |
| 33 | ProjectID int64 `json:"project_id"` |
| 34 | Address string `json:"address"` |
| 35 | } |
| 36 | |
| 37 | // GrantRef identifies a project and person; it is not a credential. The trusted |
| 38 | // job-creation adapter must derive it and Requester from the verified session. |
| 39 | // Possession of a ref never authorizes an HTTP request. |
| 40 | func GrantRef(projectID int64, address string) string { |
| 41 | address = normalizeAddress(address) |
| 42 | if projectID <= 0 || !validAddress(address) { |
| 43 | return "" |
| 44 | } |
| 45 | data, _ := json.Marshal(grantReference{Version: 1, ProjectID: projectID, Address: address}) |
| 46 | return base64.RawURLEncoding.EncodeToString(data) |
| 47 | } |
| 48 | |
| 49 | func parseGrantReference(ref string) (grantReference, error) { |
| 50 | var grant grantReference |
| 51 | data, err := base64.RawURLEncoding.Strict().DecodeString(ref) |
| 52 | if err != nil || json.Unmarshal(data, &grant) != nil || grant.Version != 1 || grant.ProjectID <= 0 || !validAddress(grant.Address) { |
| 53 | return grantReference{}, team.ErrGrantRevoked |
| 54 | } |
| 55 | // One encoding rejects extra fields, duplicate keys and address aliases. |
| 56 | if ref != GrantRef(grant.ProjectID, grant.Address) { |
| 57 | return grantReference{}, team.ErrGrantRevoked |
| 58 | } |
| 59 | return grant, nil |
| 60 | } |
| 61 | |
| 62 | func (a *RosterAuthority) Introspect(ctx context.Context, req team.AdmissionRequest) (team.Admission, error) { |
| 63 | if a == nil || a.store == nil || a.store.db == nil || a.store.db.G == nil || a.clock == nil { |
| 64 | return team.Admission{}, team.ErrAuthorityUnavailable |
| 65 | } |
| 66 | if err := ctx.Err(); err != nil { |
| 67 | return team.Admission{}, fmt.Errorf("%w: %w", team.ErrAuthorityUnavailable, err) |
| 68 | } |
| 69 | if req.Target.Kind == team.TargetConversation { |
| 70 | return a.coordinator(ctx, req) |
| 71 | } |
| 72 | grant, err := parseGrantReference(req.Grant) |
| 73 | if err != nil { |
| 74 | return team.Admission{}, err |
| 75 | } |
| 76 | if req.Project != team.ProjectID(grant.ProjectID) { |
| 77 | return team.Admission{}, team.ErrGrantRevoked |
| 78 | } |
| 79 | switch req.Operation { |
| 80 | case team.OpJobContinue, team.OpAttemptContinue, team.OpProviderCall, team.OpForgePublish: |
| 81 | default: |
| 82 | return team.Admission{}, team.ErrGrantRevoked |
| 83 | } |
| 84 | readOnly := req.Target.Kind == team.TargetProjectRead |
| 85 | if readOnly && (req.Operation == team.OpForgePublish || req.Target.Ref != "" || req.Target.DefaultRef != "") { |
| 86 | return team.Admission{}, team.ErrGrantRevoked |
| 87 | } |
| 88 | if (!readOnly && req.Target.Kind != team.TargetRepository) || req.Target.Subject != "" || req.Target.BaseRevision == "" { |
| 89 | return team.Admission{}, team.ErrGrantRevoked |
| 90 | } |
| 91 | var admission team.Admission |
| 92 | check := func(tx *gorm.DB) error { |
| 93 | var instance Team |
| 94 | if err := tx.First(&instance).Error; err != nil { |
| 95 | return err |
| 96 | } |
| 97 | if req.Account != team.AccountID(instance.ID) { |
| 98 | return team.ErrGrantRevoked |
| 99 | } |
| 100 | var project Project |
| 101 | if err := tx.First(&project, grant.ProjectID).Error; err != nil { |
| 102 | return err |
| 103 | } |
| 104 | repository, err := canonicalRepository(req.Target.Repository) |
| 105 | if err != nil || repository != req.Target.Repository || repository != project.Repository || req.Target.App != project.AppName { |
| 106 | return team.ErrGrantRevoked |
| 107 | } |
| 108 | if !readOnly { |
| 109 | savedDefault, err := CanonicalBranch(project.DefaultBranch) |
| 110 | if err != nil { |
| 111 | return team.ErrGrantRevoked |
| 112 | } |
| 113 | targetDefault, err := CanonicalBranch(req.Target.DefaultRef) |
| 114 | if err != nil || targetDefault != savedDefault { |
| 115 | return team.ErrGrantRevoked |
| 116 | } |
| 117 | ref, err := CanonicalBranch(req.Target.Ref) |
| 118 | if err != nil || ref == savedDefault { |
| 119 | return team.ErrGrantRevoked |
| 120 | } |
| 121 | } |
| 122 | member, err := memberAt(tx, grant.Address) |
| 123 | if err != nil { |
| 124 | return err |
| 125 | } |
| 126 | if !member.Active { |
| 127 | return team.ErrGrantRevoked |
| 128 | } |
| 129 | if member.Role != RoleOwner && member.Role != RoleMember { |
| 130 | return team.ErrGrantRevoked |
| 131 | } |
| 132 | if !readOnly && member.Role != RoleOwner { |
| 133 | if member.Role != RoleMember { |
| 134 | return team.ErrGrantRevoked |
| 135 | } |
| 136 | var owner DecisionOwner |
| 137 | if err := tx.Where("project_id = ? AND decision = ?", project.ID, "work").First(&owner).Error; err != nil { |
| 138 | return err |
| 139 | } |
| 140 | if owner.Address != member.Address { |
| 141 | return team.ErrGrantRevoked |
| 142 | } |
| 143 | } |
| 144 | now := a.clock.Now() |
| 145 | admission = team.Admission{Grant: req.Grant, Version: project.GrantVersion, Epoch: instance.RosterEpoch, IssuedAt: now, ExpiresAt: now.Add(5 * time.Second)} |
| 146 | return nil |
| 147 | } |
| 148 | if tx, ok := ctx.Value(runnerTxKey{}).(*gorm.DB); ok { |
| 149 | err = check(tx) |
| 150 | } else { |
| 151 | err = a.store.db.G.WithContext(ctx).Clauses(dbresolver.Read).Transaction(check, &sql.TxOptions{ReadOnly: true}) |
| 152 | } |
| 153 | if err != nil { |
| 154 | if errors.Is(err, team.ErrGrantRevoked) || errors.Is(err, gorm.ErrRecordNotFound) || errors.Is(err, ErrNotFound) { |
| 155 | return team.Admission{}, team.ErrGrantRevoked |
| 156 | } |
| 157 | return team.Admission{}, fmt.Errorf("%w: %w", team.ErrAuthorityUnavailable, err) |
| 158 | } |
| 159 | return admission, nil |
| 160 | } |
| 161 | |