--- a/cmd/gondole-cli/main.go Wed Apr 12 17:33:17 2017 +0200
+++ b/cmd/gondole-cli/main.go Wed Apr 12 17:36:27 2017 +0200
@@ -1,17 +1,20 @@
package main
import (
- "github.com/keltia/gondole"
- "github.com/urfave/cli"
"log"
"os"
"strings"
+
+ "github.com/urfave/cli"
+
+ "github.com/keltia/gondole"
)
var (
- fVerbose bool
- fInstance string
- fScopes string
+ fVerbose bool
+ fInstance string
+ fUsername, fPassword string
+ fScopes string
instance *gondole.Client
cnf *Server
@@ -135,6 +138,16 @@
Usage: "use these scopes",
Destination: &fScopes,
},
+ cli.StringFlag{
+ Name: "username,login",
+ Usage: "user name",
+ Destination: &fUsername,
+ },
+ cli.StringFlag{
+ Name: "password",
+ Usage: "user password",
+ Destination: &fPassword,
+ },
}
app.Run(os.Args)
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/login.go Wed Apr 12 17:36:27 2017 +0200
@@ -0,0 +1,59 @@
+package gondole
+
+import (
+ "encoding/json"
+ "fmt"
+
+ "github.com/sendgrid/rest"
+)
+
+type UserToken struct {
+ Access_token string `json:"access_token"`
+ CreatedAt int `json:"created_at"`
+ Scope string `json:"scope"`
+ TokenType string `json:"token_type"`
+}
+
+func (g *Client) Login(username, password string) error {
+ if username == "" {
+ return fmt.Errorf("missing username")
+ }
+ if password == "" {
+ return fmt.Errorf("missing password")
+ }
+
+ hdrs := make(map[string]string)
+ opts := make(map[string]string)
+
+ hdrs["User-Agent"] = "Gondole/" + GondoleVersion
+ hdrs["Authorization"] = "Bearer %s" + g.Secret
+
+ opts["grant_type"] = "password"
+ opts["client_id"] = g.ID
+ opts["client_secret"] = g.Secret
+ opts["username"] = username
+ opts["password"] = password
+
+ req := rest.Request{
+ BaseURL: g.InstanceURL + "/oauth/token",
+ Headers: hdrs,
+ QueryParams: opts,
+ Method: rest.Post,
+ }
+
+ r, err := rest.API(req)
+ if err != nil {
+ return err
+ }
+
+ var resp UserToken
+
+ println(r.Body)
+ err = json.Unmarshal([]byte(r.Body), &resp)
+ if err != nil {
+ return err
+ }
+
+ g.userToken = &resp
+ return nil
+}
--- a/types.go Wed Apr 12 17:33:17 2017 +0200
+++ b/types.go Wed Apr 12 17:36:27 2017 +0200
@@ -11,6 +11,8 @@
Secret string
APIBase string
InstanceURL string
+
+ userToken *UserToken
}
/*