2020-01-15 13:09:34 +02:00
|
|
|
package providers
|
|
|
|
|
|
|
|
import (
|
2020-05-05 17:53:33 +02:00
|
|
|
"context"
|
2020-01-15 13:09:34 +02:00
|
|
|
"errors"
|
|
|
|
"net/url"
|
|
|
|
|
2020-09-29 18:44:42 +02:00
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
|
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/requests"
|
2020-01-15 13:09:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// DigitalOceanProvider represents a DigitalOcean based Identity Provider
|
|
|
|
type DigitalOceanProvider struct {
|
|
|
|
*ProviderData
|
|
|
|
}
|
|
|
|
|
2020-05-05 17:53:33 +02:00
|
|
|
var _ Provider = (*DigitalOceanProvider)(nil)
|
|
|
|
|
2020-05-25 14:08:04 +02:00
|
|
|
const (
|
|
|
|
digitalOceanProviderName = "DigitalOcean"
|
|
|
|
digitalOceanDefaultScope = "read"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Default Login URL for DigitalOcean.
|
|
|
|
// Pre-parsed URL of https://cloud.digitalocean.com/v1/oauth/authorize.
|
|
|
|
digitalOceanDefaultLoginURL = &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "cloud.digitalocean.com",
|
|
|
|
Path: "/v1/oauth/authorize",
|
2020-01-15 13:09:34 +02:00
|
|
|
}
|
2020-05-25 14:08:04 +02:00
|
|
|
|
|
|
|
// Default Redeem URL for DigitalOcean.
|
|
|
|
// Pre-parsed URL of https://cloud.digitalocean.com/v1/oauth/token.
|
|
|
|
digitalOceanDefaultRedeemURL = &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "cloud.digitalocean.com",
|
|
|
|
Path: "/v1/oauth/token",
|
2020-01-15 13:09:34 +02:00
|
|
|
}
|
2020-05-25 14:08:04 +02:00
|
|
|
|
|
|
|
// Default Profile URL for DigitalOcean.
|
|
|
|
// Pre-parsed URL of https://cloud.digitalocean.com/v2/account.
|
|
|
|
digitalOceanDefaultProfileURL = &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "api.digitalocean.com",
|
|
|
|
Path: "/v2/account",
|
2020-01-15 13:09:34 +02:00
|
|
|
}
|
2020-05-25 14:08:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewDigitalOceanProvider initiates a new DigitalOceanProvider
|
|
|
|
func NewDigitalOceanProvider(p *ProviderData) *DigitalOceanProvider {
|
|
|
|
p.setProviderDefaults(providerDefaults{
|
|
|
|
name: digitalOceanProviderName,
|
|
|
|
loginURL: digitalOceanDefaultLoginURL,
|
|
|
|
redeemURL: digitalOceanDefaultRedeemURL,
|
|
|
|
profileURL: digitalOceanDefaultProfileURL,
|
|
|
|
validateURL: digitalOceanDefaultProfileURL,
|
|
|
|
scope: digitalOceanDefaultScope,
|
|
|
|
})
|
2020-01-15 13:09:34 +02:00
|
|
|
return &DigitalOceanProvider{ProviderData: p}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEmailAddress returns the Account email address
|
2020-05-05 17:53:33 +02:00
|
|
|
func (p *DigitalOceanProvider) GetEmailAddress(ctx context.Context, s *sessions.SessionState) (string, error) {
|
2020-01-15 13:09:34 +02:00
|
|
|
if s.AccessToken == "" {
|
|
|
|
return "", errors.New("missing access token")
|
|
|
|
}
|
|
|
|
|
2020-07-03 20:27:25 +02:00
|
|
|
json, err := requests.New(p.ProfileURL.String()).
|
|
|
|
WithContext(ctx).
|
2020-05-17 17:34:09 +02:00
|
|
|
WithHeaders(makeOIDCHeader(s.AccessToken)).
|
2020-07-06 18:42:26 +02:00
|
|
|
Do().
|
2020-07-03 20:27:25 +02:00
|
|
|
UnmarshalJSON()
|
2020-01-15 13:09:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
email, err := json.GetPath("account", "email").String()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return email, nil
|
|
|
|
}
|
|
|
|
|
2020-11-30 00:12:48 +02:00
|
|
|
// ValidateSession validates the AccessToken
|
2020-10-24 07:06:50 +02:00
|
|
|
func (p *DigitalOceanProvider) ValidateSession(ctx context.Context, s *sessions.SessionState) bool {
|
2020-05-17 17:34:09 +02:00
|
|
|
return validateToken(ctx, p, s.AccessToken, makeOIDCHeader(s.AccessToken))
|
2020-01-15 13:09:34 +02:00
|
|
|
}
|