-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Guard against changing consumer key. Fixes #1.
- Loading branch information
1 parent
a23cfee
commit 3edfc1f
Showing
2 changed files
with
44 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/micromdm/nanodep/client" | ||
) | ||
|
||
var CKMismatch = errors.New("mismatched consumer key") | ||
|
||
// CKCheck is a wrapper around token storage to validate the consumer key. | ||
// This attempts to prevent overwriting of incorrect auth tokens. | ||
type CKCheck struct { | ||
client.AuthTokensRetriever | ||
AuthTokensStorer | ||
} | ||
|
||
type AuthTokensStore interface { | ||
client.AuthTokensRetriever | ||
AuthTokensStorer | ||
} | ||
|
||
// NewCKCheck creates a new CKCheck. | ||
func NewCKCheck(store AuthTokensStore) *CKCheck { | ||
return &CKCheck{store, store} | ||
} | ||
|
||
// StoreAuthTokens first retrieves the existing auth tokens and checks to make | ||
// sure the consumer key of the provided auth tokens match before then storing | ||
// the provided auth tokens. | ||
func (t *CKCheck) StoreAuthTokens(ctx context.Context, name string, tokens *client.OAuth1Tokens) error { | ||
prevTokens, err := t.AuthTokensRetriever.RetrieveAuthTokens(ctx, name) | ||
if err != nil { | ||
return fmt.Errorf("retrieving auth tokens: %w", err) | ||
} | ||
if prevTokens.ConsumerKey != tokens.ConsumerKey { | ||
return CKMismatch | ||
} | ||
return t.AuthTokensStorer.StoreAuthTokens(ctx, name, tokens) | ||
} |