mirror of
https://github.com/go-gitea/gitea.git
synced 2026-09-21 05:24:49 +02:00
Fixed issues identified in PR review
This commit is contained in:
@@ -29,18 +29,20 @@ const maxGroupPages = 20
|
||||
|
||||
// Client calls Google Workspace APIs.
|
||||
type Client struct {
|
||||
httpClient *http.Client
|
||||
groupsEndpoint string
|
||||
claimName string
|
||||
httpClient *http.Client
|
||||
groupsEndpoint string
|
||||
claimName string
|
||||
failLoginOnAdditionalInfoError bool
|
||||
}
|
||||
|
||||
// NewClient creates a Client using the given authenticated HTTP client.
|
||||
// The client should be built from an OAuth2 token carrying IAMScope.
|
||||
func NewClient(httpClient *http.Client, claimName string) *Client {
|
||||
func NewClient(httpClient *http.Client, claimName string, failLoginOnAdditionalInfoError bool) *Client {
|
||||
return &Client{
|
||||
httpClient: httpClient,
|
||||
groupsEndpoint: defaultIAMGroupsEndpoint,
|
||||
claimName: claimName,
|
||||
httpClient: httpClient,
|
||||
groupsEndpoint: defaultIAMGroupsEndpoint,
|
||||
claimName: claimName,
|
||||
failLoginOnAdditionalInfoError: failLoginOnAdditionalInfoError,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,3 +131,8 @@ func (c *Client) FetchAdditionalInfo(ctx context.Context, user goth.User) (goth.
|
||||
user.RawData[c.claimName] = groups
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// FailLoginOnAdditionalInfoError implements oauth2.AdditionalInfoProvider.
|
||||
func (c *Client) FailLoginOnAdditionalInfoError() bool {
|
||||
return c.failLoginOnAdditionalInfoError
|
||||
}
|
||||
|
||||
@@ -11,13 +11,14 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/markbates/goth"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func newTestClient(t *testing.T, server *httptest.Server) *Client {
|
||||
t.Helper()
|
||||
c := NewClient(server.Client(), "groups")
|
||||
c := NewClient(server.Client(), "groups", false)
|
||||
c.groupsEndpoint = server.URL
|
||||
return c
|
||||
}
|
||||
@@ -124,3 +125,50 @@ func TestFetchGoogleGroups_InvalidJSON(t *testing.T) {
|
||||
require.Error(t, err)
|
||||
assert.Nil(t, groups)
|
||||
}
|
||||
|
||||
func TestFetchAdditionalInfo_InjectsClaimBeforeValidation(t *testing.T) {
|
||||
server := mockGroupsServer(t, "user@example.com", [][]string{
|
||||
{"required-group@example.com"},
|
||||
})
|
||||
defer server.Close()
|
||||
|
||||
c := newTestClient(t, server)
|
||||
c.claimName = "groups"
|
||||
|
||||
user := goth.User{
|
||||
Email: "user@example.com",
|
||||
RawData: map[string]any{},
|
||||
}
|
||||
|
||||
enriched, err := c.FetchAdditionalInfo(context.Background(), user)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify the claim is present and contains the group — simulating what
|
||||
// RequiredClaimName validation would check after enrichment runs.
|
||||
groups, ok := enriched.RawData["groups"]
|
||||
require.True(t, ok, "groups claim must be present in RawData after enrichment")
|
||||
groupSlice, ok := groups.([]string)
|
||||
require.True(t, ok)
|
||||
assert.Contains(t, groupSlice, "required-group@example.com")
|
||||
}
|
||||
|
||||
func TestFetchAdditionalInfo_ErrorDoesNotInjectClaim(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
_, _ = fmt.Fprint(w, `{"error":"forbidden"}`)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
c := newTestClient(t, server)
|
||||
c.claimName = "groups"
|
||||
|
||||
user := goth.User{
|
||||
Email: "user@example.com",
|
||||
RawData: map[string]any{},
|
||||
}
|
||||
|
||||
enriched, err := c.FetchAdditionalInfo(context.Background(), user)
|
||||
require.Error(t, err)
|
||||
_, hasGroups := enriched.RawData["groups"]
|
||||
assert.False(t, hasGroups)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user