-
Notifications
You must be signed in to change notification settings - Fork 3
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
feat: validate access token #308
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/launchdarkly/ldcli/internal/resources" | ||
) | ||
|
||
type Service struct { | ||
client resources.Client | ||
} | ||
|
||
func NewService(client resources.Client) Service { | ||
return Service{ | ||
client: client, | ||
} | ||
} | ||
|
||
// VerifyAccessToken is true if the given access token is valid to make API requests. | ||
func (s Service) VerifyAccessToken(accessToken string, baseURI string) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could move more of the |
||
path := fmt.Sprintf( | ||
"%s/api/v2/account", | ||
baseURI, | ||
) | ||
|
||
_, err := s.client.MakeRequest(accessToken, "HEAD", path, "application/json", nil, nil) | ||
|
||
return err == nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
package resources | ||
|
||
import "net/url" | ||
import ( | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
type MockClient struct { | ||
Input []byte | ||
Response []byte | ||
Err error | ||
Input []byte | ||
Response []byte | ||
StatusCode int | ||
} | ||
|
||
var _ Client = &MockClient{} | ||
|
@@ -16,5 +21,9 @@ func (c *MockClient) MakeRequest( | |
) ([]byte, error) { | ||
c.Input = data | ||
|
||
if c.StatusCode > http.StatusBadRequest { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added this for stubbing the HTTP client response. |
||
return c.Response, c.Err | ||
} | ||
|
||
return c.Response, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pass in the service so we can mock its HTTP client in tests.