-
Notifications
You must be signed in to change notification settings - Fork 822
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
Add Login + 2FA #725
Conversation
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.
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 == "": |
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.
Should this comparison be a logical OR instead of AND? E.g., what happens if l.Email != nil
, but l.Password == nil
?
case l.Email == "" && l.Password == "": | |
case l.Email == "" || l.Password == "": |
@@ -119,6 +120,20 @@ type Session struct { | |||
wsMutex sync.Mutex | |||
} | |||
|
|||
type TOTP struct { |
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.
Exported structs should have leading comment for godoc documentation.
Ticket string `json:"ticket"` | ||
} | ||
|
||
type LoginResponse struct { |
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.
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) |
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.
This statement could be rewritten as:
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 { |
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.
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. |
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.
[Nit] Typo in function comment.
// 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} |
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.
[Nit] This could be rewritten with field names for clarity:
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 { |
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.
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 |
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.
[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:
var login LoginResponse | |
var loginResponse LoginResponse |
return nil, err | ||
} | ||
|
||
var login LoginResponse |
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.
[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:
var login LoginResponse | |
var loginResponse LoginResponse |
New Login struct doesn't break existing bots. ErrMFA is kept.
Potentially fixes #649.