login.go
changeset 107 f0db7634e540
parent 89 8a7a33bec6e1
child 119 22c8c58ad61b
equal deleted inserted replaced
106:356507eb8db6 107:f0db7634e540
     1 package gondole
     1 package gondole
     2 
     2 
     3 import (
     3 import (
     4 	"encoding/json"
     4 	"encoding/json"
     5 	"fmt"
     5 	"fmt"
       
     6 	"strings"
     6 
     7 
     7 	"github.com/sendgrid/rest"
     8 	"github.com/sendgrid/rest"
     8 )
     9 )
     9 
    10 
       
    11 // UserToken represents a user token as returned by the Mastodon API
    10 type UserToken struct {
    12 type UserToken struct {
    11 	Access_token string `json:"access_token"`
    13 	AccessToken string `json:"access_token"`
    12 	CreatedAt    int    `json:"created_at"`
    14 	CreatedAt   int    `json:"created_at"`
    13 	Scope        string `json:"scope"`
    15 	Scope       string `json:"scope"`
    14 	TokenType    string `json:"token_type"`
    16 	TokenType   string `json:"token_type"`
    15 }
    17 }
    16 
    18 
    17 func (g *Client) LoginBasic(username, password string) error {
    19 // LoginBasic does basic user authentication
       
    20 func (g *Client) LoginBasic(username, password string, scopes []string) error {
    18 	if username == "" {
    21 	if username == "" {
    19 		return fmt.Errorf("missing username")
    22 		return fmt.Errorf("missing username")
    20 	}
    23 	}
    21 	if password == "" {
    24 	if password == "" {
    22 		return fmt.Errorf("missing password")
    25 		return fmt.Errorf("missing password")
    24 
    27 
    25 	hdrs := make(map[string]string)
    28 	hdrs := make(map[string]string)
    26 	opts := make(map[string]string)
    29 	opts := make(map[string]string)
    27 
    30 
    28 	hdrs["User-Agent"] = "Gondole/" + GondoleVersion
    31 	hdrs["User-Agent"] = "Gondole/" + GondoleVersion
    29 	hdrs["Authorization"] = "Bearer %s" + g.Secret
    32 	hdrs["Authorization"] = "Bearer " + g.Secret
    30 
    33 
    31 	opts["grant_type"] = "password"
    34 	opts["grant_type"] = "password"
    32 	opts["client_id"] = g.ID
    35 	opts["client_id"] = g.ID
    33 	opts["client_secret"] = g.Secret
    36 	opts["client_secret"] = g.Secret
    34 	opts["username"] = username
    37 	opts["username"] = username
    35 	opts["password"] = password
    38 	opts["password"] = password
       
    39 	if len(scopes) > 0 {
       
    40 		opts["scope"] = strings.Join(scopes, " ")
       
    41 	}
    36 
    42 
    37 	req := rest.Request{
    43 	req := rest.Request{
    38 		BaseURL:     g.InstanceURL + "/oauth/token",
    44 		BaseURL:     g.InstanceURL + "/oauth/token",
    39 		Headers:     hdrs,
    45 		Headers:     hdrs,
    40 		QueryParams: opts,
    46 		QueryParams: opts,
    46 		return err
    52 		return err
    47 	}
    53 	}
    48 
    54 
    49 	var resp UserToken
    55 	var resp UserToken
    50 
    56 
    51 	println(r.Body)
       
    52 	err = json.Unmarshal([]byte(r.Body), &resp)
    57 	err = json.Unmarshal([]byte(r.Body), &resp)
    53 	if err != nil {
    58 	if err != nil {
    54 		return err
    59 		return err
    55 	}
    60 	}
    56 
    61