| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262 |
- package middleware
- import (
- "bytes"
- "context"
- "encoding/json"
- "errors"
- "io/ioutil"
- "net/http"
- "net/url"
- "strconv"
- "strings"
- "github.com/google/go-github/github"
- "github.com/porter-dev/porter/internal/oauth"
- "golang.org/x/oauth2"
- "github.com/go-chi/chi"
- "github.com/gorilla/sessions"
- "github.com/porter-dev/porter/internal/auth/token"
- "github.com/porter-dev/porter/internal/models"
- "github.com/porter-dev/porter/internal/repository"
- )
- // Auth implements the authorization functions
- type Auth struct {
- store sessions.Store
- cookieName string
- tokenConf *token.TokenGeneratorConf
- repo *repository.Repository
- GithubAppConf *oauth2.Config
- }
- // NewAuth returns a new Auth instance
- func NewAuth(
- store sessions.Store,
- cookieName string,
- tokenConf *token.TokenGeneratorConf,
- repo *repository.Repository,
- GithubAppConf *oauth2.Config,
- ) *Auth {
- return &Auth{store, cookieName, tokenConf, repo, GithubAppConf}
- }
- // BasicAuthenticate just checks that a user is logged in
- func (auth *Auth) BasicAuthenticate(next http.Handler) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- if auth.isLoggedIn(w, r) {
- next.ServeHTTP(w, r)
- } else {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- return
- })
- }
- // BasicAuthenticateWithRedirect checks that a user is logged in, and if they're not, the
- // user is redirected to the login page with the redirect path stored in the session
- func (auth *Auth) BasicAuthenticateWithRedirect(next http.Handler) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- if auth.isLoggedIn(w, r) {
- next.ServeHTTP(w, r)
- } else {
- session, err := auth.store.Get(r, auth.cookieName)
- if err != nil {
- http.Redirect(w, r, "/dashboard", 302)
- }
- // need state parameter to validate when redirected
- if r.URL.RawQuery == "" {
- session.Values["redirect"] = r.URL.Path
- } else {
- session.Values["redirect"] = r.URL.Path + "?" + r.URL.RawQuery
- }
- session.Save(r, w)
- http.Redirect(w, r, "/dashboard", 302)
- return
- }
- return
- })
- }
- // IDLocation represents the location of the ID to use for authentication
- type IDLocation uint
- const (
- // URLParam location looks for a parameter in the URL endpoint
- URLParam IDLocation = iota
- // BodyParam location looks for a parameter in the body
- BodyParam
- // QueryParam location looks for a parameter in the query string
- QueryParam
- )
- type bodyUserID struct {
- UserID uint64 `json:"user_id"`
- }
- type bodyProjectID struct {
- ProjectID uint64 `json:"project_id"`
- }
- type bodyClusterID struct {
- ClusterID uint64 `json:"cluster_id"`
- }
- type bodyRegistryID struct {
- RegistryID uint64 `json:"registry_id"`
- }
- type bodyGitRepoID struct {
- GitRepoID uint64 `json:"git_repo_id"`
- }
- type bodyInfraID struct {
- InfraID uint64 `json:"infra_id"`
- }
- type bodyInviteID struct {
- InviteID uint64 `json:"invite_id"`
- }
- type bodyAWSIntegrationID struct {
- AWSIntegrationID uint64 `json:"aws_integration_id"`
- }
- type bodyGCPIntegrationID struct {
- GCPIntegrationID uint64 `json:"gcp_integration_id"`
- }
- type bodyDOIntegrationID struct {
- DOIntegrationID uint64 `json:"do_integration_id"`
- }
- // DoesUserIDMatch checks the id URL parameter and verifies that it matches
- // the one stored in the session
- func (auth *Auth) DoesUserIDMatch(next http.Handler, loc IDLocation) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- var err error
- id, err := findUserIDInRequest(r, loc)
- // first check for token
- tok := auth.getTokenFromRequest(r)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- } else if tok != nil && tok.IBy == uint(id) {
- next.ServeHTTP(w, r)
- return
- } else if auth.doesSessionMatchID(r, uint(id)) {
- next.ServeHTTP(w, r)
- } else {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- return
- })
- }
- // AccessType represents the various access types for a project
- type AccessType string
- // The various access types
- const (
- AdminAccess AccessType = "admin"
- ReadAccess AccessType = "read"
- WriteAccess AccessType = "write"
- )
- // DoesUserHaveProjectAccess looks for a project_id parameter and checks that the
- // user has access via the specified accessType
- func (auth *Auth) DoesUserHaveProjectAccess(
- next http.Handler,
- projLoc IDLocation,
- accessType AccessType,
- ) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- var err error
- projID, err := findProjIDInRequest(r, projLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- // first check for token
- tok := auth.getTokenFromRequest(r)
- var userID uint
- if tok != nil && tok.ProjectID != 0 && tok.ProjectID == uint(projID) {
- next.ServeHTTP(w, r)
- return
- } else if tok != nil {
- userID = tok.IBy
- } else {
- session, err := auth.store.Get(r, auth.cookieName)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- sessionUserID, ok := session.Values["user_id"]
- if !ok {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- userID, ok = sessionUserID.(uint)
- if !ok {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- }
- // read the user and make sure the email is verified
- user, err := auth.repo.User.ReadUser(userID)
- if err != nil || !user.EmailVerified {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- // get the project
- proj, err := auth.repo.Project.ReadProject(uint(projID))
- if err != nil {
- http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
- return
- }
- // look for the user role in the project
- for _, role := range proj.Roles {
- if role.UserID == userID {
- if accessType == AdminAccess {
- if role.Kind == models.RoleAdmin {
- next.ServeHTTP(w, r)
- return
- }
- } else if accessType == WriteAccess {
- if role.Kind == models.RoleAdmin || role.Kind == models.RoleDeveloper {
- next.ServeHTTP(w, r)
- return
- }
- } else if accessType == ReadAccess {
- next.ServeHTTP(w, r)
- return
- }
- }
- }
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- })
- }
- // DoesUserHaveClusterAccess looks for a project_id parameter and a
- // cluster_id parameter, and verifies that the cluster belongs
- // to the project
- func (auth *Auth) DoesUserHaveClusterAccess(
- next http.Handler,
- projLoc IDLocation,
- clusterLoc IDLocation,
- ) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- clusterID, err := findClusterIDInRequest(r, clusterLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- projID, err := findProjIDInRequest(r, projLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- // get the service accounts belonging to the project
- clusters, err := auth.repo.Cluster.ListClustersByProjectID(uint(projID))
- if err != nil {
- http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
- return
- }
- doesExist := false
- for _, cluster := range clusters {
- if cluster.ID == uint(clusterID) {
- doesExist = true
- break
- }
- }
- if doesExist {
- next.ServeHTTP(w, r)
- return
- }
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- })
- }
- // DoesUserHaveInviteAccess looks for a project_id parameter and a
- // invite_id parameter, and verifies that the invite belongs
- // to the project
- func (auth *Auth) DoesUserHaveInviteAccess(
- next http.Handler,
- projLoc IDLocation,
- inviteLoc IDLocation,
- ) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- inviteID, err := findInviteIDInRequest(r, inviteLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- projID, err := findProjIDInRequest(r, projLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- // get the service accounts belonging to the project
- invites, err := auth.repo.Invite.ListInvitesByProjectID(uint(projID))
- if err != nil {
- http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
- return
- }
- doesExist := false
- for _, invite := range invites {
- if invite.ID == uint(inviteID) {
- doesExist = true
- break
- }
- }
- if doesExist {
- next.ServeHTTP(w, r)
- return
- }
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- })
- }
- // DoesUserHaveRegistryAccess looks for a project_id parameter and a
- // registry_id parameter, and verifies that the registry belongs
- // to the project
- func (auth *Auth) DoesUserHaveRegistryAccess(
- next http.Handler,
- projLoc IDLocation,
- registryLoc IDLocation,
- ) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- regID, err := findRegistryIDInRequest(r, registryLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- projID, err := findProjIDInRequest(r, projLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- // get the service accounts belonging to the project
- regs, err := auth.repo.Registry.ListRegistriesByProjectID(uint(projID))
- if err != nil {
- http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
- return
- }
- doesExist := false
- for _, reg := range regs {
- if reg.ID == uint(regID) {
- doesExist = true
- break
- }
- }
- if doesExist {
- next.ServeHTTP(w, r)
- return
- }
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- })
- }
- // DoesUserHaveGitInstallationAccess checks that a user has access to an installation id
- // by ensuring the installation id exists for one org or account they have access to
- // note that this makes a github API request, but the endpoint is fast so this doesn't add
- // much overhead
- func (auth *Auth) DoesUserHaveGitInstallationAccess(
- next http.Handler,
- gitRepoLoc IDLocation,
- ) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- grID, err := findGitInstallationIDInRequest(r, gitRepoLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- tok := auth.getTokenFromRequest(r)
- var userID uint
- if tok != nil {
- userID = tok.IBy
- } else {
- session, err := auth.store.Get(r, auth.cookieName)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- sessionUserID, ok := session.Values["user_id"]
- if !ok {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- userID, ok = sessionUserID.(uint)
- if !ok {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- }
- user, err := auth.repo.User.ReadUser(userID)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- oauthInt, err := auth.repo.GithubAppOAuthIntegration.ReadGithubAppOauthIntegration(user.GithubAppIntegrationID)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- _, _, err = oauth.GetAccessToken(oauthInt.SharedOAuthModel,
- auth.GithubAppConf,
- oauth.MakeUpdateGithubAppOauthIntegrationFunction(oauthInt, *auth.repo))
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- client := github.NewClient(auth.GithubAppConf.Client(oauth2.NoContext, &oauth2.Token{
- AccessToken: string(oauthInt.AccessToken),
- RefreshToken: string(oauthInt.RefreshToken),
- TokenType: "Bearer",
- }))
- accountIDs := make([]int64, 0)
- AuthUser, _, err := client.Users.Get(context.Background(), "")
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- accountIDs = append(accountIDs, *AuthUser.ID)
- opts := &github.ListOptions{
- PerPage: 100,
- Page: 1,
- }
- for {
- orgs, pages, err := client.Organizations.List(context.Background(), "", opts)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- for _, org := range orgs {
- accountIDs = append(accountIDs, *org.ID)
- }
- if pages.NextPage == 0 {
- break
- }
- }
- installations, err := auth.repo.GithubAppInstallation.ReadGithubAppInstallationByAccountIDs(accountIDs)
- for _, installation := range installations {
- if uint64(installation.InstallationID) == grID {
- next.ServeHTTP(w, r)
- return
- }
- }
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- })
- }
- // DoesUserHaveInfraAccess looks for a project_id parameter and an
- // infra_id parameter, and verifies that the infra belongs
- // to the project
- func (auth *Auth) DoesUserHaveInfraAccess(
- next http.Handler,
- projLoc IDLocation,
- infraLoc IDLocation,
- ) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- infraID, err := findInfraIDInRequest(r, infraLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- projID, err := findProjIDInRequest(r, projLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- infras, err := auth.repo.Infra.ListInfrasByProjectID(uint(projID))
- if err != nil {
- http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
- return
- }
- doesExist := false
- for _, infra := range infras {
- if infra.ID == uint(infraID) {
- doesExist = true
- break
- }
- }
- if doesExist {
- next.ServeHTTP(w, r)
- return
- }
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- })
- }
- // DoesUserHaveAWSIntegrationAccess looks for a project_id parameter and an
- // aws_integration_id parameter, and verifies that the infra belongs
- // to the project
- func (auth *Auth) DoesUserHaveAWSIntegrationAccess(
- next http.Handler,
- projLoc IDLocation,
- awsLoc IDLocation,
- optional bool,
- ) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- awsID, err := findAWSIntegrationIDInRequest(r, awsLoc)
- if awsID == 0 && optional {
- next.ServeHTTP(w, r)
- return
- }
- if awsID == 0 || err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- projID, err := findProjIDInRequest(r, projLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- awsInts, err := auth.repo.AWSIntegration.ListAWSIntegrationsByProjectID(uint(projID))
- if err != nil {
- http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
- return
- }
- doesExist := false
- for _, awsInt := range awsInts {
- if awsInt.ID == uint(awsID) {
- doesExist = true
- break
- }
- }
- if doesExist {
- next.ServeHTTP(w, r)
- return
- }
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- })
- }
- // DoesUserHaveGCPIntegrationAccess looks for a project_id parameter and an
- // gcp_integration_id parameter, and verifies that the infra belongs
- // to the project
- func (auth *Auth) DoesUserHaveGCPIntegrationAccess(
- next http.Handler,
- projLoc IDLocation,
- gcpLoc IDLocation,
- optional bool,
- ) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- gcpID, err := findGCPIntegrationIDInRequest(r, gcpLoc)
- if gcpID == 0 && optional {
- next.ServeHTTP(w, r)
- return
- }
- if gcpID == 0 || err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- projID, err := findProjIDInRequest(r, projLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- gcpInts, err := auth.repo.GCPIntegration.ListGCPIntegrationsByProjectID(uint(projID))
- if err != nil {
- http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
- return
- }
- doesExist := false
- for _, awsInt := range gcpInts {
- if awsInt.ID == uint(gcpID) {
- doesExist = true
- break
- }
- }
- if doesExist {
- next.ServeHTTP(w, r)
- return
- }
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- })
- }
- // DoesUserHaveDOIntegrationAccess looks for a project_id parameter and an
- // do_integration_id parameter, and verifies that the infra belongs
- // to the project
- func (auth *Auth) DoesUserHaveDOIntegrationAccess(
- next http.Handler,
- projLoc IDLocation,
- doLoc IDLocation,
- optional bool,
- ) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- doID, err := findDOIntegrationIDInRequest(r, doLoc)
- if doID == 0 && optional {
- next.ServeHTTP(w, r)
- return
- }
- if doID == 0 || err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- projID, err := findProjIDInRequest(r, projLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- oauthInts, err := auth.repo.OAuthIntegration.ListOAuthIntegrationsByProjectID(uint(projID))
- if err != nil {
- http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
- return
- }
- doesExist := false
- for _, oauthInt := range oauthInts {
- if oauthInt.ID == uint(doID) {
- doesExist = true
- break
- }
- }
- if doesExist {
- next.ServeHTTP(w, r)
- return
- }
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- })
- }
- // Helpers
- func (auth *Auth) doesSessionMatchID(r *http.Request, id uint) bool {
- session, _ := auth.store.Get(r, auth.cookieName)
- userID, ok := session.Values["user_id"]
- if !ok {
- return false
- }
- if sessID, ok := userID.(uint); !ok || sessID != id {
- return false
- }
- return true
- }
- func (auth *Auth) isLoggedIn(w http.ResponseWriter, r *http.Request) bool {
- // first check for Bearer token
- tok := auth.getTokenFromRequest(r)
- if tok != nil {
- return true
- }
- session, err := auth.store.Get(r, auth.cookieName)
- if err != nil {
- session.Values["authenticated"] = false
- if err := session.Save(r, w); err != nil {
- return false
- }
- return false
- }
- if auth, ok := session.Values["authenticated"].(bool); !auth || !ok {
- return false
- }
- return true
- }
- func (auth *Auth) getTokenFromRequest(r *http.Request) *token.Token {
- reqToken := r.Header.Get("Authorization")
- splitToken := strings.Split(reqToken, "Bearer")
- if len(splitToken) != 2 {
- return nil
- }
- reqToken = strings.TrimSpace(splitToken[1])
- tok, err := token.GetTokenFromEncoded(reqToken, auth.tokenConf)
- if err != nil {
- return nil
- }
- return tok
- }
- func findUserIDInRequest(r *http.Request, userLoc IDLocation) (uint64, error) {
- var userID uint64
- var err error
- if userLoc == URLParam {
- userID, err = strconv.ParseUint(chi.URLParam(r, "user_id"), 0, 64)
- if err != nil {
- return 0, err
- }
- } else if userLoc == BodyParam {
- form := &bodyUserID{}
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return 0, err
- }
- err = json.Unmarshal(body, form)
- if err != nil {
- return 0, err
- }
- userID = form.UserID
- // need to create a new stream for the body
- r.Body = ioutil.NopCloser(bytes.NewReader(body))
- } else {
- vals, err := url.ParseQuery(r.URL.RawQuery)
- if err != nil {
- return 0, err
- }
- if userStrArr, ok := vals["user_id"]; ok && len(userStrArr) == 1 {
- userID, err = strconv.ParseUint(userStrArr[0], 10, 64)
- } else {
- return 0, errors.New("user id not found")
- }
- }
- return userID, nil
- }
- func findProjIDInRequest(r *http.Request, projLoc IDLocation) (uint64, error) {
- var projID uint64
- var err error
- if projLoc == URLParam {
- projID, err = strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
- if err != nil {
- return 0, err
- }
- } else if projLoc == BodyParam {
- form := &bodyProjectID{}
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return 0, err
- }
- err = json.Unmarshal(body, form)
- if err != nil {
- return 0, err
- }
- projID = form.ProjectID
- // need to create a new stream for the body
- r.Body = ioutil.NopCloser(bytes.NewReader(body))
- } else {
- vals, err := url.ParseQuery(r.URL.RawQuery)
- if err != nil {
- return 0, err
- }
- if projStrArr, ok := vals["project_id"]; ok && len(projStrArr) == 1 {
- projID, err = strconv.ParseUint(projStrArr[0], 10, 64)
- } else {
- return 0, errors.New("project id not found")
- }
- }
- return projID, nil
- }
- func findClusterIDInRequest(r *http.Request, clusterLoc IDLocation) (uint64, error) {
- var clusterID uint64
- var err error
- if clusterLoc == URLParam {
- clusterID, err = strconv.ParseUint(chi.URLParam(r, "cluster_id"), 0, 64)
- if err != nil {
- return 0, err
- }
- } else if clusterLoc == BodyParam {
- form := &bodyClusterID{}
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return 0, err
- }
- err = json.Unmarshal(body, form)
- if err != nil {
- return 0, err
- }
- clusterID = form.ClusterID
- // need to create a new stream for the body
- r.Body = ioutil.NopCloser(bytes.NewReader(body))
- } else {
- vals, err := url.ParseQuery(r.URL.RawQuery)
- if err != nil {
- return 0, err
- }
- if clStrArr, ok := vals["cluster_id"]; ok && len(clStrArr) == 1 {
- clusterID, err = strconv.ParseUint(clStrArr[0], 10, 64)
- } else {
- return 0, errors.New("cluster id not found")
- }
- }
- return clusterID, nil
- }
- func findInviteIDInRequest(r *http.Request, inviteLoc IDLocation) (uint64, error) {
- var inviteID uint64
- var err error
- if inviteLoc == URLParam {
- inviteID, err = strconv.ParseUint(chi.URLParam(r, "invite_id"), 0, 64)
- if err != nil {
- return 0, err
- }
- } else if inviteLoc == BodyParam {
- form := &bodyInviteID{}
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return 0, err
- }
- err = json.Unmarshal(body, form)
- if err != nil {
- return 0, err
- }
- inviteID = form.InviteID
- // need to create a new stream for the body
- r.Body = ioutil.NopCloser(bytes.NewReader(body))
- } else {
- vals, err := url.ParseQuery(r.URL.RawQuery)
- if err != nil {
- return 0, err
- }
- if invStrArr, ok := vals["invite_id"]; ok && len(invStrArr) == 1 {
- inviteID, err = strconv.ParseUint(invStrArr[0], 10, 64)
- } else {
- return 0, errors.New("invite id not found")
- }
- }
- return inviteID, nil
- }
- func findRegistryIDInRequest(r *http.Request, registryLoc IDLocation) (uint64, error) {
- var regID uint64
- var err error
- if registryLoc == URLParam {
- regID, err = strconv.ParseUint(chi.URLParam(r, "registry_id"), 0, 64)
- if err != nil {
- return 0, err
- }
- } else if registryLoc == BodyParam {
- form := &bodyRegistryID{}
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return 0, err
- }
- err = json.Unmarshal(body, form)
- if err != nil {
- return 0, err
- }
- regID = form.RegistryID
- // need to create a new stream for the body
- r.Body = ioutil.NopCloser(bytes.NewReader(body))
- } else {
- vals, err := url.ParseQuery(r.URL.RawQuery)
- if err != nil {
- return 0, err
- }
- if regStrArr, ok := vals["registry_id"]; ok && len(regStrArr) == 1 {
- regID, err = strconv.ParseUint(regStrArr[0], 10, 64)
- } else {
- return 0, errors.New("registry id not found")
- }
- }
- return regID, nil
- }
- // findGitInstallationIDInRequest extracts and installation ID from a request
- func findGitInstallationIDInRequest(r *http.Request, gitRepoLoc IDLocation) (uint64, error) {
- var grID uint64
- var err error
- if gitRepoLoc == URLParam {
- grID, err = strconv.ParseUint(chi.URLParam(r, "installation_id"), 0, 64)
- if err != nil {
- return 0, err
- }
- } else if gitRepoLoc == BodyParam {
- form := &bodyGitRepoID{}
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return 0, err
- }
- err = json.Unmarshal(body, form)
- if err != nil {
- return 0, err
- }
- grID = form.GitRepoID
- // need to create a new stream for the body
- r.Body = ioutil.NopCloser(bytes.NewReader(body))
- } else {
- vals, err := url.ParseQuery(r.URL.RawQuery)
- if err != nil {
- return 0, err
- }
- if regStrArr, ok := vals["installation_id"]; ok && len(regStrArr) == 1 {
- grID, err = strconv.ParseUint(regStrArr[0], 10, 64)
- } else {
- return 0, errors.New("git app installation id not found")
- }
- }
- return grID, nil
- }
- func findInfraIDInRequest(r *http.Request, infraLoc IDLocation) (uint64, error) {
- var infraID uint64
- var err error
- if infraLoc == URLParam {
- infraID, err = strconv.ParseUint(chi.URLParam(r, "infra_id"), 0, 64)
- if err != nil {
- return 0, err
- }
- } else if infraLoc == BodyParam {
- form := &bodyInfraID{}
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return 0, err
- }
- err = json.Unmarshal(body, form)
- if err != nil {
- return 0, err
- }
- infraID = form.InfraID
- // need to create a new stream for the body
- r.Body = ioutil.NopCloser(bytes.NewReader(body))
- } else {
- vals, err := url.ParseQuery(r.URL.RawQuery)
- if err != nil {
- return 0, err
- }
- if regStrArr, ok := vals["infra_id"]; ok && len(regStrArr) == 1 {
- infraID, err = strconv.ParseUint(regStrArr[0], 10, 64)
- } else {
- return 0, errors.New("infra id not found")
- }
- }
- return infraID, nil
- }
- func findAWSIntegrationIDInRequest(r *http.Request, awsLoc IDLocation) (uint64, error) {
- var awsID uint64
- var err error
- if awsLoc == URLParam {
- awsID, err = strconv.ParseUint(chi.URLParam(r, "aws_integration_id"), 0, 64)
- if err != nil {
- return 0, err
- }
- } else if awsLoc == BodyParam {
- form := &bodyAWSIntegrationID{}
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return 0, err
- }
- err = json.Unmarshal(body, form)
- if err != nil {
- return 0, err
- }
- awsID = form.AWSIntegrationID
- // need to create a new stream for the body
- r.Body = ioutil.NopCloser(bytes.NewReader(body))
- } else {
- vals, err := url.ParseQuery(r.URL.RawQuery)
- if err != nil {
- return 0, err
- }
- if regStrArr, ok := vals["aws_integration_id"]; ok && len(regStrArr) == 1 {
- awsID, err = strconv.ParseUint(regStrArr[0], 10, 64)
- } else {
- return 0, errors.New("aws integration id not found")
- }
- }
- return awsID, nil
- }
- func findGCPIntegrationIDInRequest(r *http.Request, gcpLoc IDLocation) (uint64, error) {
- var gcpID uint64
- var err error
- if gcpLoc == URLParam {
- gcpID, err = strconv.ParseUint(chi.URLParam(r, "gcp_integration_id"), 0, 64)
- if err != nil {
- return 0, err
- }
- } else if gcpLoc == BodyParam {
- form := &bodyGCPIntegrationID{}
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return 0, err
- }
- err = json.Unmarshal(body, form)
- if err != nil {
- return 0, err
- }
- gcpID = form.GCPIntegrationID
- // need to create a new stream for the body
- r.Body = ioutil.NopCloser(bytes.NewReader(body))
- } else {
- vals, err := url.ParseQuery(r.URL.RawQuery)
- if err != nil {
- return 0, err
- }
- if regStrArr, ok := vals["gcp_integration_id"]; ok && len(regStrArr) == 1 {
- gcpID, err = strconv.ParseUint(regStrArr[0], 10, 64)
- } else {
- return 0, errors.New("gcp integration id not found")
- }
- }
- return gcpID, nil
- }
- func findDOIntegrationIDInRequest(r *http.Request, doLoc IDLocation) (uint64, error) {
- var doID uint64
- var err error
- if doLoc == URLParam {
- doID, err = strconv.ParseUint(chi.URLParam(r, "do_integration_id"), 0, 64)
- if err != nil {
- return 0, err
- }
- } else if doLoc == BodyParam {
- form := &bodyDOIntegrationID{}
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return 0, err
- }
- err = json.Unmarshal(body, form)
- if err != nil {
- return 0, err
- }
- doID = form.DOIntegrationID
- // need to create a new stream for the body
- r.Body = ioutil.NopCloser(bytes.NewReader(body))
- } else {
- vals, err := url.ParseQuery(r.URL.RawQuery)
- if err != nil {
- return 0, err
- }
- if regStrArr, ok := vals["do_integration_id"]; ok && len(regStrArr) == 1 {
- doID, err = strconv.ParseUint(regStrArr[0], 10, 64)
- } else {
- return 0, errors.New("do integration id not found")
- }
- }
- return doID, nil
- }
|