auth.go 26 KB

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