equal
deleted
inserted
replaced
2 Copyright 2017 Mikael Berthe |
2 Copyright 2017 Mikael Berthe |
3 |
3 |
4 Licensed under the MIT license. Please see the LICENSE file is this directory. |
4 Licensed under the MIT license. Please see the LICENSE file is this directory. |
5 */ |
5 */ |
6 |
6 |
7 package gondole |
7 package madon |
8 |
8 |
9 import ( |
9 import ( |
10 "encoding/json" |
10 "encoding/json" |
11 "fmt" |
11 "fmt" |
12 "strings" |
12 "strings" |
21 Scope string `json:"scope"` |
21 Scope string `json:"scope"` |
22 TokenType string `json:"token_type"` |
22 TokenType string `json:"token_type"` |
23 } |
23 } |
24 |
24 |
25 // LoginBasic does basic user authentication |
25 // LoginBasic does basic user authentication |
26 func (g *Client) LoginBasic(username, password string, scopes []string) error { |
26 func (mc *Client) LoginBasic(username, password string, scopes []string) error { |
27 if g == nil { |
27 if mc == nil { |
28 return fmt.Errorf("use of uninitialized gondole client") |
28 return ErrUninitializedClient |
29 } |
29 } |
30 |
30 |
31 if username == "" { |
31 if username == "" { |
32 return fmt.Errorf("missing username") |
32 return fmt.Errorf("missing username") |
33 } |
33 } |
36 } |
36 } |
37 |
37 |
38 hdrs := make(map[string]string) |
38 hdrs := make(map[string]string) |
39 opts := make(map[string]string) |
39 opts := make(map[string]string) |
40 |
40 |
41 hdrs["User-Agent"] = "Gondole/" + GondoleVersion |
41 hdrs["User-Agent"] = "madon/" + MadonVersion |
42 |
42 |
43 opts["grant_type"] = "password" |
43 opts["grant_type"] = "password" |
44 opts["client_id"] = g.ID |
44 opts["client_id"] = mc.ID |
45 opts["client_secret"] = g.Secret |
45 opts["client_secret"] = mc.Secret |
46 opts["username"] = username |
46 opts["username"] = username |
47 opts["password"] = password |
47 opts["password"] = password |
48 if len(scopes) > 0 { |
48 if len(scopes) > 0 { |
49 opts["scope"] = strings.Join(scopes, " ") |
49 opts["scope"] = strings.Join(scopes, " ") |
50 } |
50 } |
51 |
51 |
52 req := rest.Request{ |
52 req := rest.Request{ |
53 BaseURL: g.InstanceURL + "/oauth/token", |
53 BaseURL: mc.InstanceURL + "/oauth/token", |
54 Headers: hdrs, |
54 Headers: hdrs, |
55 QueryParams: opts, |
55 QueryParams: opts, |
56 Method: rest.Post, |
56 Method: rest.Post, |
57 } |
57 } |
58 |
58 |
66 err = json.Unmarshal([]byte(r.Body), &resp) |
66 err = json.Unmarshal([]byte(r.Body), &resp) |
67 if err != nil { |
67 if err != nil { |
68 return fmt.Errorf("cannot unmarshal server response: %s", err.Error()) |
68 return fmt.Errorf("cannot unmarshal server response: %s", err.Error()) |
69 } |
69 } |
70 |
70 |
71 g.UserToken = &resp |
71 mc.UserToken = &resp |
72 return nil |
72 return nil |
73 } |
73 } |
74 |
74 |
75 // SetUserToken sets an existing user credentials |
75 // SetUserToken sets an existing user credentials |
76 // No verification of the arguments is made. |
76 // No verification of the arguments is made. |
77 func (g *Client) SetUserToken(token, username, password string, scopes []string) error { |
77 func (mc *Client) SetUserToken(token, username, password string, scopes []string) error { |
78 if g == nil { |
78 if mc == nil { |
79 return fmt.Errorf("use of uninitialized gondole client") |
79 return ErrUninitializedClient |
80 } |
80 } |
81 |
81 |
82 g.UserToken = &UserToken{ |
82 mc.UserToken = &UserToken{ |
83 AccessToken: token, |
83 AccessToken: token, |
84 Scope: strings.Join(scopes, " "), |
84 Scope: strings.Join(scopes, " "), |
85 TokenType: "bearer", |
85 TokenType: "bearer", |
86 } |
86 } |
87 return nil |
87 return nil |