فهرست منبع

schema changes and get oauth flow fully working

Ivan Galakhov 4 سال پیش
والد
کامیت
d4d718297f

+ 8 - 6
cmd/migrate/keyrotate/helpers_test.go

@@ -253,12 +253,14 @@ func initOAuthIntegration(tester *tester, t *testing.T) {
 	}
 
 	oauth := &ints.OAuthIntegration{
-		Client:       ints.OAuthGithub,
-		ProjectID:    tester.initProjects[0].ID,
-		UserID:       tester.initUsers[0].ID,
-		ClientID:     []byte("exampleclientid"),
-		AccessToken:  []byte("idtoken"),
-		RefreshToken: []byte("refreshtoken"),
+		SharedOAuthModel: ints.SharedOAuthModel{
+			ClientID:     []byte("exampleclientid"),
+			AccessToken:  []byte("idtoken"),
+			RefreshToken: []byte("refreshtoken"),
+		},
+		Client:    ints.OAuthGithub,
+		ProjectID: tester.initProjects[0].ID,
+		UserID:    tester.initUsers[0].ID,
 	}
 
 	oauth, err := tester.repo.OAuthIntegration.CreateOAuthIntegration(oauth)

+ 2 - 0
cmd/migrate/main.go

@@ -58,6 +58,8 @@ func main() {
 		&ints.RegTokenCache{},
 		&ints.HelmRepoTokenCache{},
 		&ints.GithubAppInstallation{},
+		&ints.GithubAppOAuthIntegration{},
+		&ints.GithubAppOAuthIntegration{},
 	)
 
 	if err != nil {

+ 8 - 6
internal/forms/helper_test.go

@@ -200,12 +200,14 @@ func initOAuthIntegration(tester *tester, t *testing.T) {
 	}
 
 	oauth := &ints.OAuthIntegration{
-		Client:       ints.OAuthGithub,
-		ProjectID:    tester.initProjects[0].ID,
-		UserID:       tester.initUsers[0].ID,
-		ClientID:     []byte("exampleclientid"),
-		AccessToken:  []byte("idtoken"),
-		RefreshToken: []byte("refreshtoken"),
+		SharedOAuthModel: ints.SharedOAuthModel{
+			ClientID:     []byte("exampleclientid"),
+			AccessToken:  []byte("idtoken"),
+			RefreshToken: []byte("refreshtoken"),
+		},
+		Client:    ints.OAuthGithub,
+		ProjectID: tester.initProjects[0].ID,
+		UserID:    tester.initUsers[0].ID,
 	}
 
 	oauth, err := tester.repo.OAuthIntegration.CreateOAuthIntegration(oauth)

+ 16 - 10
internal/repository/gorm/auth.go

@@ -633,16 +633,6 @@ func (repo *OAuthIntegrationRepository) CreateOAuthIntegration(
 	return am, nil
 }
 
-// CreateUserOAuthIntegration creates a new OAuth integration not tied to a project (ProjectID 0)
-func (repo *OAuthIntegrationRepository) CreateUserOAuthIntegration(
-	am *ints.OAuthIntegration,
-) (*ints.OAuthIntegration, error) {
-	if err := repo.db.Create(am).Error; err != nil {
-		return nil, err
-	}
-	return am, nil
-}
-
 // ReadOAuthIntegration finds a oauth auth mechanism by id
 func (repo *OAuthIntegrationRepository) ReadOAuthIntegration(
 	id uint,
@@ -1142,3 +1132,19 @@ func (repo *GithubAppInstallationRepository) DeleteGithubAppInstallationByAccoun
 
 	return nil
 }
+
+// GithubAppOAuthIntegrationRepository implements repository.GithubAppOAuthIntegrationRepository
+type GithubAppOAuthIntegrationRepository struct {
+	db *gorm.DB
+}
+
+func NewGithubAppOAuthIntegrationRepository(db *gorm.DB) repository.GithubAppOAuthIntegrationRepository {
+	return &GithubAppOAuthIntegrationRepository{db}
+}
+
+func (repo *GithubAppOAuthIntegrationRepository) CreateGithubAppOAuthIntegration(am *ints.GithubAppOAuthIntegration) (*ints.GithubAppOAuthIntegration, error) {
+	if err := repo.db.Create(am).Error; err != nil {
+		return nil, err
+	}
+	return am, nil
+}

+ 16 - 12
internal/repository/gorm/auth_test.go

@@ -285,12 +285,14 @@ func TestCreateOAuthIntegration(t *testing.T) {
 	defer cleanup(tester, t)
 
 	oauth := &ints.OAuthIntegration{
-		Client:       ints.OAuthGithub,
-		ProjectID:    tester.initProjects[0].ID,
-		UserID:       tester.initUsers[0].ID,
-		ClientID:     []byte("exampleclientid"),
-		AccessToken:  []byte("idtoken"),
-		RefreshToken: []byte("refreshtoken"),
+		SharedOAuthModel: ints.SharedOAuthModel{
+			ClientID:     []byte("exampleclientid"),
+			AccessToken:  []byte("idtoken"),
+			RefreshToken: []byte("refreshtoken"),
+		},
+		Client:    ints.OAuthGithub,
+		ProjectID: tester.initProjects[0].ID,
+		UserID:    tester.initUsers[0].ID,
 	}
 
 	expOAuth := *oauth
@@ -345,12 +347,14 @@ func TestListOAuthIntegrationsByProjectID(t *testing.T) {
 
 	// make sure data is correct
 	expOAuth := ints.OAuthIntegration{
-		Client:       ints.OAuthGithub,
-		ProjectID:    tester.initProjects[0].ID,
-		UserID:       tester.initUsers[0].ID,
-		ClientID:     []byte("exampleclientid"),
-		AccessToken:  []byte("idtoken"),
-		RefreshToken: []byte("refreshtoken"),
+		SharedOAuthModel: ints.SharedOAuthModel{
+			ClientID:     []byte("exampleclientid"),
+			AccessToken:  []byte("idtoken"),
+			RefreshToken: []byte("refreshtoken"),
+		},
+		Client:    ints.OAuthGithub,
+		ProjectID: tester.initProjects[0].ID,
+		UserID:    tester.initUsers[0].ID,
 	}
 
 	oauth := oauths[0]

+ 8 - 6
internal/repository/gorm/helpers_test.go

@@ -243,12 +243,14 @@ func initOAuthIntegration(tester *tester, t *testing.T) {
 	}
 
 	oauth := &ints.OAuthIntegration{
-		Client:       ints.OAuthGithub,
-		ProjectID:    tester.initProjects[0].ID,
-		UserID:       tester.initUsers[0].ID,
-		ClientID:     []byte("exampleclientid"),
-		AccessToken:  []byte("idtoken"),
-		RefreshToken: []byte("refreshtoken"),
+		SharedOAuthModel: ints.SharedOAuthModel{
+			ClientID:     []byte("exampleclientid"),
+			AccessToken:  []byte("idtoken"),
+			RefreshToken: []byte("refreshtoken"),
+		},
+		Client:    ints.OAuthGithub,
+		ProjectID: tester.initProjects[0].ID,
+		UserID:    tester.initUsers[0].ID,
 	}
 
 	oauth, err := tester.repo.OAuthIntegration.CreateOAuthIntegration(oauth)

+ 22 - 21
internal/repository/gorm/repository.go

@@ -9,26 +9,27 @@ import (
 // gorm.DB for querying the database
 func NewRepository(db *gorm.DB, key *[32]byte) *repository.Repository {
 	return &repository.Repository{
-		User:                  NewUserRepository(db),
-		Session:               NewSessionRepository(db),
-		Project:               NewProjectRepository(db),
-		Release:               NewReleaseRepository(db),
-		GitRepo:               NewGitRepoRepository(db, key),
-		Cluster:               NewClusterRepository(db, key),
-		HelmRepo:              NewHelmRepoRepository(db, key),
-		Registry:              NewRegistryRepository(db, key),
-		Infra:                 NewInfraRepository(db, key),
-		GitActionConfig:       NewGitActionConfigRepository(db),
-		Invite:                NewInviteRepository(db),
-		AuthCode:              NewAuthCodeRepository(db),
-		DNSRecord:             NewDNSRecordRepository(db),
-		PWResetToken:          NewPWResetTokenRepository(db),
-		KubeIntegration:       NewKubeIntegrationRepository(db, key),
-		BasicIntegration:      NewBasicIntegrationRepository(db, key),
-		OIDCIntegration:       NewOIDCIntegrationRepository(db, key),
-		OAuthIntegration:      NewOAuthIntegrationRepository(db, key),
-		GCPIntegration:        NewGCPIntegrationRepository(db, key),
-		AWSIntegration:        NewAWSIntegrationRepository(db, key),
-		GithubAppInstallation: NewGithubAppInstallationRepository(db),
+		User:                      NewUserRepository(db),
+		Session:                   NewSessionRepository(db),
+		Project:                   NewProjectRepository(db),
+		Release:                   NewReleaseRepository(db),
+		GitRepo:                   NewGitRepoRepository(db, key),
+		Cluster:                   NewClusterRepository(db, key),
+		HelmRepo:                  NewHelmRepoRepository(db, key),
+		Registry:                  NewRegistryRepository(db, key),
+		Infra:                     NewInfraRepository(db, key),
+		GitActionConfig:           NewGitActionConfigRepository(db),
+		Invite:                    NewInviteRepository(db),
+		AuthCode:                  NewAuthCodeRepository(db),
+		DNSRecord:                 NewDNSRecordRepository(db),
+		PWResetToken:              NewPWResetTokenRepository(db),
+		KubeIntegration:           NewKubeIntegrationRepository(db, key),
+		BasicIntegration:          NewBasicIntegrationRepository(db, key),
+		OIDCIntegration:           NewOIDCIntegrationRepository(db, key),
+		OAuthIntegration:          NewOAuthIntegrationRepository(db, key),
+		GCPIntegration:            NewGCPIntegrationRepository(db, key),
+		AWSIntegration:            NewAWSIntegrationRepository(db, key),
+		GithubAppInstallation:     NewGithubAppInstallationRepository(db),
+		GithubAppOAuthIntegration: NewGithubAppOAuthIntegrationRepository(db),
 	}
 }

+ 6 - 1
internal/repository/integrations.go

@@ -32,12 +32,17 @@ type OIDCIntegrationRepository interface {
 // mechanism
 type OAuthIntegrationRepository interface {
 	CreateOAuthIntegration(am *ints.OAuthIntegration) (*ints.OAuthIntegration, error)
-	CreateUserOAuthIntegration(am *ints.OAuthIntegration) (*ints.OAuthIntegration, error)
 	ReadOAuthIntegration(id uint) (*ints.OAuthIntegration, error)
 	ListOAuthIntegrationsByProjectID(projectID uint) ([]*ints.OAuthIntegration, error)
 	UpdateOAuthIntegration(am *ints.OAuthIntegration) (*ints.OAuthIntegration, error)
 }
 
+// GithubAppOAuthIntegrationRepository represents the set of queries on the oauth
+// mechanism
+type GithubAppOAuthIntegrationRepository interface {
+	CreateGithubAppOAuthIntegration(am *ints.GithubAppOAuthIntegration) (*ints.GithubAppOAuthIntegration, error)
+}
+
 // AWSIntegrationRepository represents the set of queries on the AWS auth
 // mechanism
 type AWSIntegrationRepository interface {

+ 23 - 14
internal/repository/memory/auth.go

@@ -228,20 +228,6 @@ func (repo *OAuthIntegrationRepository) CreateOAuthIntegration(
 	return am, nil
 }
 
-// CreateUserOAuthIntegration creates a new OAuth integration not tied to a project (ProjectID 0)
-func (repo *OAuthIntegrationRepository) CreateUserOAuthIntegration(
-	am *ints.OAuthIntegration,
-) (*ints.OAuthIntegration, error) {
-	if !repo.canQuery {
-		return nil, errors.New("cannot write database")
-	}
-
-	repo.oIntegrations = append(repo.oIntegrations, am)
-	am.ID = uint(len(repo.oIntegrations))
-
-	return am, nil
-}
-
 // ReadOAuthIntegration finds a o auth mechanism by id
 func (repo *OAuthIntegrationRepository) ReadOAuthIntegration(
 	id uint,
@@ -505,3 +491,26 @@ func (repo *GithubAppInstallationRepository) DeleteGithubAppInstallationByAccoun
 
 	return nil
 }
+
+type GithubAppOAuthIntegrationRepository struct {
+	canQuery                   bool
+	githubAppOauthIntegrations []*ints.GithubAppOAuthIntegration
+}
+
+func NewGithubAppOAuthIntegrationRepository(canQuery bool) repository.GithubAppOAuthIntegrationRepository {
+	return &GithubAppOAuthIntegrationRepository{
+		canQuery,
+		[]*ints.GithubAppOAuthIntegration{},
+	}
+}
+
+func (repo *GithubAppOAuthIntegrationRepository) CreateGithubAppOAuthIntegration(am *ints.GithubAppOAuthIntegration) (*ints.GithubAppOAuthIntegration, error) {
+	if !repo.canQuery {
+		return nil, errors.New("cannot write database")
+	}
+
+	repo.githubAppOauthIntegrations = append(repo.githubAppOauthIntegrations, am)
+	am.ID = uint(len(repo.githubAppOauthIntegrations))
+
+	return am, nil
+}

+ 19 - 18
internal/repository/memory/repository.go

@@ -8,23 +8,24 @@ import (
 // and accepts a parameter that can trigger read/write errors
 func NewRepository(canQuery bool) *repository.Repository {
 	return &repository.Repository{
-		User:                  NewUserRepository(canQuery),
-		Session:               NewSessionRepository(canQuery),
-		Project:               NewProjectRepository(canQuery),
-		Cluster:               NewClusterRepository(canQuery),
-		HelmRepo:              NewHelmRepoRepository(canQuery),
-		Registry:              NewRegistryRepository(canQuery),
-		GitRepo:               NewGitRepoRepository(canQuery),
-		Invite:                NewInviteRepository(canQuery),
-		AuthCode:              NewAuthCodeRepository(canQuery),
-		DNSRecord:             NewDNSRecordRepository(canQuery),
-		PWResetToken:          NewPWResetTokenRepository(canQuery),
-		KubeIntegration:       NewKubeIntegrationRepository(canQuery),
-		BasicIntegration:      NewBasicIntegrationRepository(canQuery),
-		OIDCIntegration:       NewOIDCIntegrationRepository(canQuery),
-		OAuthIntegration:      NewOAuthIntegrationRepository(canQuery),
-		GCPIntegration:        NewGCPIntegrationRepository(canQuery),
-		AWSIntegration:        NewAWSIntegrationRepository(canQuery),
-		GithubAppInstallation: NewGithubAppInstallationRepository(canQuery),
+		User:                      NewUserRepository(canQuery),
+		Session:                   NewSessionRepository(canQuery),
+		Project:                   NewProjectRepository(canQuery),
+		Cluster:                   NewClusterRepository(canQuery),
+		HelmRepo:                  NewHelmRepoRepository(canQuery),
+		Registry:                  NewRegistryRepository(canQuery),
+		GitRepo:                   NewGitRepoRepository(canQuery),
+		Invite:                    NewInviteRepository(canQuery),
+		AuthCode:                  NewAuthCodeRepository(canQuery),
+		DNSRecord:                 NewDNSRecordRepository(canQuery),
+		PWResetToken:              NewPWResetTokenRepository(canQuery),
+		KubeIntegration:           NewKubeIntegrationRepository(canQuery),
+		BasicIntegration:          NewBasicIntegrationRepository(canQuery),
+		OIDCIntegration:           NewOIDCIntegrationRepository(canQuery),
+		OAuthIntegration:          NewOAuthIntegrationRepository(canQuery),
+		GCPIntegration:            NewGCPIntegrationRepository(canQuery),
+		AWSIntegration:            NewAWSIntegrationRepository(canQuery),
+		GithubAppInstallation:     NewGithubAppInstallationRepository(canQuery),
+		GithubAppOAuthIntegration: NewGithubAppOAuthIntegrationRepository(canQuery),
 	}
 }

+ 22 - 21
internal/repository/repository.go

@@ -2,25 +2,26 @@ package repository
 
 // Repository collects the repositories for each model
 type Repository struct {
-	User                  UserRepository
-	Project               ProjectRepository
-	Release               ReleaseRepository
-	Session               SessionRepository
-	GitRepo               GitRepoRepository
-	Cluster               ClusterRepository
-	HelmRepo              HelmRepoRepository
-	Registry              RegistryRepository
-	Infra                 InfraRepository
-	GitActionConfig       GitActionConfigRepository
-	Invite                InviteRepository
-	AuthCode              AuthCodeRepository
-	DNSRecord             DNSRecordRepository
-	PWResetToken          PWResetTokenRepository
-	KubeIntegration       KubeIntegrationRepository
-	BasicIntegration      BasicIntegrationRepository
-	OIDCIntegration       OIDCIntegrationRepository
-	OAuthIntegration      OAuthIntegrationRepository
-	GCPIntegration        GCPIntegrationRepository
-	AWSIntegration        AWSIntegrationRepository
-	GithubAppInstallation GithubAppInstallationRepository
+	User                      UserRepository
+	Project                   ProjectRepository
+	Release                   ReleaseRepository
+	Session                   SessionRepository
+	GitRepo                   GitRepoRepository
+	Cluster                   ClusterRepository
+	HelmRepo                  HelmRepoRepository
+	Registry                  RegistryRepository
+	Infra                     InfraRepository
+	GitActionConfig           GitActionConfigRepository
+	Invite                    InviteRepository
+	AuthCode                  AuthCodeRepository
+	DNSRecord                 DNSRecordRepository
+	PWResetToken              PWResetTokenRepository
+	KubeIntegration           KubeIntegrationRepository
+	BasicIntegration          BasicIntegrationRepository
+	OIDCIntegration           OIDCIntegrationRepository
+	OAuthIntegration          OAuthIntegrationRepository
+	GCPIntegration            GCPIntegrationRepository
+	AWSIntegration            AWSIntegrationRepository
+	GithubAppInstallation     GithubAppInstallationRepository
+	GithubAppOAuthIntegration GithubAppOAuthIntegrationRepository
 }

+ 8 - 5
server/api/oauth_github_handler.go

@@ -353,24 +353,21 @@ func (app *App) HandleGithubAppOAuthCallback(w http.ResponseWriter, r *http.Requ
 		return
 	}
 
-	oauthInt := &integrations.OAuthIntegration{
+	oauthInt := &integrations.GithubAppOAuthIntegration{
 		SharedOAuthModel: integrations.SharedOAuthModel{
 			AccessToken:  []byte(token.AccessToken),
 			RefreshToken: []byte(token.RefreshToken),
 		},
-		Client: integrations.OAuthGithub,
 		UserID: user.ID,
 	}
 
-	oauthInt, err = app.Repo.OAuthIntegration.CreateUserOAuthIntegration(oauthInt)
+	oauthInt, err = app.Repo.GithubAppOAuthIntegration.CreateGithubAppOAuthIntegration(oauthInt)
 
 	if err != nil {
 		app.handleErrorInternal(err, w)
 		return
 	}
 
-	fmt.Println(oauthInt.ID)
-
 	user.GithubAppIntegrationID = oauthInt.ID
 
 	user, err = app.Repo.User.UpdateUser(user)
@@ -379,4 +376,10 @@ func (app *App) HandleGithubAppOAuthCallback(w http.ResponseWriter, r *http.Requ
 		app.handleErrorInternal(err, w)
 		return
 	}
+
+	if session.Values["query_params"] != "" {
+		http.Redirect(w, r, fmt.Sprintf("/dashboard?%s", session.Values["query_params"]), 302)
+	} else {
+		http.Redirect(w, r, "/dashboard", 302)
+	}
 }