2017-02-16 17:05:55 +00:00
|
|
|
package oauth2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/influxdata/chronograf"
|
|
|
|
|
|
|
|
"golang.org/x/oauth2"
|
2017-02-16 17:56:01 +00:00
|
|
|
hrk "golang.org/x/oauth2/heroku"
|
2017-02-16 17:05:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Ensure that Heroku is an oauth2.Provider
|
|
|
|
var _ Provider = &Heroku{}
|
|
|
|
|
|
|
|
const (
|
2017-03-06 16:11:52 +00:00
|
|
|
// HerokuAccountRoute is required for interacting with Heroku API
|
|
|
|
HerokuAccountRoute string = "https://api.heroku.com/account"
|
2017-02-16 17:05:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Heroku is an OAuth2 Provider allowing users to authenticate with Heroku to
|
|
|
|
// gain access to Chronograf
|
|
|
|
type Heroku struct {
|
|
|
|
// OAuth2 Secrets
|
|
|
|
ClientID string
|
|
|
|
ClientSecret string
|
|
|
|
|
2017-02-21 18:04:17 +00:00
|
|
|
Organizations []string // set of organizations permitted to access the protected resource. Empty means "all"
|
|
|
|
|
2017-02-16 17:05:55 +00:00
|
|
|
Logger chronograf.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config returns the OAuth2 exchange information and endpoints
|
|
|
|
func (h *Heroku) Config() *oauth2.Config {
|
2017-02-16 17:56:01 +00:00
|
|
|
return &oauth2.Config{
|
|
|
|
ClientID: h.ID(),
|
|
|
|
ClientSecret: h.Secret(),
|
|
|
|
Scopes: h.Scopes(),
|
|
|
|
Endpoint: hrk.Endpoint,
|
|
|
|
}
|
2017-02-16 17:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns the Heroku application client ID
|
|
|
|
func (h *Heroku) ID() string {
|
|
|
|
return h.ClientID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of this provider (heroku)
|
|
|
|
func (h *Heroku) Name() string {
|
|
|
|
return "heroku"
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrincipalID returns the Heroku email address of the user.
|
|
|
|
func (h *Heroku) PrincipalID(provider *http.Client) (string, error) {
|
2017-02-21 18:04:17 +00:00
|
|
|
type DefaultOrg struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
type Account struct {
|
|
|
|
Email string `json:"email"`
|
|
|
|
DefaultOrganization DefaultOrg `json:"default_organization"`
|
|
|
|
}
|
|
|
|
|
2017-03-06 16:11:52 +00:00
|
|
|
resp, err := provider.Get(HerokuAccountRoute)
|
2017-02-16 17:05:55 +00:00
|
|
|
if err != nil {
|
|
|
|
h.Logger.Error("Unable to communicate with Heroku. err:", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
d := json.NewDecoder(resp.Body)
|
2017-03-06 16:11:52 +00:00
|
|
|
|
2017-02-21 18:04:17 +00:00
|
|
|
var account Account
|
2017-02-16 17:05:55 +00:00
|
|
|
if err := d.Decode(&account); err != nil {
|
|
|
|
h.Logger.Error("Unable to decode response from Heroku. err:", err)
|
|
|
|
return "", err
|
|
|
|
}
|
2017-02-21 18:04:17 +00:00
|
|
|
|
|
|
|
// check if member of org
|
|
|
|
if len(h.Organizations) > 0 {
|
|
|
|
for _, org := range h.Organizations {
|
|
|
|
if account.DefaultOrganization.Name == org {
|
|
|
|
return account.Email, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
h.Logger.Error(ErrOrgMembership)
|
|
|
|
return "", ErrOrgMembership
|
|
|
|
}
|
2017-03-06 16:11:52 +00:00
|
|
|
return account.Email, nil
|
2017-02-16 17:05:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-02 23:48:58 +00:00
|
|
|
// Group returns the Heroku organization that user belongs to.
|
|
|
|
func (h *Heroku) Group(provider *http.Client) (string, error) {
|
|
|
|
type DefaultOrg struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
type Account struct {
|
|
|
|
Email string `json:"email"`
|
|
|
|
DefaultOrganization DefaultOrg `json:"default_organization"`
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := provider.Get(HerokuAccountRoute)
|
|
|
|
if err != nil {
|
|
|
|
h.Logger.Error("Unable to communicate with Heroku. err:", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
d := json.NewDecoder(resp.Body)
|
|
|
|
|
|
|
|
var account Account
|
|
|
|
if err := d.Decode(&account); err != nil {
|
|
|
|
h.Logger.Error("Unable to decode response from Heroku. err:", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return account.DefaultOrganization.Name, nil
|
|
|
|
}
|
|
|
|
|
2017-02-16 17:05:55 +00:00
|
|
|
// Scopes for heroku is "identity" which grants access to user account
|
|
|
|
// information. This will grant us access to the user's email address which is
|
|
|
|
// used as the Principal's identifier.
|
|
|
|
func (h *Heroku) Scopes() []string {
|
|
|
|
return []string{"identity"}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Secret returns the Heroku application client secret
|
|
|
|
func (h *Heroku) Secret() string {
|
|
|
|
return h.ClientSecret
|
|
|
|
}
|