Skip to content

Conversation

hiranya911
Copy link
Contributor

Since we migrated the user management API to use the Google API client (identitytoolkit package), this code has become bit of a readability nightmare:

  • Unnecessary indirection: We store all user parameters in maps and then "apply" those values to identitytoolkit types (see UserToCreate and UserToUpdate types).
  • Too many small/redundant helper functions.
  • Heavy use of reflection.

This PR addresses the above issues by:

  • Directly applying user parameters to the corresponding identitytoolkit types. Intermediate maps are no longer used.
  • Removing most of the redundant helpers, and performing validations explicitly where needed.
  • Removing all usages of reflection (these were mostly present in the helper functions).

auth/user_mgt.go Outdated
}

func (u *UserToCreate) validatedRequest() (*identitytoolkit.IdentitytoolkitRelyingpartySignupNewUserRequest, error) {
req := u.request() // it is allowed to create a user without any parameters
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tiny nit:
// creating a user without any parameters is allowed

Otherwise, it's ambiguous what "it" means.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

auth/user_mgt.go Outdated
@@ -107,83 +97,236 @@ type UserIterator struct {

// UserToCreate is the parameter struct for the CreateUser function.
type UserToCreate struct {
params map[string]interface{}
_req *identitytoolkit.IdentitytoolkitRelyingpartySignupNewUserRequest
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with prefixing names with underscore in go. Isn't that a python convention?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to somehow indicate in the code that this field should not be accessed directly. It should always be accessed via request() or validatedRequest() functions. This also works well with IDE assist, since typing u.req auto completes to u.request(). WDYT?

auth/user_mgt.go Outdated
@@ -107,83 +97,236 @@ type UserIterator struct {

// UserToCreate is the parameter struct for the CreateUser function.
type UserToCreate struct {
params map[string]interface{}
_req *identitytoolkit.IdentitytoolkitRelyingpartySignupNewUserRequest
fields struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason these are in a custom struct instead of just at the top level of the UserToCreate struct? If so, maybe a comment here explaining why would be helpful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose the level of indirection can be removed without taking a hit on the readability. Removed.

return fmt.Errorf("photo url must be a non-empty string")
}
return nil
}

func validateEmail(val interface{}) error {
email := val.(string)
func validateEmail(email string) error {
if email == "" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just slightly strange that you use len(s) == 0 above but use s == "" here. I'm not sure which one would be preferred, but it would be nice to be consistent. Also, the actual error string is worded slightly differently.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to == ""

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just slightly odd that "photo url must be a non-empty string", but "email must not be empty". Minor nit, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@hiranya911 hiranya911 assigned hiranya911 and unassigned bklimt Apr 23, 2018
@hiranya911 hiranya911 assigned bklimt and unassigned hiranya911 Apr 23, 2018
@hiranya911
Copy link
Contributor Author

Made the suggested changes, and left a follow up question for discussion.

@hiranya911
Copy link
Contributor Author

Thanks for the comments. Made the suggested changes as per comments stated here, and offline chat.

@hiranya911 hiranya911 assigned hiranya911 and unassigned bklimt Apr 23, 2018
@hiranya911 hiranya911 merged commit 8194437 into breaking_context_change Apr 23, 2018
@hiranya911 hiranya911 deleted the hkj-auth-cleanup branch April 23, 2018 20:06
hiranya911 pushed a commit that referenced this pull request May 7, 2018
* context signatures

* VerifyIDToken context

* custom token context

* fix context arguments

* custom token with claims context

* remove TODO

* integration_iid_name

* propagate ctx

* propagate to private functions

* snippets and tests ctx

* integration test user2 fix

* error message

* Revert "integration test user2 fix"

This reverts commit 57148ce.

* add context to AE, fix integration test after merge.

* Revert "error message"

This reverts commit db7c34a.

* Merged with latest dev

* Using the 1.6 import for context

* Dropping call to WithContext

* Cleaning up the auth package (#134)

* Cleaning up the auth package

* Variable grouping

* Removing some punctuation; Changed id to ID in error messages

* Adding a minor comment

* Cleaning up the auth (user management) code (#136)

* Cleaning up the auth (user management) code

* Added a few more tests

* Removed the fields inner type from UserToCreate and UserToUpdate

* Renamed _req variables; More consistent error messages
hiranya911 added a commit that referenced this pull request May 8, 2018
* Breaking context change (#88)

* context signatures

* VerifyIDToken context

* custom token context

* fix context arguments

* custom token with claims context

* remove TODO

* integration_iid_name

* propagate ctx

* propagate to private functions

* snippets and tests ctx

* integration test user2 fix

* error message

* Revert "integration test user2 fix"

This reverts commit 57148ce.

* add context to AE, fix integration test after merge.

* Revert "error message"

This reverts commit db7c34a.

* Merged with latest dev

* Using the 1.6 import for context

* Dropping call to WithContext

* Cleaning up the auth package (#134)

* Cleaning up the auth package

* Variable grouping

* Removing some punctuation; Changed id to ID in error messages

* Adding a minor comment

* Cleaning up the auth (user management) code (#136)

* Cleaning up the auth (user management) code

* Added a few more tests

* Removed the fields inner type from UserToCreate and UserToUpdate

* Renamed _req variables; More consistent error messages

* Removing golint checks from 1.6.x build (#141)

* Removing golint checks from 1.6.x build

* Minor reformat

* Bumped version to 3.0.0 (#142)

* Bumped version to 3.0.0

* Updated changelog
hiranya911 pushed a commit that referenced this pull request Jun 12, 2018
* context signatures

* VerifyIDToken context

* custom token context

* fix context arguments

* custom token with claims context

* remove TODO

* integration_iid_name

* propagate ctx

* propagate to private functions

* snippets and tests ctx

* integration test user2 fix

* error message

* Revert "integration test user2 fix"

This reverts commit 57148ce.

* add context to AE, fix integration test after merge.

* Revert "error message"

This reverts commit db7c34a.

* Merged with latest dev

* Using the 1.6 import for context

* Dropping call to WithContext

* Cleaning up the auth package (#134)

* Cleaning up the auth package

* Variable grouping

* Removing some punctuation; Changed id to ID in error messages

* Adding a minor comment

* Cleaning up the auth (user management) code (#136)

* Cleaning up the auth (user management) code

* Added a few more tests

* Removed the fields inner type from UserToCreate and UserToUpdate

* Renamed _req variables; More consistent error messages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants