author | Mikael Berthe <mikael@lilotux.net> |
Sun, 13 Jan 2019 13:25:58 +0100 | |
changeset 247 | 17e32e951670 |
parent 207 | 301d5b94be3f |
permissions | -rw-r--r-- |
130 | 1 |
/* |
207 | 2 |
Copyright 2017-2018 Mikael Berthe |
130 | 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" |
|
107
f0db7634e540
Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents:
89
diff
changeset
|
11 |
"strings" |
85 | 12 |
|
199 | 13 |
"golang.org/x/net/context" |
178
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
14 |
"golang.org/x/oauth2" |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
15 |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
159
diff
changeset
|
16 |
"github.com/pkg/errors" |
85 | 17 |
"github.com/sendgrid/rest" |
18 |
) |
|
19 |
||
178
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
20 |
const oAuthRelPath = "/oauth/" |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
21 |
|
107
f0db7634e540
Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents:
89
diff
changeset
|
22 |
// UserToken represents a user token as returned by the Mastodon API |
85 | 23 |
type UserToken struct { |
107
f0db7634e540
Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents:
89
diff
changeset
|
24 |
AccessToken string `json:"access_token"` |
159
408aa794d9bb
s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
25 |
CreatedAt int64 `json:"created_at"` |
107
f0db7634e540
Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents:
89
diff
changeset
|
26 |
Scope string `json:"scope"` |
f0db7634e540
Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents:
89
diff
changeset
|
27 |
TokenType string `json:"token_type"` |
85 | 28 |
} |
29 |
||
107
f0db7634e540
Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents:
89
diff
changeset
|
30 |
// LoginBasic does basic user authentication |
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
135
diff
changeset
|
31 |
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
|
32 |
if mc == nil { |
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
135
diff
changeset
|
33 |
return ErrUninitializedClient |
128
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
34 |
} |
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
35 |
|
85 | 36 |
if username == "" { |
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
159
diff
changeset
|
37 |
return errors.New("missing username") |
85 | 38 |
} |
39 |
if password == "" { |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
159
diff
changeset
|
40 |
return errors.New("missing password") |
85 | 41 |
} |
42 |
||
43 |
hdrs := make(map[string]string) |
|
44 |
opts := make(map[string]string) |
|
45 |
||
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
135
diff
changeset
|
46 |
hdrs["User-Agent"] = "madon/" + MadonVersion |
85 | 47 |
|
48 |
opts["grant_type"] = "password" |
|
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
135
diff
changeset
|
49 |
opts["client_id"] = mc.ID |
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
135
diff
changeset
|
50 |
opts["client_secret"] = mc.Secret |
85 | 51 |
opts["username"] = username |
52 |
opts["password"] = password |
|
107
f0db7634e540
Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents:
89
diff
changeset
|
53 |
if len(scopes) > 0 { |
f0db7634e540
Add scopes to the basic login, fix some login bugs
Mikael Berthe <mikael@lilotux.net>
parents:
89
diff
changeset
|
54 |
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
|
55 |
} |
85 | 56 |
|
57 |
req := rest.Request{ |
|
178
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
58 |
BaseURL: mc.InstanceURL + oAuthRelPath + "token", |
85 | 59 |
Headers: hdrs, |
60 |
QueryParams: opts, |
|
61 |
Method: rest.Post, |
|
62 |
} |
|
63 |
||
125
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
123
diff
changeset
|
64 |
r, err := restAPI(req) |
85 | 65 |
if err != nil { |
66 |
return err |
|
67 |
} |
|
68 |
||
69 |
var resp UserToken |
|
70 |
||
71 |
err = json.Unmarshal([]byte(r.Body), &resp) |
|
72 |
if err != nil { |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
159
diff
changeset
|
73 |
return errors.Wrap(err, "cannot unmarshal server response") |
85 | 74 |
} |
75 |
||
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
135
diff
changeset
|
76 |
mc.UserToken = &resp |
85 | 77 |
return nil |
78 |
} |
|
135
c578c80ed882
Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
79 |
|
c578c80ed882
Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
80 |
// SetUserToken sets an existing user credentials |
c578c80ed882
Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
81 |
// No verification of the arguments is made. |
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
135
diff
changeset
|
82 |
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
|
83 |
if mc == nil { |
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
135
diff
changeset
|
84 |
return ErrUninitializedClient |
135
c578c80ed882
Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
85 |
} |
c578c80ed882
Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
86 |
|
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
135
diff
changeset
|
87 |
mc.UserToken = &UserToken{ |
135
c578c80ed882
Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
88 |
AccessToken: token, |
c578c80ed882
Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
89 |
Scope: strings.Join(scopes, " "), |
c578c80ed882
Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
90 |
TokenType: "bearer", |
c578c80ed882
Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
91 |
} |
c578c80ed882
Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
92 |
return nil |
c578c80ed882
Add SetUserToken() to restore a user token
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
93 |
} |
178
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
94 |
|
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
95 |
// LoginOAuth2 handles OAuth2 authentication |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
96 |
// If code is empty, the URL to the server consent page will be returned; |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
97 |
// if not, the user token is set. |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
98 |
func (mc *Client) LoginOAuth2(code string, scopes []string) (string, error) { |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
99 |
if mc == nil { |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
100 |
return "", ErrUninitializedClient |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
101 |
} |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
102 |
|
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
103 |
conf := &oauth2.Config{ |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
104 |
ClientID: mc.ID, |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
105 |
ClientSecret: mc.Secret, |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
106 |
Scopes: scopes, |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
107 |
Endpoint: oauth2.Endpoint{ |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
108 |
AuthURL: mc.InstanceURL + oAuthRelPath + "authorize", |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
109 |
TokenURL: mc.InstanceURL + oAuthRelPath + "token", |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
110 |
}, |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
111 |
RedirectURL: NoRedirect, |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
112 |
} |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
113 |
|
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
114 |
if code == "" { |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
115 |
// URL to consent page to ask for permission |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
116 |
// for the scopes specified above. |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
117 |
return conf.AuthCodeURL("state", oauth2.AccessTypeOffline), nil |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
118 |
} |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
119 |
|
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
120 |
// Return token |
199 | 121 |
t, err := conf.Exchange(context.TODO(), code) |
178
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
122 |
if err != nil { |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
123 |
return "", errors.Wrap(err, "cannot convert code into a token") |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
124 |
} |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
125 |
if t == nil || t.AccessToken == "" { |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
126 |
return "", errors.New("empty token") |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
127 |
} |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
128 |
return "", mc.SetUserToken(t.AccessToken, "", "", scopes) |
b63095e0f301
Add OAuth 2.0 authentication support
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
129 |
} |