-
Notifications
You must be signed in to change notification settings - Fork 41
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
fix: added validation check for user credentials #228
Changes from 1 commit
ba008f2
645c526
4836dc7
bab0120
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 |
---|---|---|
|
@@ -87,6 +87,24 @@ func (c *Client) doWithErr(req *http.Request) (*http.Response, error) { | |
return res, nil | ||
} | ||
|
||
func isAlphanumeric(word string) bool { | ||
return regexp.MustCompile(`^[a-zA-Z0-9]*$`).MatchString(word) | ||
} | ||
|
||
func (c *Client) validateCredentials() error { | ||
username, password := c.basicAuth() | ||
err := &TwilioRestError{Status: 400} | ||
if !isAlphanumeric(username) { | ||
err.Message = "Invalid Username" | ||
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. This is going to be pretty vague and confusing for end users - if I get "Invalid Username" it might not be obvious to me where the problem is, especially if e.g. the user is pulling a username from an environment variable or a web form. Can we make the error clearer about what the problem is? |
||
return err | ||
} | ||
if !isAlphanumeric(password) { | ||
err.Message = "Invalid Password" | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// SendRequest verifies, constructs, and authorizes an HTTP request. | ||
func (c *Client) SendRequest(method string, rawURL string, data url.Values, | ||
headers map[string]interface{}) (*http.Response, error) { | ||
|
@@ -112,6 +130,11 @@ func (c *Client) SendRequest(method string, rawURL string, data url.Values, | |
valueReader = strings.NewReader(data.Encode()) | ||
} | ||
|
||
credErr := c.validateCredentials() | ||
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 believe that there are some Twilio endpoints that allow data to be retrieved without a valid username and password so it would be great to know if there is a product spec for this. |
||
if credErr != nil { | ||
return nil, credErr | ||
} | ||
|
||
req, err := http.NewRequest(method, u.String(), valueReader) | ||
if err != nil { | ||
return nil, err | ||
|
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.
It's not very efficient to do this every single time someone calls
isAlphanumeric
- make the regexp a package level variable that gets initialized one time.Note, I believe this will also pass if
word
is the empty string. It would be pretty easy to add some basic smoke tests for this function.