author | Mikael Berthe <mikael@lilotux.net> |
Sat, 15 Apr 2017 21:08:34 +0200 | |
changeset 125 | 2bbb72b9ebf6 |
parent 123 | 9b566c020a17 |
child 128 | a5a00fad7a32 |
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 |
|
32 |
||
33 |
opts["grant_type"] = "password" |
|
34 |
opts["client_id"] = g.ID |
|
35 |
opts["client_secret"] = g.Secret |
|
36 |
opts["username"] = username |
|
37 |
opts["password"] = password |
|
107
f0db7634e540
Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents:
89
diff
changeset
|
38 |
if len(scopes) > 0 { |
f0db7634e540
Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents:
89
diff
changeset
|
39 |
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
|
40 |
} |
85 | 41 |
|
42 |
req := rest.Request{ |
|
43 |
BaseURL: g.InstanceURL + "/oauth/token", |
|
44 |
Headers: hdrs, |
|
45 |
QueryParams: opts, |
|
46 |
Method: rest.Post, |
|
47 |
} |
|
48 |
||
125
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
123
diff
changeset
|
49 |
r, err := restAPI(req) |
85 | 50 |
if err != nil { |
51 |
return err |
|
52 |
} |
|
53 |
||
54 |
var resp UserToken |
|
55 |
||
56 |
err = json.Unmarshal([]byte(r.Body), &resp) |
|
57 |
if err != nil { |
|
58 |
return err |
|
59 |
} |
|
60 |
||
123 | 61 |
g.UserToken = &resp |
85 | 62 |
return nil |
63 |
} |