login.go
author convert-repo
Mon, 01 May 2017 12:29:23 +0000
changeset 167 b2cee3fad4fe
parent 162 68df3a01e1a7
child 178 b63095e0f301
permissions -rw-r--r--
update tags
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"
107
f0db7634e540 Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents: 89
diff changeset
    11
	"strings"
85
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
162
68df3a01e1a7 Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    13
	"github.com/pkg/errors"
85
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"`
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
    20
	CreatedAt   int64  `json:"created_at"`
107
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 == "" {
162
68df3a01e1a7 Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    32
		return errors.New("missing username")
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
	if password == "" {
162
68df3a01e1a7 Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    35
		return errors.New("missing password")
85
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 {
162
68df3a01e1a7 Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents: 159
diff changeset
    68
		return errors.Wrap(err, "cannot unmarshal server response")
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
}