2019-06-05 18:59:04 +02:00
|
|
|
package providers
|
|
|
|
|
|
|
|
import (
|
2020-05-05 17:53:33 +02:00
|
|
|
"context"
|
2019-06-05 18:59:04 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2020-03-29 15:54:36 +02:00
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/pkg/apis/sessions"
|
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/pkg/requests"
|
2019-06-05 18:59:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// NextcloudProvider represents an Nextcloud based Identity Provider
|
|
|
|
type NextcloudProvider struct {
|
|
|
|
*ProviderData
|
|
|
|
}
|
|
|
|
|
2020-05-05 17:53:33 +02:00
|
|
|
var _ Provider = (*NextcloudProvider)(nil)
|
|
|
|
|
2019-06-05 18:59:04 +02:00
|
|
|
// NewNextcloudProvider initiates a new NextcloudProvider
|
|
|
|
func NewNextcloudProvider(p *ProviderData) *NextcloudProvider {
|
|
|
|
p.ProviderName = "Nextcloud"
|
|
|
|
return &NextcloudProvider{ProviderData: p}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNextcloudHeader(accessToken string) http.Header {
|
|
|
|
header := make(http.Header)
|
|
|
|
header.Set("Authorization", fmt.Sprintf("Bearer %s", accessToken))
|
|
|
|
return header
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEmailAddress returns the Account email address
|
2020-05-05 17:53:33 +02:00
|
|
|
func (p *NextcloudProvider) GetEmailAddress(ctx context.Context, s *sessions.SessionState) (string, error) {
|
2020-07-03 20:27:25 +02:00
|
|
|
json, err := requests.New(p.ValidateURL.String()).
|
|
|
|
WithContext(ctx).
|
|
|
|
WithHeaders(getNextcloudHeader(s.AccessToken)).
|
2020-07-06 18:42:26 +02:00
|
|
|
Do().
|
2020-07-03 20:27:25 +02:00
|
|
|
UnmarshalJSON()
|
2019-06-05 18:59:04 +02:00
|
|
|
if err != nil {
|
2020-07-03 20:27:25 +02:00
|
|
|
return "", fmt.Errorf("error making request: %v", err)
|
2019-06-05 18:59:04 +02:00
|
|
|
}
|
2020-07-03 20:27:25 +02:00
|
|
|
|
2019-06-05 18:59:04 +02:00
|
|
|
email, err := json.Get("ocs").Get("data").Get("email").String()
|
|
|
|
return email, err
|
|
|
|
}
|