author | Mikael Berthe <mikael@lilotux.net> |
Mon, 17 Apr 2017 19:31:59 +0200 | |
changeset 135 | c578c80ed882 |
parent 130 | c450bb73f59a |
child 138 | 23d3a518d0ad |
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 |
||
85 | 7 |
package gondole |
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 |
f0db7634e540
Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents:
89
diff
changeset
|
26 |
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
|
27 |
if g == nil { |
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
28 |
return fmt.Errorf("use of uninitialized gondole client") |
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 |
||
41 |
hdrs["User-Agent"] = "Gondole/" + GondoleVersion |
|
42 |
||
43 |
opts["grant_type"] = "password" |
|
44 |
opts["client_id"] = g.ID |
|
45 |
opts["client_secret"] = g.Secret |
|
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{ |
|
53 |
BaseURL: g.InstanceURL + "/oauth/token", |
|
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 |
||
123 | 71 |
g.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. |
c578c80ed882
Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
77 |
func (g *Client) SetUserToken(token, username, password string, scopes []string) error { |
c578c80ed882
Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
78 |
if g == nil { |
c578c80ed882
Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
79 |
return fmt.Errorf("use of uninitialized gondole client") |
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 |
|
c578c80ed882
Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
82 |
g.UserToken = &UserToken{ |
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 |
} |