Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding the default user that is returned when the queue is empty #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ cfg := m.Config()
// ClientID string
// ClientSecret string
// Issuer string
//
//
// AccessTTL time.Duration
// RefreshTTL time.Duration
// }
Expand Down Expand Up @@ -97,6 +97,15 @@ m.QueueCode("12345")
// ...Request to m.AuthorizationEndpoint()
```

You can also override the default user that is returned:

```
user := &mockoidc.User{
// User details...
}
m.SetDefaultUser(user)
```

### Forcing Errors

Arbitrary errors can also be queued for handlers to return instead of their
Expand Down
10 changes: 9 additions & 1 deletion mockoidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func NewServer(key *rsa.PrivateKey) (*MockOIDC, error) {
CodeChallengeMethodsSupported: []string{"plain", "S256"},
Keypair: keypair,
SessionStore: NewSessionStore(),
UserQueue: &UserQueue{},
UserQueue: &UserQueue{DefaultUser: DefaultUser()},
ErrorQueue: &ErrorQueue{},
}, nil
}
Expand Down Expand Up @@ -169,6 +169,14 @@ func (m *MockOIDC) QueueUser(user User) {
m.UserQueue.Push(user)
}

// SetDefaultUser allows setting the default mock User object to return for
// authentication when the authentication queue is empty.
// Calls to the `authorization_endpoint` will return the set user instead of
// `DefaultUser()` when the queue is empty.
func (m *MockOIDC) SetDefaultUser(user User) {
m.UserQueue.SetDefaultUser(user)
}

// QueueCode allows adding mock code strings to the authentication queue.
// Calls to the `authorization_endpoint` will pop these code strings
// off the queue and create a session with them and return them as the
Expand Down
14 changes: 12 additions & 2 deletions queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import "sync"
type UserQueue struct {
sync.Mutex
Queue []User

DefaultUser User
}

// CodeQueue manages the queue of codes returned for each
Expand Down Expand Up @@ -37,12 +39,15 @@ func (q *UserQueue) Push(user User) {
q.Queue = append(q.Queue, user)
}

// Pop a User from the Queue. If empty, return `DefaultUser()`
// Pop a User from the Queue. If empty, return `q.DefaultUser` if set, otherwise
// `DefaultUser()`
func (q *UserQueue) Pop() User {
q.Lock()
defer q.Unlock()

if len(q.Queue) == 0 {
if len(q.Queue) == 0 && q.DefaultUser != nil {
return q.DefaultUser
} else if len(q.Queue) == 0 {
return DefaultUser()
}

Expand All @@ -51,6 +56,11 @@ func (q *UserQueue) Pop() User {
return user
}

// SetDefaultUser configures the queue with the default user to return when empty.
func (q *UserQueue) SetDefaultUser(user User) {
q.DefaultUser = user
}

// Push adds a code to the Queue to be returned by subsequent
// `authorization_endpoint` calls as the code
func (q *CodeQueue) Push(code string) {
Expand Down