Skip to content

Commit

Permalink
Fix review comments
Browse files Browse the repository at this point in the history
Co-authored-by: Rita Zerrizuela <zeta@widcket.com>
  • Loading branch information
ewanharris and Widcket committed Jun 13, 2023
1 parent bbd484c commit 7072976
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 43 deletions.
1 change: 0 additions & 1 deletion authentication/authentication_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func newError(response *http.Response) error {
// This can happen in case the error message structure changes.
// If that happens we still want to display the correct code.
if apiError.Status() == 0 {
// Always set status code as it is not returned from te
apiError.StatusCode = response.StatusCode
apiError.Err = http.StatusText(response.StatusCode)
}
Expand Down
6 changes: 3 additions & 3 deletions authentication/authentication_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ func Test_newError(t *testing.T) {
expectedError authenticationError
}{
{
name: "it fails to decode if body is not a json",
name: "it fails to decode if body is not json",
givenResponse: http.Response{
StatusCode: http.StatusForbidden,
Body: io.NopCloser(strings.NewReader("Hello, I'm not a JSON.")),
Body: io.NopCloser(strings.NewReader("Hello, I'm not JSON.")),
},
expectedError: authenticationError{
StatusCode: 403,
Expand Down Expand Up @@ -52,7 +52,7 @@ func Test_newError(t *testing.T) {
},
},
{
name: "it will handle a invalid sign up response",
name: "it will handle an invalid sign up response",
givenResponse: http.Response{
StatusCode: http.StatusBadRequest,
Body: io.NopCloser(strings.NewReader(`{"name":"BadRequestError","code":"invalid_signup","description":"Invalid sign up","statusCode":400}`)),
Expand Down
2 changes: 1 addition & 1 deletion authentication/authentication_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func WithClientID(clientID string) Option {
}
}

// WithClientSecret configures the Client ID to be provided with requests if one is not provided.
// WithClientSecret configures the Client secret to be provided with requests if one is not provided.
func WithClientSecret(clientSecret string) Option {
return func(a *Authentication) {
a.clientSecret = clientSecret
Expand Down
19 changes: 3 additions & 16 deletions authentication/authentication_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
)

// URI returns the absolute URL of the Management API with any path segments
// URI returns the absolute URL of the Authentication API with any path segments
// appended to the end.
func (a *Authentication) URI(path ...string) string {
baseURL := &url.URL{
Expand All @@ -20,19 +20,7 @@ func (a *Authentication) URI(path ...string) string {
Path: a.basePath + "/",
}

const escapedForwardSlash = "%2F"
var escapedPath []string
for _, unescapedPath := range path {
// Go's url.PathEscape will not escape "/", but some user IDs do have a valid "/" in them.
// See https://github.com/golang/go/blob/b55a2fb3b0d67b346bac871737b862f16e5a6447/src/net/url/url.go#L141.
defaultPathEscaped := url.PathEscape(unescapedPath)
escapedPath = append(
escapedPath,
strings.ReplaceAll(defaultPathEscaped, "/", escapedForwardSlash),
)
}

return baseURL.String() + strings.Join(escapedPath, "/")
return baseURL.String() + strings.Join(path, "/")
}

// NewRequest returns a new HTTP request. If the payload is not nil it will be
Expand Down Expand Up @@ -175,8 +163,7 @@ func (a *Authentication) FormRequest(ctx context.Context, method, uri string, pa
return nil
}

// RequestOption configures a call (typically to retrieve a resource) to Auth0 with
// query parameters.
// RequestOption configures a call to Auth0 with query parameters.
type RequestOption interface {
apply(*http.Request, url.Values)
}
Expand Down
4 changes: 2 additions & 2 deletions authentication/authentication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestAuthenticationApiCallContextCancel(t *testing.T) {

assert.NoError(t, err)

_, err = a.Database.SignUp(ctx, database.SignUpRequest{
_, err = a.Database.Signup(ctx, database.SignupRequest{
Username: "test",
Password: "test",
})
Expand All @@ -117,7 +117,7 @@ func TestAuthenticationApiCallContextTimeout(t *testing.T) {

assert.NoError(t, err)

_, err = a.Database.SignUp(ctx, database.SignUpRequest{
_, err = a.Database.Signup(ctx, database.SignupRequest{
Username: "test",
Password: "test",
})
Expand Down
4 changes: 2 additions & 2 deletions authentication/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
// Database manager.
type Database manager

// SignUp given a user's credentials, and a connection, will create a new user using active authentication.
// Signup given a user's credentials and a connection, will create a new user using active authentication.
//
// This endpoint only works for database connections.
// See: https://auth0.com/docs/api/authentication?http#signup
func (d *Database) SignUp(ctx context.Context, params database.SignUpRequest) (r *database.SignUpResponse, err error) {
func (d *Database) Signup(ctx context.Context, params database.SignupRequest) (r *database.SignupResponse, err error) {
if params.ClientID == "" {
params.ClientID = d.authentication.clientID
}
Expand Down
14 changes: 7 additions & 7 deletions authentication/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"go.devnw.com/structs"
)

// SignUpRequest is a sign up request.
type SignUpRequest struct {
// SignupRequest is a sign up request.
type SignupRequest struct {
// The client_id of your client.
ClientID string `json:"client_id,omitempty"`
// The user's email address.
Expand All @@ -31,17 +31,17 @@ type SignUpRequest struct {
// The user metadata to be associated with the user. If set, the field must be an object containing no more than ten properties.
// Property names can have a maximum of 100 characters, and property values must be strings of no more than 500 characters.
UserMetadata *map[string]interface{} `json:"user_metadata,omitempty"`
// Extra parameters to be merged into the request body. Values set here will override any existing values
// Extra parameters to be merged into the request body. Values set here will override any existing values.
ExtraParameters map[string]string `json:"-"`
}

// SignUpResponse is a sign up response.
type SignUpResponse struct {
// SignupResponse is a sign up response.
type SignupResponse struct {
// The user's email address.
Email string `json:"email,omitempty"`
// Indicates whether a user has verified their email address.
EmailVerified bool `json:"email_verified,omitempty"`
// The users ID
// The user's ID.
ID string `json:"_id,omitempty"`
// The user's username. Only valid if the connection requires a username.
Username string `json:"username,omitempty"`
Expand All @@ -64,7 +64,7 @@ type SignUpResponse struct {
//
// It is required to support adding parameters from the `ExtraParameters`
// field onto the request object.
func (s *SignUpRequest) MarshalJSON() ([]byte, error) {
func (s *SignupRequest) MarshalJSON() ([]byte, error) {
n := structs.New(s)
n.TagName = "json"

Expand Down
4 changes: 2 additions & 2 deletions authentication/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (
func TestDatabaseSignUp(t *testing.T) {
configureHTTPTestRecordings(t)

userData := database.SignUpRequest{
userData := database.SignupRequest{
Connection: "Username-Password-Authentication",
Username: "mytestaccount",
Password: "mypassword",
Email: "mytestaccount@example.com",
}

createdUser, err := authAPI.Database.SignUp(context.Background(), userData)
createdUser, err := authAPI.Database.Signup(context.Background(), userData)
assert.NoError(t, err)
assert.NotEmpty(t, createdUser.ID)
assert.Equal(t, userData.Username, createdUser.Username)
Expand Down
10 changes: 5 additions & 5 deletions authentication/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ func (o *OAuth) LoginWithGrant(ctx context.Context, grantType string, body url.V
return
}

// LoginWithPassword is for logging in with information is typically received from a highly trusted
// LoginWithPassword is for logging in with information that is typically received from a highly trusted
// public client like a SPA.
// For single-page applications and native/mobile apps, we recommend using web flows instead.)
// For single-page applications and native/mobile apps, we recommend using web flows instead.
//
// See: https://auth0.com/docs/api/authentication#resource-owner-password
//
// Use the `Header` RequestOption to set the `auth0-forwarded-for` header to an end-users IP if you
// you want brute force protection to work in server-side scenarios
// See See https://auth0.com/docs/get-started/authentication-and-authorization-flow/avoid-common-issues-with-resource-owner-password-flow-and-attack-protection
// Use the `Header` RequestOption to set the `auth0-forwarded-for` header to an end-user's IP if you
// you want brute force protection to work in server-side scenarios.
// See https://auth0.com/docs/get-started/authentication-and-authorization-flow/avoid-common-issues-with-resource-owner-password-flow-and-attack-protection
func (o *OAuth) LoginWithPassword(ctx context.Context, body oauth.LoginWithPasswordRequest, opts ...RequestOption) (t *oauth.TokenSet, err error) {
grantType := "password"
data := url.Values{
Expand Down
8 changes: 4 additions & 4 deletions authentication/oauth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package oauth
type ClientAuthentication struct {
// ClientID to use for the specific request.
ClientID string
// ClientSecret to use for the specific request .Required when Client Secret Basic or Client
// ClientSecret to use for the specific request. Required when Client Secret Basic or Client
// Secret Post is the application authentication method.
ClientSecret string
}
Expand All @@ -13,7 +13,7 @@ type ClientAuthentication struct {
type TokenSet struct {
// The access token.
AccessToken string `json:"access_token,omitempty"`
// The duration in seconds. that the access token is valid.
// The duration in seconds that the access token is valid for.
ExpiresIn int64 `json:"expires_in,omitempty"`
// The user's ID token.
IDToken string `json:"id_token,omitempty"`
Expand All @@ -34,8 +34,8 @@ type LoginWithPasswordRequest struct {
Scope string
// The unique identifier of the target API you want to access.
Audience string
// String value of the realm the user belongs. Set this if you want to add realm support at this grant.
// String value of the realm the user belongs. Set this if you want to add realm support to this grant.
Realm string
// Extra parameters to be merged into the request body. Values set here will override any existing values
// Extra parameters to be merged into the request body. Values set here will override any existing values.
ExtraParameters map[string]string
}

0 comments on commit 7072976

Please sign in to comment.