author | Mikael Berthe <mikael@lilotux.net> |
Sun, 16 Apr 2017 20:52:52 +0200 | |
changeset 129 | 0d4544ce7e5e |
parent 128 | a5a00fad7a32 |
child 130 | c450bb73f59a |
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 { |
128
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
21 |
if g == nil { |
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
22 |
return fmt.Errorf("use of uninitialized gondole client") |
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
23 |
} |
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
24 |
|
85 | 25 |
if username == "" { |
26 |
return fmt.Errorf("missing username") |
|
27 |
} |
|
28 |
if password == "" { |
|
29 |
return fmt.Errorf("missing password") |
|
30 |
} |
|
31 |
||
32 |
hdrs := make(map[string]string) |
|
33 |
opts := make(map[string]string) |
|
34 |
||
35 |
hdrs["User-Agent"] = "Gondole/" + GondoleVersion |
|
36 |
||
37 |
opts["grant_type"] = "password" |
|
38 |
opts["client_id"] = g.ID |
|
39 |
opts["client_secret"] = g.Secret |
|
40 |
opts["username"] = username |
|
41 |
opts["password"] = password |
|
107
f0db7634e540
Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents:
89
diff
changeset
|
42 |
if len(scopes) > 0 { |
f0db7634e540
Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents:
89
diff
changeset
|
43 |
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
|
44 |
} |
85 | 45 |
|
46 |
req := rest.Request{ |
|
47 |
BaseURL: g.InstanceURL + "/oauth/token", |
|
48 |
Headers: hdrs, |
|
49 |
QueryParams: opts, |
|
50 |
Method: rest.Post, |
|
51 |
} |
|
52 |
||
125
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
123
diff
changeset
|
53 |
r, err := restAPI(req) |
85 | 54 |
if err != nil { |
55 |
return err |
|
56 |
} |
|
57 |
||
58 |
var resp UserToken |
|
59 |
||
60 |
err = json.Unmarshal([]byte(r.Body), &resp) |
|
61 |
if err != nil { |
|
129
0d4544ce7e5e
login: improve error message
Mikael Berthe <mikael@lilotux.net>
parents:
128
diff
changeset
|
62 |
return fmt.Errorf("cannot unmarshal server response: %s", err.Error()) |
85 | 63 |
} |
64 |
||
123 | 65 |
g.UserToken = &resp |
85 | 66 |
return nil |
67 |
} |