login.go
author Mikael Berthe <mikael@lilotux.net>
Sun, 16 Apr 2017 13:37:37 +0200
changeset 128 a5a00fad7a32
parent 125 2bbb72b9ebf6
child 129 0d4544ce7e5e
permissions -rw-r--r--
Add checks for client initialization
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 {
128
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
    21
	if g == nil {
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
    22
		return fmt.Errorf("use of uninitialized gondole client")
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
    23
	}
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
    24
85
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
	if username == "" {
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
		return fmt.Errorf("missing username")
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
	if password == "" {
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
		return fmt.Errorf("missing password")
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
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
	hdrs := make(map[string]string)
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
	opts := make(map[string]string)
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
	hdrs["User-Agent"] = "Gondole/" + GondoleVersion
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
	opts["grant_type"] = "password"
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
	opts["client_id"] = g.ID
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
	opts["client_secret"] = g.Secret
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
	opts["username"] = username
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
	opts["password"] = password
107
f0db7634e540 Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    42
	if len(scopes) > 0 {
f0db7634e540 Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    43
		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
    44
	}
85
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
	req := rest.Request{
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
		BaseURL:     g.InstanceURL + "/oauth/token",
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
		Headers:     hdrs,
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    49
		QueryParams: opts,
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
		Method:      rest.Post,
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
	}
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
125
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents: 123
diff changeset
    53
	r, err := restAPI(req)
85
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
	if err != nil {
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
		return err
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
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
	var resp UserToken
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
	err = json.Unmarshal([]byte(r.Body), &resp)
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
	if err != nil {
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
		return err
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
	}
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
123
9b566c020a17 Export UserToken
Mikael Berthe <mikael@lilotux.net>
parents: 119
diff changeset
    65
	g.UserToken = &resp
85
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    66
	return nil
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
}