author | Mikael Berthe <mikael@lilotux.net> |
Thu, 13 Apr 2017 13:44:09 +0200 | |
changeset 107 | f0db7634e540 |
parent 89 | 8a7a33bec6e1 |
child 119 | 22c8c58ad61b |
permissions | -rw-r--r-- |
85 | 1 |
package gondole |
2 |
||
3 |
import ( |
|
4 |
"encoding/json" |
|
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 | 7 |
|
8 |
"github.com/sendgrid/rest" |
|
9 |
) |
|
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 | 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 | 17 |
} |
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 { |
85 | 21 |
if username == "" { |
22 |
return fmt.Errorf("missing username") |
|
23 |
} |
|
24 |
if password == "" { |
|
25 |
return fmt.Errorf("missing password") |
|
26 |
} |
|
27 |
||
28 |
hdrs := make(map[string]string) |
|
29 |
opts := make(map[string]string) |
|
30 |
||
31 |
hdrs["User-Agent"] = "Gondole/" + GondoleVersion |
|
107
f0db7634e540
Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents:
89
diff
changeset
|
32 |
hdrs["Authorization"] = "Bearer " + g.Secret |
85 | 33 |
|
34 |
opts["grant_type"] = "password" |
|
35 |
opts["client_id"] = g.ID |
|
36 |
opts["client_secret"] = g.Secret |
|
37 |
opts["username"] = username |
|
38 |
opts["password"] = password |
|
107
f0db7634e540
Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents:
89
diff
changeset
|
39 |
if len(scopes) > 0 { |
f0db7634e540
Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents:
89
diff
changeset
|
40 |
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
|
41 |
} |
85 | 42 |
|
43 |
req := rest.Request{ |
|
44 |
BaseURL: g.InstanceURL + "/oauth/token", |
|
45 |
Headers: hdrs, |
|
46 |
QueryParams: opts, |
|
47 |
Method: rest.Post, |
|
48 |
} |
|
49 |
||
50 |
r, err := rest.API(req) |
|
51 |
if err != nil { |
|
52 |
return err |
|
53 |
} |
|
54 |
||
55 |
var resp UserToken |
|
56 |
||
57 |
err = json.Unmarshal([]byte(r.Body), &resp) |
|
58 |
if err != nil { |
|
59 |
return err |
|
60 |
} |
|
61 |
||
62 |
g.userToken = &resp |
|
63 |
return nil |
|
64 |
} |