-
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
Integrate tenant authentication #92
Merged
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -50,6 +50,7 @@ func New(endpoint string, opts ...ClientOption) (_ TenantClient, err error) { | |
type APIv1 struct { | ||
endpoint *url.URL | ||
client *http.Client | ||
creds string | ||
} | ||
|
||
// Ensures the APIv1 implements the TenantClient interface | ||
|
@@ -795,7 +796,10 @@ func (s *APIv1) NewRequest(ctx context.Context, method, path string, data interf | |
req.Header.Add("Accept-Encoding", acceptEncode) | ||
req.Header.Add("Content-Type", contentType) | ||
|
||
// TODO: add authentication if it is available | ||
// Add authentication if it is available | ||
if s.creds != "" { | ||
req.Header.Add("Authorization", "Bearer "+s.creds) | ||
} | ||
|
||
// Adds CSRF protection if it is available | ||
if s.client.Jar != nil { | ||
|
@@ -846,3 +850,54 @@ func (s *APIv1) Do(req *http.Request, data interface{}, checkStatus bool) (rep * | |
|
||
return rep, nil | ||
} | ||
|
||
// SetCredentials is a helper function for external users to override credentials at | ||
// runtime by directly passing in the token, which is useful for testing. | ||
// TODO: Pass in a credentials interface instead of the token string. | ||
func (c *APIv1) SetCredentials(token string) { | ||
c.creds = token | ||
} | ||
|
||
// SetCSRFProtect is a helper function to set CSRF cookies on the client. This is not | ||
// possible in a browser because of the HttpOnly flag. This method should only be used | ||
// for testing purposes and an error is returned if the URL is not localhost. For live | ||
// clients - the server should set these cookies. If protect is false, then the cookies | ||
// are removed from the client by setting the cookies to an empty slice. | ||
func (c *APIv1) SetCSRFProtect(protect bool) error { | ||
Comment on lines
+861
to
+866
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. Is this implementation from the Admin API or is it different? 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. It's from the BFF API |
||
if c.client.Jar == nil { | ||
return errors.New("client does not have a cookie jar, cannot set cookies") | ||
} | ||
|
||
if c.endpoint.Hostname() != "127.0.0.1" && c.endpoint.Hostname() != "localhost" { | ||
return fmt.Errorf("csrf protect is for local testing only, cannot set cookies for %s", c.endpoint.Hostname()) | ||
} | ||
|
||
// The URL for the cookies | ||
u := c.endpoint.ResolveReference(&url.URL{Path: "/"}) | ||
|
||
var cookies []*http.Cookie | ||
if protect { | ||
cookies = []*http.Cookie{ | ||
{ | ||
Name: "csrf_token", | ||
Value: "testingcsrftoken", | ||
Expires: time.Now().Add(10 * time.Minute), | ||
HttpOnly: false, | ||
}, | ||
{ | ||
Name: "csrf_reference_token", | ||
Value: "testingcsrftoken", | ||
Expires: time.Now().Add(10 * time.Minute), | ||
HttpOnly: true, | ||
}, | ||
} | ||
} else { | ||
cookies = c.client.Jar.Cookies(u) | ||
for _, cookie := range cookies { | ||
cookie.MaxAge = -1 | ||
} | ||
} | ||
|
||
c.client.Jar.SetCookies(u, cookies) | ||
return nil | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Good stub!