Skip to content

Commit

Permalink
feat: empty user list matches any user
Browse files Browse the repository at this point in the history
  • Loading branch information
clambin committed Apr 26, 2024
1 parent bef74c1 commit 2e53ba0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/oauth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewHandler(provider, clientID, clientSecret, authURL string, logger *slog.L
}
}

// BaseHandler implements the provider-agnostic part of a Handler.
// BaseHandler implements the generic part of a Handler.
type BaseHandler struct {
oauth2.Config
HTTPClient *http.Client
Expand Down
8 changes: 4 additions & 4 deletions pkg/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ const stateSize = 32
// before redirecting to the oauth provider, we generate a random state. During callback, we then check if the oauth provider
// sent us back the same state. The state is maintained for a limited amount of time to prevent (very unlikely) replay attacks.
type Store[T any] struct {
cache *cache.Cache[string, T]
values *cache.Cache[string, T]
}

// New creates a new state Store
func New[T any](retention time.Duration) Store[T] {
return Store[T]{
cache: cache.New[string, T](retention, time.Minute),
values: cache.New[string, T](retention, time.Minute),
}
}

Expand All @@ -29,11 +29,11 @@ func (s Store[T]) Add(value T) string {
// theoretically this could fail, but in practice this will never happen.
_, _ = rand.Read(state)
encodedState := hex.EncodeToString(state)
s.cache.Add(encodedState, value)
s.values.Add(encodedState, value)
return encodedState
}

// Get checks if the state exists and returns the associated value
func (s Store[T]) Get(state string) (T, bool) {
return s.cache.Get(state)
return s.values.Get(state)
}
8 changes: 8 additions & 0 deletions pkg/whitelist/whitelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ func (w Whitelist) Contains(email string) bool {
return ok
}

// Match returns true if the email address is on the whitelist, or if the whitelist is empty
func (w Whitelist) Match(email string) bool {
if len(w) == 0 {
return true
}
return w.Contains(email)
}

func (w Whitelist) list() []string {
list := make([]string, 0, len(w))
for email := range w {
Expand Down
5 changes: 3 additions & 2 deletions pkg/whitelist/whitelist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ func Test_whitelist(t *testing.T) {
{
name: "empty",
emails: []string{},
want: assert.False,
email: "foo@example.com",
want: assert.True,
},
{
name: "case-insensitive",
Expand All @@ -43,7 +44,7 @@ func Test_whitelist(t *testing.T) {
t.Parallel()

list := New(tt.emails)
tt.want(t, list.Contains(tt.email))
tt.want(t, list.Match(tt.email))

sortedList := list.list()
slices.Sort(sortedList)
Expand Down

0 comments on commit 2e53ba0

Please # to comment.