github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/systemauth/resolver.go (about) 1 package systemauth 2 3 import ( 4 "context" 5 6 "github.com/kyma-incubator/compass/components/director/internal/model" 7 8 "github.com/kyma-incubator/compass/components/director/pkg/log" 9 pkgmodel "github.com/kyma-incubator/compass/components/director/pkg/model" 10 11 "github.com/kyma-incubator/compass/components/director/pkg/graphql" 12 "github.com/kyma-incubator/compass/components/director/pkg/persistence" 13 "github.com/pkg/errors" 14 ) 15 16 // SystemAuthService missing godoc 17 //go:generate mockery --name=SystemAuthService --output=automock --outpkg=automock --case=underscore --disable-version-string 18 type SystemAuthService interface { 19 GetByIDForObject(ctx context.Context, objectType pkgmodel.SystemAuthReferenceObjectType, authID string) (*pkgmodel.SystemAuth, error) 20 GetGlobal(ctx context.Context, id string) (*pkgmodel.SystemAuth, error) 21 GetByToken(ctx context.Context, token string) (*pkgmodel.SystemAuth, error) 22 DeleteByIDForObject(ctx context.Context, objectType pkgmodel.SystemAuthReferenceObjectType, authID string) error 23 Update(ctx context.Context, item *pkgmodel.SystemAuth) error 24 UpdateValue(ctx context.Context, id string, item *model.Auth) (*pkgmodel.SystemAuth, error) 25 InvalidateToken(ctx context.Context, id string) (*pkgmodel.SystemAuth, error) 26 } 27 28 // OAuth20Service missing godoc 29 //go:generate mockery --name=OAuth20Service --output=automock --outpkg=automock --case=underscore --disable-version-string 30 type OAuth20Service interface { 31 DeleteClientCredentials(ctx context.Context, clientID string) error 32 } 33 34 // OneTimeTokenService missing godoc 35 //go:generate mockery --name=OneTimeTokenService --output=automock --outpkg=automock --case=underscore --disable-version-string 36 type OneTimeTokenService interface { 37 IsTokenValid(systemAuth *pkgmodel.SystemAuth) (bool, error) 38 } 39 40 // SystemAuthConverter missing godoc 41 //go:generate mockery --name=SystemAuthConverter --output=automock --outpkg=automock --case=underscore --disable-version-string 42 type SystemAuthConverter interface { 43 ToGraphQL(model *pkgmodel.SystemAuth) (graphql.SystemAuth, error) 44 } 45 46 // Resolver missing godoc 47 type Resolver struct { 48 transact persistence.Transactioner 49 svc SystemAuthService 50 oAuth20Svc OAuth20Service 51 conv SystemAuthConverter 52 authConv AuthConverter 53 onetimetokenSvc OneTimeTokenService 54 } 55 56 // NewResolver missing godoc 57 func NewResolver(transact persistence.Transactioner, svc SystemAuthService, oAuth20Svc OAuth20Service, onetimetokenSvc OneTimeTokenService, conv SystemAuthConverter, authConverter AuthConverter) *Resolver { 58 return &Resolver{transact: transact, svc: svc, oAuth20Svc: oAuth20Svc, onetimetokenSvc: onetimetokenSvc, conv: conv, authConv: authConverter} 59 } 60 61 // GenericDeleteSystemAuth missing godoc 62 func (r *Resolver) GenericDeleteSystemAuth(objectType pkgmodel.SystemAuthReferenceObjectType) func(ctx context.Context, id string) (graphql.SystemAuth, error) { 63 return func(ctx context.Context, id string) (graphql.SystemAuth, error) { 64 tx, err := r.transact.Begin() 65 if err != nil { 66 return nil, err 67 } 68 defer r.transact.RollbackUnlessCommitted(ctx, tx) 69 70 ctx = persistence.SaveToContext(ctx, tx) 71 72 item, err := r.svc.GetByIDForObject(ctx, objectType, id) 73 if err != nil { 74 return nil, err 75 } 76 77 deletedItem, err := r.conv.ToGraphQL(item) 78 if err != nil { 79 return nil, errors.Wrap(err, "while converting SystemAuth to GraphQL") 80 } 81 82 if item.Value != nil && item.Value.Credential.Oauth != nil { 83 err := r.oAuth20Svc.DeleteClientCredentials(ctx, item.Value.Credential.Oauth.ClientID) 84 if err != nil { 85 return nil, errors.Wrap(err, "while deleting OAuth 2.0 client") 86 } 87 } 88 89 err = r.svc.DeleteByIDForObject(ctx, objectType, id) 90 if err != nil { 91 return nil, err 92 } 93 94 err = tx.Commit() 95 if err != nil { 96 return nil, err 97 } 98 99 return deletedItem, nil 100 } 101 } 102 103 // SystemAuth get a SystemAuth by ID 104 func (r *Resolver) SystemAuth(ctx context.Context, id string) (graphql.SystemAuth, error) { 105 tx, err := r.transact.Begin() 106 if err != nil { 107 return nil, err 108 } 109 defer r.transact.RollbackUnlessCommitted(ctx, tx) 110 111 ctx = persistence.SaveToContext(ctx, tx) 112 113 systemAuth, err := r.svc.GetGlobal(ctx, id) 114 if err != nil { 115 return nil, err 116 } 117 118 if err = tx.Commit(); err != nil { 119 return nil, err 120 } 121 122 return r.conv.ToGraphQL(systemAuth) 123 } 124 125 // SystemAuthByToken gets a SystemAuth by a provided one time token 126 func (r *Resolver) SystemAuthByToken(ctx context.Context, token string) (graphql.SystemAuth, error) { 127 tx, err := r.transact.Begin() 128 if err != nil { 129 return nil, err 130 } 131 defer r.transact.RollbackUnlessCommitted(ctx, tx) 132 133 ctx = persistence.SaveToContext(ctx, tx) 134 135 systemAuth, err := r.svc.GetByToken(ctx, token) 136 if err != nil { 137 return nil, err 138 } 139 140 if err = tx.Commit(); err != nil { 141 return nil, err 142 } 143 144 if _, err := r.onetimetokenSvc.IsTokenValid(systemAuth); err != nil { 145 return nil, err 146 } 147 148 return r.conv.ToGraphQL(systemAuth) 149 } 150 151 // UpdateSystemAuth updates a SystemAuth with an AuthInput 152 func (r *Resolver) UpdateSystemAuth(ctx context.Context, id string, in graphql.AuthInput) (graphql.SystemAuth, error) { 153 tx, err := r.transact.Begin() 154 if err != nil { 155 return nil, err 156 } 157 defer r.transact.RollbackUnlessCommitted(ctx, tx) 158 159 ctx = persistence.SaveToContext(ctx, tx) 160 161 log.C(ctx).Infof("Updating System Auth with id %s", id) 162 163 convertedIn, err := r.authConv.ModelFromGraphQLInput(in) 164 if err != nil { 165 return nil, err 166 } 167 168 systemAuth, err := r.svc.UpdateValue(ctx, id, convertedIn) 169 if err != nil { 170 return nil, err 171 } 172 173 if err = tx.Commit(); err != nil { 174 return nil, err 175 } 176 177 log.C(ctx).Infof("System Auth with id %s successfully updated", id) 178 179 return r.conv.ToGraphQL(systemAuth) 180 } 181 182 // InvalidateSystemAuthOneTimeToken checks if the the OTT for the SystemAuth is valid. If yes, it invalidates the OTT. If not, returns an error 183 func (r *Resolver) InvalidateSystemAuthOneTimeToken(ctx context.Context, id string) (graphql.SystemAuth, error) { 184 tx, err := r.transact.Begin() 185 if err != nil { 186 return nil, err 187 } 188 defer r.transact.RollbackUnlessCommitted(ctx, tx) 189 190 ctx = persistence.SaveToContext(ctx, tx) 191 192 systemAuth, err := r.svc.GetGlobal(ctx, id) 193 if err != nil { 194 return nil, err 195 } 196 197 if _, err := r.onetimetokenSvc.IsTokenValid(systemAuth); err != nil { 198 return nil, err 199 } 200 201 systemAuth, err = r.svc.InvalidateToken(ctx, id) 202 if err != nil { 203 return nil, err 204 } 205 206 if err = tx.Commit(); err != nil { 207 return nil, err 208 } 209 210 return r.conv.ToGraphQL(systemAuth) 211 }