Skip to content
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

Add Login + 2FA #725

Closed
wants to merge 1 commit into from
Closed

Conversation

diamondburned
Copy link

@diamondburned diamondburned commented Dec 31, 2019

New Login struct doesn't break existing bots. ErrMFA is kept.

Potentially fixes #649.

Copy link
Contributor

@ewohltman ewohltman left a comment

Choose a reason for hiding this comment

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

Nice chunk of work, at the least fixing the exported struct comments ought to fix the CI tests currently failing this PR 😄

case l.Token != "":
auth = l.Token

case l.Email == "" && l.Password == "":
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this comparison be a logical OR instead of AND? E.g., what happens if l.Email != nil, but l.Password == nil?

Suggested change
case l.Email == "" && l.Password == "":
case l.Email == "" || l.Password == "":

@@ -119,6 +120,20 @@ type Session struct {
wsMutex sync.Mutex
}

type TOTP struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

Exported structs should have leading comment for godoc documentation.

Ticket string `json:"ticket"`
}

type LoginResponse struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

Exported structs should have leading comment for godoc documentation.

}
return
if err = s.LoginMFA(auth, pass, mfa); err != nil {
return nil, fmt.Errorf(ErrFailedToken.Error()+". %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

This statement could be rewritten as:

Suggested change
return nil, fmt.Errorf(ErrFailedToken.Error()+". %v", err)
return nil, fmt.Errorf("%s: %s", ErrFailedToken, err)

// ErrFailedToken is returned if the Token is empty after authenticating.
var ErrFailedToken = errors.New("Unable to fetch discord authentication token")

type Login struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

Exported structs should have leading comment for godoc documentation.

@@ -222,6 +221,76 @@ func (s *Session) Login(email, password string) (err error) {
return
}

// LoginMFA tries to log in with or without a 2FA code. Only email and password
// and required.
Copy link
Contributor

Choose a reason for hiding this comment

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

[Nit] Typo in function comment.

Suggested change
// and required.
// are required.


// TOTP finishes logging in by sending over the final 2FA code.
func (s *Session) TOTP(ticket, code string) (*LoginResponse, error) {
totp := TOTP{code, nil, nil, ticket}
Copy link
Contributor

Choose a reason for hiding this comment

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

[Nit] This could be rewritten with field names for clarity:

Suggested change
totp := TOTP{code, nil, nil, ticket}
totp := TOTP{
Code: code,
GiftCodeSkuID: nil,
LoginSource: nil,
Ticket: ticket,
}

// ErrFailedToken is returned if the Token is empty after authenticating.
var ErrFailedToken = errors.New("Unable to fetch discord authentication token")

type Login struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

I like this new Login struct and that it takes care of a TODO 👍

// LoginMFA tries to log in with or without a 2FA code. Only email and password
// and required.
func (s *Session) LoginMFA(email, password, mfa string) (err error) {
var login LoginResponse
Copy link
Contributor

Choose a reason for hiding this comment

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

[Nit] This variable could be renamed for clarity to avoid confusion when reading it later in the function with the new Login struct. E.g:

Suggested change
var login LoginResponse
var loginResponse LoginResponse

return nil, err
}

var login LoginResponse
Copy link
Contributor

Choose a reason for hiding this comment

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

[Nit] This variable could be renamed for clarity to avoid confusion when reading it later in the functiion with the new Login struct. E.g:

Suggested change
var login LoginResponse
var loginResponse LoginResponse

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