Skip to content

Commit

Permalink
fix(api): store user organization on signup (#6035)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlt authored Dec 9, 2021
1 parent 24a6183 commit 71913cd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
7 changes: 3 additions & 4 deletions engine/api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,9 @@ func (api *API) postAuthSigninHandler() service.Handler {
}

u = &sdk.AuthentifiedUser{
Ring: sdk.UserRingUser,
Username: userInfo.Username,
Fullname: userInfo.Fullname,
Organization: userInfo.Organization,
Ring: sdk.UserRingUser,
Username: userInfo.Username,
Fullname: userInfo.Fullname,
}

// If a magic token is given and there is no admin already registered, set new user as admin
Expand Down
24 changes: 15 additions & 9 deletions engine/api/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -353,6 +354,13 @@ func Test_postAuthSigninHandler_WithCorporateSSO(t *testing.T) {
})

t.Run("Test_postAuthSigninHandler_WithCorporateSSO", func(t *testing.T) {
t.Cleanup(func() {
u, _ := user.LoadByUsername(context.TODO(), api.mustDB(), "mattgroening")
if u != nil {
require.NoError(t, user.DeleteByID(api.mustDB(), u.ID))
}
})

uri := api.Router.GetRoute(http.MethodPost, api.postAuthSigninHandler, map[string]string{
"consumerType": string(sdk.ConsumerCorporateSSO),
})
Expand All @@ -372,29 +380,27 @@ func Test_postAuthSigninHandler_WithCorporateSSO(t *testing.T) {

t.Logf("response: %s", string(bodyRaw))

assert.Equal(t, "mattgroening", response.User.GetUsername())
assert.NotEmpty(t, response.Token)
require.Equal(t, "mattgroening", response.User.GetUsername())
require.NotEmpty(t, response.Token)

u, err := user.LoadByUsername(context.TODO(), api.mustDB(), "mattgroening", user.LoadOptions.WithContacts)
u, err := user.LoadByUsername(context.TODO(), api.mustDB(), "mattgroening", user.LoadOptions.WithContacts, user.LoadOptions.WithOrganization)
require.NoError(t, err)
require.NotNil(t, u)
require.Equal(t, "planet-express", u.Organization)
require.Equal(t, "Mattgroening", u.Fullname)

consumer, err := authentication.LoadConsumerByTypeAndUserID(context.TODO(), api.mustDB(), sdk.ConsumerCorporateSSO, u.ID)
require.NoError(t, err)
assert.Equal(t, sdk.ConsumerCorporateSSO, consumer.Type)
require.Equal(t, sdk.ConsumerCorporateSSO, consumer.Type)

t.Logf("consumer %s: %+v", consumer.Type, consumer.Data)

// tear down
err = user.DeleteByID(api.mustDB(), u.ID)
require.NoError(t, err)
})
}

func generateToken(t *testing.T, username string) string {
ssoToken := corpsso.IssuedToken{
RemoteUser: username,
RemoteUsername: username,
RemoteUsername: strings.Title(username),
Email: username + "@planet-express.futurama",
Organization: "planet-express",
Audience: sdk.UUID(),
Expand Down
1 change: 1 addition & 0 deletions engine/api/authentication/corpsso/corpsso.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ func (d authDriver) GetUserInfo(ctx context.Context, req sdk.AuthConsumerSigninR
if len(u.Username) < 3 && itk.RemoteUsername != "" {
u.Username = slug.Convert(itk.RemoteUsername)
}
u.Fullname = itk.RemoteUsername
u.ExternalID = itk.RemoteUser
u.MFA = itk.MFA && d.Config.MFASupportEnabled
u.Email = itk.Email
Expand Down

0 comments on commit 71913cd

Please sign in to comment.