Skip to content

Commit

Permalink
added additional check for empty ExternalAuth data in case the provid…
Browse files Browse the repository at this point in the history
…er api changes
  • Loading branch information
ganigeorgiev committed Sep 2, 2022
1 parent 0ac4c9e commit 93b3788
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions daos/external_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (dao *Dao) FindExternalAuthByProvider(provider, providerId string) (*models
model := &models.ExternalAuth{}

err := dao.ExternalAuthQuery().
AndWhere(dbx.Not(dbx.HashExp{"providerId": ""})). // exclude empty providerIds
AndWhere(dbx.HashExp{
"provider": provider,
"providerId": providerId,
Expand Down Expand Up @@ -71,6 +72,12 @@ func (dao *Dao) FindExternalAuthByUserIdAndProvider(userId, provider string) (*m

// SaveExternalAuth upserts the provided ExternalAuth model.
func (dao *Dao) SaveExternalAuth(model *models.ExternalAuth) error {
// extra check the model data in case the provider's API response
// changes and no longer returns the expected fields
if model.UserId == "" || model.Provider == "" || model.ProviderId == "" {
return errors.New("Missing required ExternalAuth fields.")
}

return dao.Save(model)
}

Expand Down
6 changes: 6 additions & 0 deletions daos/external_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ func TestSaveExternalAuth(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()

// save with empty provider data
emptyAuth := &models.ExternalAuth{}
if err := app.Dao().SaveExternalAuth(emptyAuth); err == nil {
t.Fatal("Expected error, got nil")
}

auth := &models.ExternalAuth{
UserId: "97cc3d3d-6ba2-383f-b42a-7bc84d27410c",
Provider: "test",
Expand Down

0 comments on commit 93b3788

Please sign in to comment.