login.go
author Mikael Berthe <mikael@lilotux.net>
Wed, 12 Apr 2017 22:26:52 +0200
changeset 89 8a7a33bec6e1
parent 85 abf0f5e40281
child 107 f0db7634e540
permissions -rw-r--r--
Merge remote-tracking branch 'upstream/develop' into mckael-experiments_2 Conflicts: cmd/gondole-cli/main.go
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
85
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
package gondole
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
import (
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
	"encoding/json"
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
	"fmt"
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
	"github.com/sendgrid/rest"
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
)
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
type UserToken struct {
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
	Access_token string `json:"access_token"`
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
	CreatedAt    int    `json:"created_at"`
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
	Scope        string `json:"scope"`
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
	TokenType    string `json:"token_type"`
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
}
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
89
8a7a33bec6e1 Merge remote-tracking branch 'upstream/develop' into mckael-experiments_2
Mikael Berthe <mikael@lilotux.net>
parents: 85
diff changeset
    17
func (g *Client) LoginBasic(username, password string) error {
85
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
	if username == "" {
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
		return fmt.Errorf("missing username")
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
	}
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
	if password == "" {
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
		return fmt.Errorf("missing password")
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
	}
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
	hdrs := make(map[string]string)
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
	opts := make(map[string]string)
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
	hdrs["User-Agent"] = "Gondole/" + GondoleVersion
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
	hdrs["Authorization"] = "Bearer %s" + g.Secret
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
	opts["grant_type"] = "password"
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
	opts["client_id"] = g.ID
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
	opts["client_secret"] = g.Secret
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
	opts["username"] = username
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
	opts["password"] = password
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
	req := rest.Request{
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
		BaseURL:     g.InstanceURL + "/oauth/token",
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
		Headers:     hdrs,
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
		QueryParams: opts,
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
		Method:      rest.Post,
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
	}
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
	r, err := rest.API(req)
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
	if err != nil {
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
		return err
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
	}
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    49
	var resp UserToken
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
	println(r.Body)
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
	err = json.Unmarshal([]byte(r.Body), &resp)
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
	if err != nil {
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
		return err
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
	}
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
	g.userToken = &resp
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
	return nil
abf0f5e40281 Initial login support
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
}