author | Mikael Berthe <mikael@lilotux.net> |
Sat, 29 Apr 2017 17:30:58 +0200 | |
changeset 157 | 6e9d927d5e32 |
parent 138 | 23d3a518d0ad |
child 159 | 408aa794d9bb |
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" |
|
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 | 13 |
|
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"` |
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 | 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 == "" { |
32 |
return fmt.Errorf("missing username") |
|
33 |
} |
|
34 |
if password == "" { |
|
35 |
return fmt.Errorf("missing password") |
|
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 { |
|
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 | 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 |
} |