1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2024-11-30 09:16:52 +02:00
oauth2-proxy/providers/nextcloud.go

45 lines
1.2 KiB
Go
Raw Normal View History

2019-06-05 18:59:04 +02:00
package providers
import (
"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
}
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
func (p *NextcloudProvider) GetEmailAddress(ctx context.Context, s *sessions.SessionState) (string, error) {
json, err := requests.New(p.ValidateURL.String()).
WithContext(ctx).
WithHeaders(getNextcloudHeader(s.AccessToken)).
2020-07-06 18:42:26 +02:00
Do().
UnmarshalJSON()
2019-06-05 18:59:04 +02:00
if err != nil {
return "", fmt.Errorf("error making request: %v", err)
2019-06-05 18:59:04 +02:00
}
2019-06-05 18:59:04 +02:00
email, err := json.Get("ocs").Get("data").Get("email").String()
return email, err
}