Add scopes to the basic login, fix some login bugs
authorMikael Berthe <mikael@lilotux.net>
Thu, 13 Apr 2017 13:44:09 +0200
changeset 107 f0db7634e540
parent 106 356507eb8db6
child 108 3f21113728f4
Add scopes to the basic login, fix some login bugs
cmd/gondole-cli/main.go
gondole.go
login.go
--- a/cmd/gondole-cli/main.go	Thu Apr 13 13:44:09 2017 +0200
+++ b/cmd/gondole-cli/main.go	Thu Apr 13 13:44:09 2017 +0200
@@ -41,7 +41,7 @@
 	Name        string `json:"name"`
 	BearerToken string `json:"bearer_token"`
 	APIBase     string `json:"base_url"`
-	InstanceURL string `json:"base_url"`
+	InstanceURL string `json:"instance_url"`
 }
 
 type Config struct {
@@ -74,6 +74,13 @@
 
 	}
 
+	// Set scopes
+	if fScopes != "" {
+		scopes = strings.Split(fScopes, " ")
+	} else {
+		scopes = ourScopes
+	}
+
 	// Load configuration, will register if none is found
 	cnf, err = LoadConfig(instanceName)
 	if err == nil && cnf != nil {
@@ -100,13 +107,6 @@
 			log.Fatalf("error: can not write config for %s", instanceName)
 		}
 
-		// Now register this through OAuth
-		if fScopes != "" {
-			scopes = strings.Split(fScopes, " ")
-		} else {
-			scopes = ourScopes
-		}
-
 		instance, err = gondole.NewApp("gondole-cli", scopes, gondole.NoRedirect, instanceURL)
 
 		server := &Server{
@@ -129,12 +129,11 @@
 		if err != nil {
 			log.Fatalf("error: can not write config for %s", instance.Name)
 		}
-
 	}
 
 	// Log in to the instance
 	if fAuthMethod != "oauth2" {
-		err = instance.LoginBasic(fUsername, fPassword)
+		err = instance.LoginBasic(fUsername, fPassword, scopes)
 	}
 
 	return err
--- a/gondole.go	Thu Apr 13 13:44:09 2017 +0200
+++ b/gondole.go	Thu Apr 13 13:44:09 2017 +0200
@@ -39,7 +39,7 @@
 	// Insert our sig
 	hdrs["User-Agent"] = fmt.Sprintf("Gondole/%s", GondoleVersion)
 	if g.userToken != nil {
-		hdrs["Authorization"] = fmt.Sprintf("Bearer %s", g.userToken.Access_token)
+		hdrs["Authorization"] = fmt.Sprintf("Bearer %s", g.userToken.AccessToken)
 	}
 
 	req = rest.Request{
--- a/login.go	Thu Apr 13 13:44:09 2017 +0200
+++ b/login.go	Thu Apr 13 13:44:09 2017 +0200
@@ -3,18 +3,21 @@
 import (
 	"encoding/json"
 	"fmt"
+	"strings"
 
 	"github.com/sendgrid/rest"
 )
 
+// UserToken represents a user token as returned by the Mastodon API
 type UserToken struct {
-	Access_token string `json:"access_token"`
-	CreatedAt    int    `json:"created_at"`
-	Scope        string `json:"scope"`
-	TokenType    string `json:"token_type"`
+	AccessToken string `json:"access_token"`
+	CreatedAt   int    `json:"created_at"`
+	Scope       string `json:"scope"`
+	TokenType   string `json:"token_type"`
 }
 
-func (g *Client) LoginBasic(username, password string) error {
+// LoginBasic does basic user authentication
+func (g *Client) LoginBasic(username, password string, scopes []string) error {
 	if username == "" {
 		return fmt.Errorf("missing username")
 	}
@@ -26,13 +29,16 @@
 	opts := make(map[string]string)
 
 	hdrs["User-Agent"] = "Gondole/" + GondoleVersion
-	hdrs["Authorization"] = "Bearer %s" + g.Secret
+	hdrs["Authorization"] = "Bearer " + g.Secret
 
 	opts["grant_type"] = "password"
 	opts["client_id"] = g.ID
 	opts["client_secret"] = g.Secret
 	opts["username"] = username
 	opts["password"] = password
+	if len(scopes) > 0 {
+		opts["scope"] = strings.Join(scopes, " ")
+	}
 
 	req := rest.Request{
 		BaseURL:     g.InstanceURL + "/oauth/token",
@@ -48,7 +54,6 @@
 
 	var resp UserToken
 
-	println(r.Body)
 	err = json.Unmarshal([]byte(r.Body), &resp)
 	if err != nil {
 		return err