| 1 |
package google |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
|
| 7 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/idtoken" |
| 8 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/types" |
| 9 |
) |
| 10 |
|
| 11 |
// resolveGroupMemberships replaces userData.Groups with the configured required |
| 12 |
// groups that the user is a member of. Do not stop after the first match: |
| 13 |
// client-specific configuration can depend on the complete resolved group list. |
| 14 |
func (p Provider) resolveGroupMemberships(ctx context.Context, userData *types.UserInfo, tokens *idtoken.IDToken) error { |
| 15 |
if tokens.AccessToken == "" { |
| 16 |
return errors.New("access token is empty") |
| 17 |
} |
| 18 |
|
| 19 |
userData.Groups = make([]string, 0, len(p.Conf.OAuth2.Validate.Groups)) |
| 20 |
|
| 21 |
for _, group := range p.Conf.OAuth2.Validate.Groups { |
| 22 |
isMember, err := p.isGroupMember(ctx, group, *userData, tokens) |
| 23 |
if err != nil { |
| 24 |
return err |
| 25 |
} |
| 26 |
|
| 27 |
if isMember { |
| 28 |
userData.Groups = append(userData.Groups, group) |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
return nil |
| 33 |
} |
| 34 |
|
| 35 |
// isGroupMember dispatches to the direct or transitive membership check based on configuration. |
| 36 |
func (p Provider) isGroupMember(ctx context.Context, group string, userData types.UserInfo, tokens *idtoken.IDToken) (bool, error) { |
| 37 |
if p.Conf.Provider.Google.Validate.GroupsTransitive { |
| 38 |
return p.checkTransitiveGroupMembership(ctx, group, userData, tokens) |
| 39 |
} |
| 40 |
|
| 41 |
return p.checkGroupMembership(ctx, group, userData, tokens) |
| 42 |
} |