login.go
author Mikael Berthe <mikael@lilotux.net>
Thu, 13 Apr 2017 13:44:09 +0200
changeset 107 f0db7634e540
parent 89 8a7a33bec6e1
child 119 22c8c58ad61b
permissions -rw-r--r--
Add scopes to the basic login, fix some login bugs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
85
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
package gondole
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
import (
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
	"encoding/json"
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
	"fmt"
107
f0db7634e540 Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
     6
	"strings"
85
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
	"github.com/sendgrid/rest"
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
)
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
107
f0db7634e540 Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    11
// UserToken represents a user token as returned by the Mastodon API
85
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
type UserToken struct {
107
f0db7634e540 Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    13
	AccessToken string `json:"access_token"`
f0db7634e540 Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    14
	CreatedAt   int    `json:"created_at"`
f0db7634e540 Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    15
	Scope       string `json:"scope"`
f0db7634e540 Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    16
	TokenType   string `json:"token_type"`
85
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
}
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
107
f0db7634e540 Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    19
// LoginBasic does basic user authentication
f0db7634e540 Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    20
func (g *Client) LoginBasic(username, password string, scopes []string) error {
85
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
	if username == "" {
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
		return fmt.Errorf("missing username")
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
	}
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
	if password == "" {
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
		return fmt.Errorf("missing password")
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
	}
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
	hdrs := make(map[string]string)
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
	opts := make(map[string]string)
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
	hdrs["User-Agent"] = "Gondole/" + GondoleVersion
107
f0db7634e540 Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    32
	hdrs["Authorization"] = "Bearer " + g.Secret
85
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
	opts["grant_type"] = "password"
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
	opts["client_id"] = g.ID
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
	opts["client_secret"] = g.Secret
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
	opts["username"] = username
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
	opts["password"] = password
107
f0db7634e540 Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    39
	if len(scopes) > 0 {
f0db7634e540 Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    40
		opts["scope"] = strings.Join(scopes, " ")
f0db7634e540 Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    41
	}
85
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
	req := rest.Request{
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
		BaseURL:     g.InstanceURL + "/oauth/token",
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
		Headers:     hdrs,
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
		QueryParams: opts,
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
		Method:      rest.Post,
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
	}
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    49
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
	r, err := rest.API(req)
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
	if err != nil {
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
		return err
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
	}
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
	var resp UserToken
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
	err = json.Unmarshal([]byte(r.Body), &resp)
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
	if err != nil {
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
		return err
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
	}
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
	g.userToken = &resp
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
	return nil
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
}