| 1 |
package generic |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"slices" |
| 7 |
|
| 8 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2" |
| 9 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/idtoken" |
| 10 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/types" |
| 11 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/state" |
| 12 |
) |
| 13 |
|
| 14 |
// CheckUser validates the resolved user and ID token against the configured generic provider rules. |
| 15 |
func (p Provider) CheckUser( |
| 16 |
_ context.Context, |
| 17 |
_ state.State, |
| 18 |
userInfo types.UserInfo, |
| 19 |
_ *idtoken.IDToken, |
| 20 |
) error { |
| 21 |
return p.CheckGroups(userInfo) |
| 22 |
} |
| 23 |
|
| 24 |
// CheckGroups verifies that the user belongs to at least one required group. |
| 25 |
func (p Provider) CheckGroups(userInfo types.UserInfo) error { |
| 26 |
if len(p.Conf.OAuth2.Validate.Groups) == 0 { |
| 27 |
return nil |
| 28 |
} |
| 29 |
|
| 30 |
if userInfo.Groups == nil { |
| 31 |
return fmt.Errorf("%w: groups", oauth2.ErrMissingClaim) |
| 32 |
} |
| 33 |
|
| 34 |
for _, group := range p.Conf.OAuth2.Validate.Groups { |
| 35 |
if slices.Contains(userInfo.Groups, group) { |
| 36 |
return nil |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
return oauth2.ErrMissingRequiredGroup |
| 41 |
} |