author | convert-repo |
Mon, 01 May 2017 06:34:53 +0000 | |
changeset 164 | bc53768d9c8c |
parent 162 | 68df3a01e1a7 |
child 178 | b63095e0f301 |
permissions | -rw-r--r-- |
130 | 1 |
/* |
2 |
Copyright 2017 Mikael Berthe |
|
3 |
||
4 |
Licensed under the MIT license. Please see the LICENSE file is this directory. |
|
5 |
*/ |
|
6 |
||
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
135
diff
changeset
|
7 |
package madon |
85 | 8 |
|
9 |
import ( |
|
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 | 12 |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
159
diff
changeset
|
13 |
"github.com/pkg/errors" |
85 | 14 |
"github.com/sendgrid/rest" |
15 |
) |
|
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 | 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 | 23 |
} |
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 | 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 | 33 |
} |
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 | 36 |
} |
37 |
||
38 |
hdrs := make(map[string]string) |
|
39 |
opts := make(map[string]string) |
|
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 | 42 |
|
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 | 46 |
opts["username"] = username |
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 | 51 |
|
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 | 54 |
Headers: hdrs, |
55 |
QueryParams: opts, |
|
56 |
Method: rest.Post, |
|
57 |
} |
|
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 | 60 |
if err != nil { |
61 |
return err |
|
62 |
} |
|
63 |
||
64 |
var resp UserToken |
|
65 |
||
66 |
err = json.Unmarshal([]byte(r.Body), &resp) |
|
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 | 69 |
} |
70 |
||
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
135
diff
changeset
|
71 |
mc.UserToken = &resp |
85 | 72 |
return nil |
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 |
} |