Update New to allow an auth token to be specified.

This commit is contained in:
Chris Rhodes 2016-01-17 14:17:51 -08:00
parent e2ab871e12
commit 1d9f97e283
2 changed files with 23 additions and 20 deletions

View file

@ -20,9 +20,15 @@ const VERSION = "0.10.0-alpha"
// New creates a new Discord session and will automate some startup // New creates a new Discord session and will automate some startup
// tasks if given enough information to do so. Currently you can pass zero // tasks if given enough information to do so. Currently you can pass zero
// arguments and it will return an empty Discord session. If you pass a token // arguments and it will return an empty Discord session.
// or username and password (in that order), then it will attempt to login to // There are 3 ways to call New:
// Discord and open a websocket connection. // With a single auth token - All requests will use the token blindly,
// no verification of the token will be done and requests may fail.
// With an email and password - Discord will sign in with the provided
// credentials.
// With an email, password and auth token - Discord will verify the auth
// token, if it is invalid it will sign in with the provided
// credentials. This is the Discord recommended way to sign in.
func New(args ...interface{}) (s *Session, err error) { func New(args ...interface{}) (s *Session, err error) {
// Create an empty Session interface. // Create an empty Session interface.
@ -46,7 +52,7 @@ func New(args ...interface{}) (s *Session, err error) {
switch v := arg.(type) { switch v := arg.(type) {
case []string: case []string:
if len(v) > 2 { if len(v) > 3 {
err = fmt.Errorf("Too many string parameters provided.") err = fmt.Errorf("Too many string parameters provided.")
return return
} }
@ -61,6 +67,11 @@ func New(args ...interface{}) (s *Session, err error) {
pass = v[1] pass = v[1]
} }
// If third string exists, it must be an auth token.
if len(v) > 2 {
s.Token = v[2]
}
case string: case string:
// First string must be either auth token or username. // First string must be either auth token or username.
// Second string must be a password. // Second string must be a password.
@ -70,6 +81,8 @@ func New(args ...interface{}) (s *Session, err error) {
auth = v auth = v
} else if pass == "" { } else if pass == "" {
pass = v pass = v
} else if s.Token == "" {
s.Token = v
} else { } else {
err = fmt.Errorf("Too many string parameters provided.") err = fmt.Errorf("Too many string parameters provided.")
return return
@ -85,11 +98,13 @@ func New(args ...interface{}) (s *Session, err error) {
} }
// If only one string was provided, assume it is an auth token. // If only one string was provided, assume it is an auth token.
// Otherwise get auth token from Discord // Otherwise get auth token from Discord, if a token was specified
// Discord will verify it for free, or log the user in if it is
// invalid.
if pass == "" { if pass == "" {
s.Token = auth s.Token = auth
} else { } else {
s.Token, err = s.Login(auth, pass) err = s.Login(auth, pass)
if err != nil || s.Token == "" { if err != nil || s.Token == "" {
err = fmt.Errorf("Unable to fetch discord authentication token. %v", err) err = fmt.Errorf("Unable to fetch discord authentication token. %v", err)
return return

View file

@ -129,7 +129,7 @@ func unmarshal(data []byte, v interface{}) error {
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Login asks the Discord server for an authentication token. // Login asks the Discord server for an authentication token.
func (s *Session) Login(email, password string) (token string, err error) { func (s *Session) Login(email, password string) (err error) {
data := struct { data := struct {
Email string `json:"email"` Email string `json:"email"`
@ -150,19 +150,7 @@ func (s *Session) Login(email, password string) (token string, err error) {
return return
} }
token = temp.Token s.Token = temp.Token
return
}
// LoginWithToken will verify a login token, or return a new one if it is invalid.
// This is the preferred way to login, as it uses less rate limiting quota.
func (s *Session) LoginWithToken(email, password, token string) (newToken string, err error) {
old := s.Token
s.Token = token
newToken, err = s.Login(email, password)
s.Token = old
return return
} }