auth.go 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. package middleware
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "net/url"
  10. "strconv"
  11. "strings"
  12. "github.com/go-chi/chi"
  13. "github.com/gorilla/sessions"
  14. "github.com/porter-dev/porter/internal/auth/token"
  15. "github.com/porter-dev/porter/internal/models"
  16. "github.com/porter-dev/porter/internal/repository"
  17. )
  18. // Auth implements the authorization functions
  19. type Auth struct {
  20. store sessions.Store
  21. cookieName string
  22. tokenConf *token.TokenGeneratorConf
  23. repo *repository.Repository
  24. }
  25. // NewAuth returns a new Auth instance
  26. func NewAuth(
  27. store sessions.Store,
  28. cookieName string,
  29. tokenConf *token.TokenGeneratorConf,
  30. repo *repository.Repository,
  31. ) *Auth {
  32. return &Auth{store, cookieName, tokenConf, repo}
  33. }
  34. // BasicAuthenticate just checks that a user is logged in
  35. func (auth *Auth) BasicAuthenticate(next http.Handler) http.Handler {
  36. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  37. if auth.isLoggedIn(w, r) {
  38. next.ServeHTTP(w, r)
  39. } else {
  40. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  41. return
  42. }
  43. return
  44. })
  45. }
  46. // BasicAuthenticateWithRedirect checks that a user is logged in, and if they're not, the
  47. // user is redirected to the login page with the redirect path stored in the session
  48. func (auth *Auth) BasicAuthenticateWithRedirect(next http.Handler) http.Handler {
  49. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  50. if auth.isLoggedIn(w, r) {
  51. next.ServeHTTP(w, r)
  52. } else {
  53. session, err := auth.store.Get(r, auth.cookieName)
  54. if err != nil {
  55. http.Redirect(w, r, "/dashboard", 302)
  56. }
  57. // need state parameter to validate when redirected
  58. if r.URL.RawQuery == "" {
  59. session.Values["redirect"] = r.URL.Path
  60. } else {
  61. session.Values["redirect"] = r.URL.Path + "?" + r.URL.RawQuery
  62. }
  63. session.Save(r, w)
  64. http.Redirect(w, r, "/dashboard", 302)
  65. return
  66. }
  67. return
  68. })
  69. }
  70. // IDLocation represents the location of the ID to use for authentication
  71. type IDLocation uint
  72. const (
  73. // URLParam location looks for a parameter in the URL endpoint
  74. URLParam IDLocation = iota
  75. // BodyParam location looks for a parameter in the body
  76. BodyParam
  77. // QueryParam location looks for a parameter in the query string
  78. QueryParam
  79. )
  80. type bodyUserID struct {
  81. UserID uint64 `json:"user_id"`
  82. }
  83. type bodyProjectID struct {
  84. ProjectID uint64 `json:"project_id"`
  85. }
  86. type bodyClusterID struct {
  87. ClusterID uint64 `json:"cluster_id"`
  88. }
  89. type bodyRegistryID struct {
  90. RegistryID uint64 `json:"registry_id"`
  91. }
  92. type bodyGitRepoID struct {
  93. GitRepoID uint64 `json:"git_repo_id"`
  94. }
  95. type bodyInfraID struct {
  96. InfraID uint64 `json:"infra_id"`
  97. }
  98. type bodyInviteID struct {
  99. InviteID uint64 `json:"invite_id"`
  100. }
  101. type bodyAWSIntegrationID struct {
  102. AWSIntegrationID uint64 `json:"aws_integration_id"`
  103. }
  104. type bodyGCPIntegrationID struct {
  105. GCPIntegrationID uint64 `json:"gcp_integration_id"`
  106. }
  107. type bodyDOIntegrationID struct {
  108. DOIntegrationID uint64 `json:"do_integration_id"`
  109. }
  110. // DoesUserIDMatch checks the id URL parameter and verifies that it matches
  111. // the one stored in the session
  112. func (auth *Auth) DoesUserIDMatch(next http.Handler, loc IDLocation) http.Handler {
  113. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  114. var err error
  115. id, err := findUserIDInRequest(r, loc)
  116. // first check for token
  117. tok := auth.getTokenFromRequest(r)
  118. if err != nil {
  119. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  120. return
  121. } else if tok != nil && tok.IBy == uint(id) {
  122. next.ServeHTTP(w, r)
  123. return
  124. } else if auth.doesSessionMatchID(r, uint(id)) {
  125. next.ServeHTTP(w, r)
  126. } else {
  127. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  128. return
  129. }
  130. return
  131. })
  132. }
  133. // AccessType represents the various access types for a project
  134. type AccessType string
  135. // The various access types
  136. const (
  137. ReadAccess AccessType = "read"
  138. WriteAccess AccessType = "write"
  139. )
  140. // DoesUserHaveProjectAccess looks for a project_id parameter and checks that the
  141. // user has access via the specified accessType
  142. func (auth *Auth) DoesUserHaveProjectAccess(
  143. next http.Handler,
  144. projLoc IDLocation,
  145. accessType AccessType,
  146. ) http.Handler {
  147. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  148. var err error
  149. projID, err := findProjIDInRequest(r, projLoc)
  150. if err != nil {
  151. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  152. return
  153. }
  154. // first check for token
  155. tok := auth.getTokenFromRequest(r)
  156. var userID uint
  157. if tok != nil && tok.ProjectID == uint(projID) {
  158. next.ServeHTTP(w, r)
  159. return
  160. } else if tok != nil {
  161. userID = tok.IBy
  162. } else {
  163. session, err := auth.store.Get(r, auth.cookieName)
  164. if err != nil {
  165. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  166. return
  167. }
  168. sessionUserID, ok := session.Values["user_id"]
  169. userID = sessionUserID.(uint)
  170. if !ok {
  171. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  172. return
  173. }
  174. }
  175. // get the project
  176. proj, err := auth.repo.Project.ReadProject(uint(projID))
  177. if err != nil {
  178. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  179. return
  180. }
  181. // look for the user role in the project
  182. for _, role := range proj.Roles {
  183. if role.UserID == userID {
  184. if accessType == ReadAccess {
  185. next.ServeHTTP(w, r)
  186. return
  187. } else if accessType == WriteAccess {
  188. if role.Kind == models.RoleAdmin {
  189. next.ServeHTTP(w, r)
  190. return
  191. }
  192. }
  193. }
  194. }
  195. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  196. return
  197. })
  198. }
  199. // DoesUserHaveClusterAccess looks for a project_id parameter and a
  200. // cluster_id parameter, and verifies that the cluster belongs
  201. // to the project
  202. func (auth *Auth) DoesUserHaveClusterAccess(
  203. next http.Handler,
  204. projLoc IDLocation,
  205. clusterLoc IDLocation,
  206. ) http.Handler {
  207. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  208. clusterID, err := findClusterIDInRequest(r, clusterLoc)
  209. if err != nil {
  210. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  211. return
  212. }
  213. projID, err := findProjIDInRequest(r, projLoc)
  214. if err != nil {
  215. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  216. return
  217. }
  218. // get the service accounts belonging to the project
  219. clusters, err := auth.repo.Cluster.ListClustersByProjectID(uint(projID))
  220. if err != nil {
  221. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  222. return
  223. }
  224. doesExist := false
  225. for _, cluster := range clusters {
  226. if cluster.ID == uint(clusterID) {
  227. doesExist = true
  228. break
  229. }
  230. }
  231. if doesExist {
  232. next.ServeHTTP(w, r)
  233. return
  234. }
  235. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  236. return
  237. })
  238. }
  239. // DoesUserHaveInviteAccess looks for a project_id parameter and a
  240. // invite_id parameter, and verifies that the invite belongs
  241. // to the project
  242. func (auth *Auth) DoesUserHaveInviteAccess(
  243. next http.Handler,
  244. projLoc IDLocation,
  245. inviteLoc IDLocation,
  246. ) http.Handler {
  247. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  248. inviteID, err := findInviteIDInRequest(r, inviteLoc)
  249. if err != nil {
  250. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  251. return
  252. }
  253. projID, err := findProjIDInRequest(r, projLoc)
  254. if err != nil {
  255. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  256. return
  257. }
  258. // get the service accounts belonging to the project
  259. invites, err := auth.repo.Invite.ListInvitesByProjectID(uint(projID))
  260. if err != nil {
  261. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  262. return
  263. }
  264. doesExist := false
  265. for _, invite := range invites {
  266. if invite.ID == uint(inviteID) {
  267. doesExist = true
  268. break
  269. }
  270. }
  271. if doesExist {
  272. next.ServeHTTP(w, r)
  273. return
  274. }
  275. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  276. return
  277. })
  278. }
  279. // DoesUserHaveRegistryAccess looks for a project_id parameter and a
  280. // registry_id parameter, and verifies that the registry belongs
  281. // to the project
  282. func (auth *Auth) DoesUserHaveRegistryAccess(
  283. next http.Handler,
  284. projLoc IDLocation,
  285. registryLoc IDLocation,
  286. ) http.Handler {
  287. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  288. regID, err := findRegistryIDInRequest(r, registryLoc)
  289. if err != nil {
  290. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  291. return
  292. }
  293. projID, err := findProjIDInRequest(r, projLoc)
  294. if err != nil {
  295. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  296. return
  297. }
  298. // get the service accounts belonging to the project
  299. regs, err := auth.repo.Registry.ListRegistriesByProjectID(uint(projID))
  300. if err != nil {
  301. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  302. return
  303. }
  304. doesExist := false
  305. for _, reg := range regs {
  306. if reg.ID == uint(regID) {
  307. doesExist = true
  308. break
  309. }
  310. }
  311. if doesExist {
  312. next.ServeHTTP(w, r)
  313. return
  314. }
  315. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  316. return
  317. })
  318. }
  319. // DoesUserHaveGitRepoAccess looks for a project_id parameter and a
  320. // git_repo_id parameter, and verifies that the git repo belongs
  321. // to the project
  322. func (auth *Auth) DoesUserHaveGitRepoAccess(
  323. next http.Handler,
  324. projLoc IDLocation,
  325. gitRepoLoc IDLocation,
  326. ) http.Handler {
  327. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  328. grID, err := findGitRepoIDInRequest(r, gitRepoLoc)
  329. if err != nil {
  330. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  331. return
  332. }
  333. projID, err := findProjIDInRequest(r, projLoc)
  334. if err != nil {
  335. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  336. return
  337. }
  338. // get the service accounts belonging to the project
  339. grs, err := auth.repo.GitRepo.ListGitReposByProjectID(uint(projID))
  340. if err != nil {
  341. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  342. return
  343. }
  344. doesExist := false
  345. for _, gr := range grs {
  346. if gr.ID == uint(grID) {
  347. doesExist = true
  348. break
  349. }
  350. }
  351. if doesExist {
  352. next.ServeHTTP(w, r)
  353. return
  354. }
  355. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  356. return
  357. })
  358. }
  359. // DoesUserHaveInfraAccess looks for a project_id parameter and an
  360. // infra_id parameter, and verifies that the infra belongs
  361. // to the project
  362. func (auth *Auth) DoesUserHaveInfraAccess(
  363. next http.Handler,
  364. projLoc IDLocation,
  365. infraLoc IDLocation,
  366. ) http.Handler {
  367. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  368. infraID, err := findInfraIDInRequest(r, infraLoc)
  369. if err != nil {
  370. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  371. return
  372. }
  373. projID, err := findProjIDInRequest(r, projLoc)
  374. if err != nil {
  375. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  376. return
  377. }
  378. infras, err := auth.repo.Infra.ListInfrasByProjectID(uint(projID))
  379. if err != nil {
  380. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  381. return
  382. }
  383. doesExist := false
  384. for _, infra := range infras {
  385. if infra.ID == uint(infraID) {
  386. doesExist = true
  387. break
  388. }
  389. }
  390. if doesExist {
  391. next.ServeHTTP(w, r)
  392. return
  393. }
  394. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  395. return
  396. })
  397. }
  398. // DoesUserHaveAWSIntegrationAccess looks for a project_id parameter and an
  399. // aws_integration_id parameter, and verifies that the infra belongs
  400. // to the project
  401. func (auth *Auth) DoesUserHaveAWSIntegrationAccess(
  402. next http.Handler,
  403. projLoc IDLocation,
  404. awsLoc IDLocation,
  405. optional bool,
  406. ) http.Handler {
  407. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  408. awsID, err := findAWSIntegrationIDInRequest(r, awsLoc)
  409. if awsID == 0 && optional {
  410. next.ServeHTTP(w, r)
  411. return
  412. }
  413. if awsID == 0 || err != nil {
  414. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  415. return
  416. }
  417. projID, err := findProjIDInRequest(r, projLoc)
  418. if err != nil {
  419. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  420. return
  421. }
  422. awsInts, err := auth.repo.AWSIntegration.ListAWSIntegrationsByProjectID(uint(projID))
  423. if err != nil {
  424. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  425. return
  426. }
  427. doesExist := false
  428. for _, awsInt := range awsInts {
  429. if awsInt.ID == uint(awsID) {
  430. doesExist = true
  431. break
  432. }
  433. }
  434. if doesExist {
  435. next.ServeHTTP(w, r)
  436. return
  437. }
  438. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  439. return
  440. })
  441. }
  442. // DoesUserHaveGCPIntegrationAccess looks for a project_id parameter and an
  443. // gcp_integration_id parameter, and verifies that the infra belongs
  444. // to the project
  445. func (auth *Auth) DoesUserHaveGCPIntegrationAccess(
  446. next http.Handler,
  447. projLoc IDLocation,
  448. gcpLoc IDLocation,
  449. optional bool,
  450. ) http.Handler {
  451. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  452. gcpID, err := findGCPIntegrationIDInRequest(r, gcpLoc)
  453. if gcpID == 0 && optional {
  454. next.ServeHTTP(w, r)
  455. return
  456. }
  457. if gcpID == 0 || err != nil {
  458. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  459. return
  460. }
  461. projID, err := findProjIDInRequest(r, projLoc)
  462. if err != nil {
  463. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  464. return
  465. }
  466. gcpInts, err := auth.repo.GCPIntegration.ListGCPIntegrationsByProjectID(uint(projID))
  467. if err != nil {
  468. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  469. return
  470. }
  471. doesExist := false
  472. for _, awsInt := range gcpInts {
  473. if awsInt.ID == uint(gcpID) {
  474. doesExist = true
  475. break
  476. }
  477. }
  478. if doesExist {
  479. next.ServeHTTP(w, r)
  480. return
  481. }
  482. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  483. return
  484. })
  485. }
  486. // DoesUserHaveDOIntegrationAccess looks for a project_id parameter and an
  487. // do_integration_id parameter, and verifies that the infra belongs
  488. // to the project
  489. func (auth *Auth) DoesUserHaveDOIntegrationAccess(
  490. next http.Handler,
  491. projLoc IDLocation,
  492. doLoc IDLocation,
  493. optional bool,
  494. ) http.Handler {
  495. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  496. doID, err := findDOIntegrationIDInRequest(r, doLoc)
  497. if doID == 0 && optional {
  498. next.ServeHTTP(w, r)
  499. return
  500. }
  501. if doID == 0 || err != nil {
  502. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  503. return
  504. }
  505. projID, err := findProjIDInRequest(r, projLoc)
  506. if err != nil {
  507. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  508. return
  509. }
  510. oauthInts, err := auth.repo.OAuthIntegration.ListOAuthIntegrationsByProjectID(uint(projID))
  511. if err != nil {
  512. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  513. return
  514. }
  515. doesExist := false
  516. for _, oauthInt := range oauthInts {
  517. if oauthInt.ID == uint(doID) {
  518. doesExist = true
  519. break
  520. }
  521. }
  522. if doesExist {
  523. next.ServeHTTP(w, r)
  524. return
  525. }
  526. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  527. return
  528. })
  529. }
  530. // Helpers
  531. func (auth *Auth) doesSessionMatchID(r *http.Request, id uint) bool {
  532. session, _ := auth.store.Get(r, auth.cookieName)
  533. if sessID, ok := session.Values["user_id"].(uint); !ok || sessID != id {
  534. return false
  535. }
  536. return true
  537. }
  538. func (auth *Auth) isLoggedIn(w http.ResponseWriter, r *http.Request) bool {
  539. // first check for Bearer token
  540. tok := auth.getTokenFromRequest(r)
  541. fmt.Println("CHECKED TOKEN FROM REQUEST", tok)
  542. if tok != nil {
  543. return true
  544. }
  545. session, err := auth.store.Get(r, auth.cookieName)
  546. if err != nil {
  547. session.Values["authenticated"] = false
  548. if err := session.Save(r, w); err != nil {
  549. fmt.Println("error while saving session in isLoggedIn", err)
  550. }
  551. return false
  552. }
  553. if auth, ok := session.Values["authenticated"].(bool); !auth || !ok {
  554. return false
  555. }
  556. return true
  557. }
  558. func (auth *Auth) getTokenFromRequest(r *http.Request) *token.Token {
  559. reqToken := r.Header.Get("Authorization")
  560. splitToken := strings.Split(reqToken, "Bearer")
  561. if len(splitToken) != 2 {
  562. return nil
  563. }
  564. reqToken = strings.TrimSpace(splitToken[1])
  565. tok, err := token.GetTokenFromEncoded(reqToken, auth.tokenConf)
  566. fmt.Printf("ERROR WAS %v\n", err)
  567. return tok
  568. }
  569. func findUserIDInRequest(r *http.Request, userLoc IDLocation) (uint64, error) {
  570. var userID uint64
  571. var err error
  572. if userLoc == URLParam {
  573. userID, err = strconv.ParseUint(chi.URLParam(r, "user_id"), 0, 64)
  574. if err != nil {
  575. return 0, err
  576. }
  577. } else if userLoc == BodyParam {
  578. form := &bodyUserID{}
  579. body, err := ioutil.ReadAll(r.Body)
  580. if err != nil {
  581. return 0, err
  582. }
  583. err = json.Unmarshal(body, form)
  584. if err != nil {
  585. return 0, err
  586. }
  587. userID = form.UserID
  588. // need to create a new stream for the body
  589. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  590. } else {
  591. vals, err := url.ParseQuery(r.URL.RawQuery)
  592. if err != nil {
  593. return 0, err
  594. }
  595. if userStrArr, ok := vals["user_id"]; ok && len(userStrArr) == 1 {
  596. userID, err = strconv.ParseUint(userStrArr[0], 10, 64)
  597. } else {
  598. return 0, errors.New("user id not found")
  599. }
  600. }
  601. return userID, nil
  602. }
  603. func findProjIDInRequest(r *http.Request, projLoc IDLocation) (uint64, error) {
  604. var projID uint64
  605. var err error
  606. if projLoc == URLParam {
  607. projID, err = strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  608. if err != nil {
  609. return 0, err
  610. }
  611. } else if projLoc == BodyParam {
  612. form := &bodyProjectID{}
  613. body, err := ioutil.ReadAll(r.Body)
  614. if err != nil {
  615. return 0, err
  616. }
  617. err = json.Unmarshal(body, form)
  618. if err != nil {
  619. return 0, err
  620. }
  621. projID = form.ProjectID
  622. // need to create a new stream for the body
  623. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  624. } else {
  625. vals, err := url.ParseQuery(r.URL.RawQuery)
  626. if err != nil {
  627. return 0, err
  628. }
  629. if projStrArr, ok := vals["project_id"]; ok && len(projStrArr) == 1 {
  630. projID, err = strconv.ParseUint(projStrArr[0], 10, 64)
  631. } else {
  632. return 0, errors.New("project id not found")
  633. }
  634. }
  635. return projID, nil
  636. }
  637. func findClusterIDInRequest(r *http.Request, clusterLoc IDLocation) (uint64, error) {
  638. var clusterID uint64
  639. var err error
  640. if clusterLoc == URLParam {
  641. clusterID, err = strconv.ParseUint(chi.URLParam(r, "cluster_id"), 0, 64)
  642. if err != nil {
  643. return 0, err
  644. }
  645. } else if clusterLoc == BodyParam {
  646. form := &bodyClusterID{}
  647. body, err := ioutil.ReadAll(r.Body)
  648. if err != nil {
  649. return 0, err
  650. }
  651. err = json.Unmarshal(body, form)
  652. if err != nil {
  653. return 0, err
  654. }
  655. clusterID = form.ClusterID
  656. // need to create a new stream for the body
  657. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  658. } else {
  659. vals, err := url.ParseQuery(r.URL.RawQuery)
  660. if err != nil {
  661. return 0, err
  662. }
  663. if clStrArr, ok := vals["cluster_id"]; ok && len(clStrArr) == 1 {
  664. clusterID, err = strconv.ParseUint(clStrArr[0], 10, 64)
  665. } else {
  666. return 0, errors.New("cluster id not found")
  667. }
  668. }
  669. return clusterID, nil
  670. }
  671. func findInviteIDInRequest(r *http.Request, inviteLoc IDLocation) (uint64, error) {
  672. var inviteID uint64
  673. var err error
  674. if inviteLoc == URLParam {
  675. inviteID, err = strconv.ParseUint(chi.URLParam(r, "invite_id"), 0, 64)
  676. if err != nil {
  677. return 0, err
  678. }
  679. } else if inviteLoc == BodyParam {
  680. form := &bodyInviteID{}
  681. body, err := ioutil.ReadAll(r.Body)
  682. if err != nil {
  683. return 0, err
  684. }
  685. err = json.Unmarshal(body, form)
  686. if err != nil {
  687. return 0, err
  688. }
  689. inviteID = form.InviteID
  690. // need to create a new stream for the body
  691. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  692. } else {
  693. vals, err := url.ParseQuery(r.URL.RawQuery)
  694. if err != nil {
  695. return 0, err
  696. }
  697. if invStrArr, ok := vals["invite_id"]; ok && len(invStrArr) == 1 {
  698. inviteID, err = strconv.ParseUint(invStrArr[0], 10, 64)
  699. } else {
  700. return 0, errors.New("invite id not found")
  701. }
  702. }
  703. return inviteID, nil
  704. }
  705. func findRegistryIDInRequest(r *http.Request, registryLoc IDLocation) (uint64, error) {
  706. var regID uint64
  707. var err error
  708. if registryLoc == URLParam {
  709. regID, err = strconv.ParseUint(chi.URLParam(r, "registry_id"), 0, 64)
  710. if err != nil {
  711. return 0, err
  712. }
  713. } else if registryLoc == BodyParam {
  714. form := &bodyRegistryID{}
  715. body, err := ioutil.ReadAll(r.Body)
  716. if err != nil {
  717. return 0, err
  718. }
  719. err = json.Unmarshal(body, form)
  720. if err != nil {
  721. return 0, err
  722. }
  723. regID = form.RegistryID
  724. // need to create a new stream for the body
  725. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  726. } else {
  727. vals, err := url.ParseQuery(r.URL.RawQuery)
  728. if err != nil {
  729. return 0, err
  730. }
  731. if regStrArr, ok := vals["registry_id"]; ok && len(regStrArr) == 1 {
  732. regID, err = strconv.ParseUint(regStrArr[0], 10, 64)
  733. } else {
  734. return 0, errors.New("registry id not found")
  735. }
  736. }
  737. return regID, nil
  738. }
  739. func findGitRepoIDInRequest(r *http.Request, gitRepoLoc IDLocation) (uint64, error) {
  740. var grID uint64
  741. var err error
  742. if gitRepoLoc == URLParam {
  743. grID, err = strconv.ParseUint(chi.URLParam(r, "git_repo_id"), 0, 64)
  744. if err != nil {
  745. return 0, err
  746. }
  747. } else if gitRepoLoc == BodyParam {
  748. form := &bodyGitRepoID{}
  749. body, err := ioutil.ReadAll(r.Body)
  750. if err != nil {
  751. return 0, err
  752. }
  753. err = json.Unmarshal(body, form)
  754. if err != nil {
  755. return 0, err
  756. }
  757. grID = form.GitRepoID
  758. // need to create a new stream for the body
  759. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  760. } else {
  761. vals, err := url.ParseQuery(r.URL.RawQuery)
  762. if err != nil {
  763. return 0, err
  764. }
  765. if regStrArr, ok := vals["git_repo_id"]; ok && len(regStrArr) == 1 {
  766. grID, err = strconv.ParseUint(regStrArr[0], 10, 64)
  767. } else {
  768. return 0, errors.New("git repo id not found")
  769. }
  770. }
  771. return grID, nil
  772. }
  773. func findInfraIDInRequest(r *http.Request, infraLoc IDLocation) (uint64, error) {
  774. var infraID uint64
  775. var err error
  776. if infraLoc == URLParam {
  777. infraID, err = strconv.ParseUint(chi.URLParam(r, "infra_id"), 0, 64)
  778. if err != nil {
  779. return 0, err
  780. }
  781. } else if infraLoc == BodyParam {
  782. form := &bodyInfraID{}
  783. body, err := ioutil.ReadAll(r.Body)
  784. if err != nil {
  785. return 0, err
  786. }
  787. err = json.Unmarshal(body, form)
  788. if err != nil {
  789. return 0, err
  790. }
  791. infraID = form.InfraID
  792. // need to create a new stream for the body
  793. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  794. } else {
  795. vals, err := url.ParseQuery(r.URL.RawQuery)
  796. if err != nil {
  797. return 0, err
  798. }
  799. if regStrArr, ok := vals["infra_id"]; ok && len(regStrArr) == 1 {
  800. infraID, err = strconv.ParseUint(regStrArr[0], 10, 64)
  801. } else {
  802. return 0, errors.New("infra id not found")
  803. }
  804. }
  805. return infraID, nil
  806. }
  807. func findAWSIntegrationIDInRequest(r *http.Request, awsLoc IDLocation) (uint64, error) {
  808. var awsID uint64
  809. var err error
  810. if awsLoc == URLParam {
  811. awsID, err = strconv.ParseUint(chi.URLParam(r, "aws_integration_id"), 0, 64)
  812. if err != nil {
  813. return 0, err
  814. }
  815. } else if awsLoc == BodyParam {
  816. form := &bodyAWSIntegrationID{}
  817. body, err := ioutil.ReadAll(r.Body)
  818. if err != nil {
  819. return 0, err
  820. }
  821. err = json.Unmarshal(body, form)
  822. if err != nil {
  823. return 0, err
  824. }
  825. awsID = form.AWSIntegrationID
  826. // need to create a new stream for the body
  827. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  828. } else {
  829. vals, err := url.ParseQuery(r.URL.RawQuery)
  830. if err != nil {
  831. return 0, err
  832. }
  833. if regStrArr, ok := vals["aws_integration_id"]; ok && len(regStrArr) == 1 {
  834. awsID, err = strconv.ParseUint(regStrArr[0], 10, 64)
  835. } else {
  836. return 0, errors.New("aws integration id not found")
  837. }
  838. }
  839. return awsID, nil
  840. }
  841. func findGCPIntegrationIDInRequest(r *http.Request, gcpLoc IDLocation) (uint64, error) {
  842. var gcpID uint64
  843. var err error
  844. if gcpLoc == URLParam {
  845. gcpID, err = strconv.ParseUint(chi.URLParam(r, "gcp_integration_id"), 0, 64)
  846. if err != nil {
  847. return 0, err
  848. }
  849. } else if gcpLoc == BodyParam {
  850. form := &bodyGCPIntegrationID{}
  851. body, err := ioutil.ReadAll(r.Body)
  852. if err != nil {
  853. return 0, err
  854. }
  855. err = json.Unmarshal(body, form)
  856. if err != nil {
  857. return 0, err
  858. }
  859. gcpID = form.GCPIntegrationID
  860. // need to create a new stream for the body
  861. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  862. } else {
  863. vals, err := url.ParseQuery(r.URL.RawQuery)
  864. if err != nil {
  865. return 0, err
  866. }
  867. if regStrArr, ok := vals["gcp_integration_id"]; ok && len(regStrArr) == 1 {
  868. gcpID, err = strconv.ParseUint(regStrArr[0], 10, 64)
  869. } else {
  870. return 0, errors.New("gcp integration id not found")
  871. }
  872. }
  873. return gcpID, nil
  874. }
  875. func findDOIntegrationIDInRequest(r *http.Request, doLoc IDLocation) (uint64, error) {
  876. var doID uint64
  877. var err error
  878. if doLoc == URLParam {
  879. doID, err = strconv.ParseUint(chi.URLParam(r, "do_integration_id"), 0, 64)
  880. if err != nil {
  881. return 0, err
  882. }
  883. } else if doLoc == BodyParam {
  884. form := &bodyDOIntegrationID{}
  885. body, err := ioutil.ReadAll(r.Body)
  886. if err != nil {
  887. return 0, err
  888. }
  889. err = json.Unmarshal(body, form)
  890. if err != nil {
  891. return 0, err
  892. }
  893. doID = form.DOIntegrationID
  894. // need to create a new stream for the body
  895. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  896. } else {
  897. vals, err := url.ParseQuery(r.URL.RawQuery)
  898. if err != nil {
  899. return 0, err
  900. }
  901. if regStrArr, ok := vals["do_integration_id"]; ok && len(regStrArr) == 1 {
  902. doID, err = strconv.ParseUint(regStrArr[0], 10, 64)
  903. } else {
  904. return 0, errors.New("do integration id not found")
  905. }
  906. }
  907. return doID, nil
  908. }