login.go
author Mikael Berthe <mikael@lilotux.net>
Sat, 29 Apr 2017 17:27:15 +0200
changeset 156 70aadba26338
parent 138 23d3a518d0ad
child 159 408aa794d9bb
permissions -rw-r--r--
Add field "All" to LimitParams, change Limit behaviour If All is true, the library will send several requests (if needed) until the API server has sent all the results. If not, and if a Limit is set, the library will try to fetch at least this number of results.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
130
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 129
diff changeset
     1
/*
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 129
diff changeset
     2
Copyright 2017 Mikael Berthe
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 129
diff changeset
     3
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 129
diff changeset
     4
Licensed under the MIT license.  Please see the LICENSE file is this directory.
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 129
diff changeset
     5
*/
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 129
diff changeset
     6
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 135
diff changeset
     7
package madon
85
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
import (
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
	"encoding/json"
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
	"fmt"
107
f0db7634e540 Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    12
	"strings"
85
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
	"github.com/sendgrid/rest"
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
)
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
107
f0db7634e540 Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    17
// UserToken represents a user token as returned by the Mastodon API
85
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
type UserToken struct {
107
f0db7634e540 Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    19
	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
    20
	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
    21
	Scope       string `json:"scope"`
f0db7634e540 Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    22
	TokenType   string `json:"token_type"`
85
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
107
f0db7634e540 Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    25
// LoginBasic does basic user authentication
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 135
diff changeset
    26
func (mc *Client) LoginBasic(username, password string, scopes []string) error {
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 135
diff changeset
    27
	if mc == nil {
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 135
diff changeset
    28
		return ErrUninitializedClient
128
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
    29
	}
a5a00fad7a32 Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
    30
85
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
	if username == "" {
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
		return fmt.Errorf("missing username")
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
	if password == "" {
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
		return fmt.Errorf("missing password")
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
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
	hdrs := make(map[string]string)
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
	opts := make(map[string]string)
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 135
diff changeset
    41
	hdrs["User-Agent"] = "madon/" + MadonVersion
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
	opts["grant_type"] = "password"
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 135
diff changeset
    44
	opts["client_id"] = mc.ID
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 135
diff changeset
    45
	opts["client_secret"] = mc.Secret
85
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
	opts["username"] = username
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
	opts["password"] = password
107
f0db7634e540 Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    48
	if len(scopes) > 0 {
f0db7634e540 Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    49
		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
    50
	}
85
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
	req := rest.Request{
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 135
diff changeset
    53
		BaseURL:     mc.InstanceURL + "/oauth/token",
85
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
		Headers:     hdrs,
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
		QueryParams: opts,
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
		Method:      rest.Post,
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
125
2bbb72b9ebf6 Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents: 123
diff changeset
    59
	r, err := restAPI(req)
85
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
	if err != nil {
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
		return err
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
	}
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
	var resp UserToken
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    65
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    66
	err = json.Unmarshal([]byte(r.Body), &resp)
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
	if err != nil {
129
0d4544ce7e5e login: improve error message
Mikael Berthe <mikael@lilotux.net>
parents: 128
diff changeset
    68
		return fmt.Errorf("cannot unmarshal server response: %s", err.Error())
85
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    69
	}
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    70
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 135
diff changeset
    71
	mc.UserToken = &resp
85
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    72
	return nil
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    73
}
135
c578c80ed882 Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
    74
c578c80ed882 Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
    75
// SetUserToken sets an existing user credentials
c578c80ed882 Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
    76
// No verification of the arguments is made.
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 135
diff changeset
    77
func (mc *Client) SetUserToken(token, username, password string, scopes []string) error {
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 135
diff changeset
    78
	if mc == nil {
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 135
diff changeset
    79
		return ErrUninitializedClient
135
c578c80ed882 Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
    80
	}
c578c80ed882 Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
    81
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 135
diff changeset
    82
	mc.UserToken = &UserToken{
135
c578c80ed882 Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
    83
		AccessToken: token,
c578c80ed882 Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
    84
		Scope:       strings.Join(scopes, " "),
c578c80ed882 Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
    85
		TokenType:   "bearer",
c578c80ed882 Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
    86
	}
c578c80ed882 Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
    87
	return nil
c578c80ed882 Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
    88
}