Skip to content

Commit a24fce9

Browse files
committed
update identity BB profile data parsing, add IdentityBBProfileFields to ApplicationOrganization model
1 parent 29e50af commit a24fce9

File tree

11 files changed

+79
-51
lines changed

11 files changed

+79
-51
lines changed

.secrets.baseline

+2-2
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@
316316
"filename": "driver\\web\\docs\\gen\\gen_types.go",
317317
"hashed_secret": "c9739eab2dfa093cc0e450bf0ea81a43ae67b581",
318318
"is_verified": false,
319-
"line_number": 1875
319+
"line_number": 1876
320320
}
321321
],
322322
"driver\\web\\docs\\resources\\admin\\auth\\login.yaml": [
@@ -347,5 +347,5 @@
347347
}
348348
]
349349
},
350-
"generated_at": "2024-11-25T23:02:14Z"
350+
"generated_at": "2024-11-27T23:13:12Z"
351351
}

core/auth/auth.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ func (a *Auth) applyOrgSignUpExternal(context storage.TransactionContext, authTy
480480

481481
var identityBBProfile *model.Profile
482482
if identityProviderSetting.IdentityBBBaseURL != "" {
483-
identityBBProfile, err = a.identityBB.GetUserProfile(identityProviderSetting.IdentityBBBaseURL, externalUser, externalCreds, l)
483+
identityBBProfile, err = a.identityBB.GetUserProfile(identityProviderSetting.IdentityBBBaseURL, externalUser, externalCreds, identityProviderSetting.IdentityBBProfileFields, l)
484484
if err != nil {
485485
l.WarnError(logutils.MessageAction(logutils.StatusError, "syncing", "identity bb data", nil), err)
486486
}
@@ -644,7 +644,7 @@ func (a *Auth) updateExternalUserIfNeeded(accountAuthType model.AccountAuthType,
644644

645645
var identityBBProfile *model.Profile
646646
if identityProviderSetting.IdentityBBBaseURL != "" {
647-
identityBBProfile, err = a.identityBB.GetUserProfile(identityProviderSetting.IdentityBBBaseURL, externalUser, externalCreds, l)
647+
identityBBProfile, err = a.identityBB.GetUserProfile(identityProviderSetting.IdentityBBBaseURL, externalUser, externalCreds, identityProviderSetting.IdentityBBProfileFields, l)
648648
if err != nil {
649649
l.WarnError(logutils.MessageAction(logutils.StatusError, "syncing", "identity bb data", nil), err)
650650
}

core/auth/interfaces.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ type ProfileBuildingBlock interface {
607607

608608
// IdentityBuildingBlock is used by auth to communicate with the identity building block.
609609
type IdentityBuildingBlock interface {
610-
GetUserProfile(baseURL string, externalUser model.ExternalSystemUser, externalAccessToken string, l *logs.Log) (*model.Profile, error)
610+
GetUserProfile(baseURL string, externalUser model.ExternalSystemUser, externalAccessToken string, profileFields map[string]string, l *logs.Log) (*model.Profile, error)
611611
}
612612

613613
// Emailer is used by core to send emails

core/model/application.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,9 @@ type IdentityProviderSetting struct {
384384

385385
UserSpecificFields []string `bson:"user_specific_fields"`
386386

387-
AlwaysSyncProfile bool `bson:"always_sync_profile"` // if true, profile data will be overwritten with data from external user on each login/refresh
388-
IdentityBBBaseURL string `bson:"identity_bb_base_url"`
387+
AlwaysSyncProfile bool `bson:"always_sync_profile"` // if true, profile data will be overwritten with data from external user on each login/refresh
388+
IdentityBBBaseURL string `bson:"identity_bb_base_url"`
389+
IdentityBBProfileFields map[string]string `bson:"identity_bb_profile_fields"` // a map from paths into the data returned by the Identity BB to keys in Profile.UnstructuredProperties
389390

390391
Roles map[string]string `bson:"roles"` //map[identity_provider_role]app_role_id
391392
Groups map[string]string `bson:"groups"` //map[identity_provider_group]app_group_id

core/model/user.go

+13-23
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"fmt"
2020
"reflect"
2121
"sort"
22-
"strings"
2322
"time"
2423

2524
"github.com/rokwire/logging-library-go/v2/errors"
@@ -89,37 +88,22 @@ type Privacy struct {
8988
}
9089

9190
// GetFieldVisibility determines the privacy setting for the account data at path
92-
func (p *Privacy) GetFieldVisibility(path string, visibilityMap map[string]interface{}) (string, error) {
93-
if len(visibilityMap) == 0 {
94-
if len(p.FieldVisibility) == 0 {
95-
return VisibilityPrivate, nil
96-
}
97-
visibilityMap = p.FieldVisibility
98-
}
99-
100-
splitPath := strings.Split(path, ".")
101-
var err error
102-
visibilityEntry, ok := visibilityMap[splitPath[0]]
103-
if !ok {
91+
func (p *Privacy) GetFieldVisibility(path string) (string, error) {
92+
visibilityEntry := utils.GetMapEntryFromPath(p.FieldVisibility, path)
93+
if visibilityEntry == nil {
10494
return VisibilityPrivate, nil
10595
}
96+
10697
visibility, ok := visibilityEntry.(string)
10798
if !ok {
108-
insideMap, ok := visibilityEntry.(map[string]interface{})
109-
if !ok {
110-
return "", errors.ErrorData(logutils.StatusInvalid, "privacy field visibility", nil)
111-
}
112-
visibility, err = p.GetFieldVisibility(strings.Join(splitPath[1:], "."), insideMap)
113-
if err != nil {
114-
return "", errors.WrapErrorAction(logutils.ActionGet, "account field visibility", &logutils.FieldArgs{"path": path}, err)
115-
}
99+
return "", errors.ErrorData(logutils.StatusInvalid, "privacy field visibility", &logutils.FieldArgs{"path": path})
116100
}
117101
return visibility, nil
118102
}
119103

120104
// IsFieldVisible determines whether the account data at path should be visible to the requesting user
121105
func (p *Privacy) IsFieldVisible(path string, isConnection bool) (bool, error) {
122-
visibility, err := p.GetFieldVisibility(path, nil)
106+
visibility, err := p.GetFieldVisibility(path)
123107
if err != nil {
124108
return false, errors.WrapErrorAction(logutils.ActionGet, "account field visibility", &logutils.FieldArgs{"path": path}, err)
125109
}
@@ -781,7 +765,7 @@ func (p Profile) Merge(src Profile) Profile {
781765
}
782766

783767
// ProfileFromMap parses a map and converts it into a Profile struct
784-
func ProfileFromMap(profileMap map[string]interface{}) Profile {
768+
func ProfileFromMap(profileMap map[string]interface{}, profileFields map[string]string) Profile {
785769
profile := Profile{UnstructuredProperties: make(map[string]interface{})}
786770
for key, val := range profileMap {
787771
if key == "first_name" {
@@ -828,6 +812,12 @@ func ProfileFromMap(profileMap map[string]interface{}) Profile {
828812
profile.UnstructuredProperties[key] = val
829813
}
830814
}
815+
816+
for path, profileKey := range profileFields {
817+
if value := utils.GetMapEntryFromPath(profileMap, path); value != nil {
818+
profile.UnstructuredProperties[profileKey] = value
819+
}
820+
}
831821
return profile
832822
}
833823

driven/identitybb/adapter.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type Adapter struct {
3535
}
3636

3737
// GetUserProfile gets user profile info for the provided user credentials
38-
func (a *Adapter) GetUserProfile(baseURL string, externalUser model.ExternalSystemUser, externalAccessToken string, l *logs.Log) (*model.Profile, error) {
38+
func (a *Adapter) GetUserProfile(baseURL string, externalUser model.ExternalSystemUser, externalAccessToken string, profileFields map[string]string, l *logs.Log) (*model.Profile, error) {
3939
if baseURL == "" || externalAccessToken == "" {
4040
return nil, errors.ErrorData(logutils.StatusMissing, "base url", nil)
4141
}
@@ -80,7 +80,7 @@ func (a *Adapter) GetUserProfile(baseURL string, externalUser model.ExternalSyst
8080
return nil, errors.WrapErrorAction(logutils.ActionUnmarshal, logutils.TypeResponseBody, nil, err)
8181
}
8282

83-
profile := model.ProfileFromMap(profileData)
83+
profile := model.ProfileFromMap(profileData, profileFields)
8484

8585
return &profile, nil
8686
}

driver/web/conversions_application.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,10 @@ func identityProviderSettingFromDef(item *Def.IdentityProviderSettings) *model.I
418418
if item.IdentityBbBaseUrl != nil {
419419
identityBBBaseURL = *item.IdentityBbBaseUrl
420420
}
421+
var identityBBProfileFields map[string]string
422+
if item.IdentityBbProfileFields != nil {
423+
identityBBProfileFields = *item.IdentityBbProfileFields
424+
}
421425

422426
var adminAppAccessRoles []string
423427
if item.AdminAppAccessRoles != nil {
@@ -427,8 +431,8 @@ func identityProviderSettingFromDef(item *Def.IdentityProviderSettings) *model.I
427431
return &model.IdentityProviderSetting{IdentityProviderID: item.IdentityProviderId, UserIdentifierField: item.UserIdentifierField,
428432
ExternalIDFields: externalIDFields, FirstNameField: firstNameField, MiddleNameField: middleNameField,
429433
LastNameField: lastNameField, EmailField: emailField, FerpaField: ferpaField, RolesField: rolesField, GroupsField: groupsField,
430-
UserSpecificFields: userSpecificFields, Roles: roles, Groups: groups,
431-
AlwaysSyncProfile: alwaysSyncProfile, IdentityBBBaseURL: identityBBBaseURL, AdminAppAccessRoles: adminAppAccessRoles}
434+
UserSpecificFields: userSpecificFields, Roles: roles, Groups: groups, AlwaysSyncProfile: alwaysSyncProfile,
435+
IdentityBBBaseURL: identityBBBaseURL, IdentityBBProfileFields: identityBBProfileFields, AdminAppAccessRoles: adminAppAccessRoles}
432436
}
433437

434438
func identityProviderSettingsToDef(items []model.IdentityProviderSetting) []Def.IdentityProviderSettings {

driver/web/docs/gen/def.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -6377,6 +6377,11 @@ components:
63776377
type: boolean
63786378
identity_bb_base_url:
63796379
type: string
6380+
identity_bb_profile_fields:
6381+
type: object
6382+
additionalProperties:
6383+
type: string
6384+
nullable: true
63806385
admin_app_access_roles:
63816386
type: array
63826387
items:

driver/web/docs/gen/gen_types.go

+17-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

driver/web/docs/schemas/application/IdentityProviderSettings.yaml

+6-1
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,14 @@ properties:
4242
type: string
4343
nullable: true
4444
always_sync_profile:
45-
type: boolean
45+
type: boolean
4646
identity_bb_base_url:
4747
type: string
48+
identity_bb_profile_fields:
49+
type: object
50+
additionalProperties:
51+
type: string
52+
nullable: true
4853
admin_app_access_roles:
4954
type: array
5055
items:

utils/utils.go

+22
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,28 @@ func GetPrintableString(v *string, defaultVal string) string {
227227
return defaultVal
228228
}
229229

230+
// GetMapEntryFromPath returns the data entry corresponding to path, a period-separated string
231+
func GetMapEntryFromPath(data map[string]interface{}, path string) interface{} {
232+
if len(data) == 0 {
233+
return nil
234+
}
235+
236+
splitPath := strings.Split(path, ".")
237+
entry, ok := data[splitPath[0]]
238+
if !ok {
239+
return nil
240+
}
241+
if len(splitPath) == 1 {
242+
return entry
243+
}
244+
245+
entryData, ok := entry.(map[string]interface{})
246+
if !ok {
247+
return nil
248+
}
249+
return GetMapEntryFromPath(entryData, strings.Join(splitPath[1:], "."))
250+
}
251+
230252
// StartTimer starts a timer with the given name, period, and function to call when the timer goes off
231253
func StartTimer(timer *time.Timer, timerDone chan bool, period time.Duration, periodicFunc func(), name string, logger *logs.Logger) {
232254
if logger != nil {

0 commit comments

Comments
 (0)