@@ -76,20 +76,66 @@ func (c *Client) ListGroupUsers(ctx context.Context, groupId string, opts ...Lis
7676 return users , nil
7777}
7878
79- func (c * Client ) GroupByName (ctx context.Context , groupName string ) (* okta.Group , error ) {
79+ func (c * Client ) GroupByName (ctx context.Context , groupName string ) (okta.Group , error ) {
80+
8081 query := c .GroupAPI .ListGroups (ctx ).Q (groupName )
8182 oktaGroups , _ , err := query .Execute ()
8283 if err != nil {
83- return nil , fmt .Errorf ("failed to query okta group: %w" , err )
84+ return okta. Group {} , fmt .Errorf ("failed to query okta group: %w" , err )
8485 }
8586
8687 for _ , group := range oktaGroups {
8788 if group .Profile != nil && group .Profile .Name != nil && * group .Profile .Name == groupName {
88- return & group , nil
89+ return group , nil
90+ }
91+ }
92+
93+ return okta.Group {}, fmt .Errorf ("unable to find okta group %q" , groupName )
94+ }
95+
96+ func (c * Client ) GroupsByName (ctx context.Context , groupNames []string , batchsize int ) ([]okta.Group , error ) {
97+ groups := []okta.Group {}
98+
99+ batches := buildFilterNameBatches (groupNames , batchsize )
100+
101+ for _ , filter := range batches {
102+ // expanding stats to get the number of users in the group
103+ query := c .GroupAPI .ListGroups (ctx ).Search (filter ).Expand ("stats" )
104+ oktaGroups , resp , err := query .Execute ()
105+ for {
106+ if err != nil {
107+ return nil , fmt .Errorf ("failed to query okta group: %w" , err )
108+ }
109+
110+ groups = append (groups , oktaGroups ... )
111+
112+ if ! resp .HasNextPage () {
113+ break
114+ }
115+ oktaGroups = []okta.Group {}
116+ resp , err = resp .Next (& oktaGroups )
117+ }
118+ }
119+ return groups , nil
120+ }
121+
122+ func buildFilterNameBatches (groupNames []string , batchsize int ) []string {
123+ batches := []string {}
124+ for i := 0 ; i < len (groupNames ); i += batchsize {
125+ end := i + batchsize
126+ if end > len (groupNames ) {
127+ end = len (groupNames )
89128 }
129+ batches = append (batches , toFilterString (groupNames [i :end ]))
90130 }
131+ return batches
132+ }
91133
92- return nil , fmt .Errorf ("unable to find okta group %q" , groupName )
134+ func toFilterString (groupNames []string ) string {
135+ if len (groupNames ) == 0 {
136+ return ""
137+ }
138+ return fmt .Sprintf ("profile.name eq \" %s\" " , strings .Join (groupNames , "\" or profile.name eq \" " ))
93139}
94140
95141func jwkFromBytes (bytes []byte ) (* jose.JSONWebKey , error ) {
0 commit comments