2023-05-08 21:07:38 +00:00
|
|
|
// Copyright 2021-2023 the Pinniped contributors. All Rights Reserved.
|
2021-05-11 17:31:33 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
// Package idpdiscovery provides a handler for the upstream IDP discovery endpoint.
|
|
|
|
package idpdiscovery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"sort"
|
|
|
|
|
2021-08-17 22:23:03 +00:00
|
|
|
"go.pinniped.dev/generated/latest/apis/supervisor/idpdiscovery/v1alpha1"
|
2023-06-22 20:12:50 +00:00
|
|
|
"go.pinniped.dev/internal/oidc/provider/federationdomainproviders"
|
2021-05-11 17:31:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewHandler returns an http.Handler that serves the upstream IDP discovery endpoint.
|
2023-06-22 20:12:50 +00:00
|
|
|
func NewHandler(upstreamIDPs federationdomainproviders.FederationDomainIdentityProvidersListerI) http.Handler {
|
2021-05-11 17:31:33 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
http.Error(w, `Method not allowed (try GET)`, http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
encodedMetadata, encodeErr := responseAsJSON(upstreamIDPs)
|
|
|
|
if encodeErr != nil {
|
|
|
|
http.Error(w, encodeErr.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
if _, err := w.Write(encodedMetadata); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-06-22 20:12:50 +00:00
|
|
|
func responseAsJSON(upstreamIDPs federationdomainproviders.FederationDomainIdentityProvidersListerI) ([]byte, error) {
|
2021-08-18 17:20:33 +00:00
|
|
|
r := v1alpha1.IDPDiscoveryResponse{PinnipedIDPs: []v1alpha1.PinnipedIDP{}}
|
2021-05-11 17:31:33 +00:00
|
|
|
|
|
|
|
// The cache of IDPs could change at any time, so always recalculate the list.
|
2023-05-08 21:07:38 +00:00
|
|
|
for _, federationDomainIdentityProvider := range upstreamIDPs.GetLDAPIdentityProviders() {
|
2021-08-18 17:20:33 +00:00
|
|
|
r.PinnipedIDPs = append(r.PinnipedIDPs, v1alpha1.PinnipedIDP{
|
2023-05-08 21:07:38 +00:00
|
|
|
Name: federationDomainIdentityProvider.DisplayName,
|
2021-08-18 00:50:02 +00:00
|
|
|
Type: v1alpha1.IDPTypeLDAP,
|
2022-04-20 21:58:09 +00:00
|
|
|
Flows: []v1alpha1.IDPFlow{v1alpha1.IDPFlowCLIPassword, v1alpha1.IDPFlowBrowserAuthcode},
|
2021-08-12 17:00:18 +00:00
|
|
|
})
|
2021-05-11 17:31:33 +00:00
|
|
|
}
|
2023-05-08 21:07:38 +00:00
|
|
|
for _, federationDomainIdentityProvider := range upstreamIDPs.GetActiveDirectoryIdentityProviders() {
|
2021-08-24 19:19:29 +00:00
|
|
|
r.PinnipedIDPs = append(r.PinnipedIDPs, v1alpha1.PinnipedIDP{
|
2023-05-08 21:07:38 +00:00
|
|
|
Name: federationDomainIdentityProvider.DisplayName,
|
2021-08-24 19:19:29 +00:00
|
|
|
Type: v1alpha1.IDPTypeActiveDirectory,
|
2022-04-20 21:58:09 +00:00
|
|
|
Flows: []v1alpha1.IDPFlow{v1alpha1.IDPFlowCLIPassword, v1alpha1.IDPFlowBrowserAuthcode},
|
2021-08-24 19:19:29 +00:00
|
|
|
})
|
2021-07-02 22:30:27 +00:00
|
|
|
}
|
2023-05-08 21:07:38 +00:00
|
|
|
for _, federationDomainIdentityProvider := range upstreamIDPs.GetOIDCIdentityProviders() {
|
2021-08-18 00:50:02 +00:00
|
|
|
flows := []v1alpha1.IDPFlow{v1alpha1.IDPFlowBrowserAuthcode}
|
2023-05-08 21:07:38 +00:00
|
|
|
if federationDomainIdentityProvider.Provider.AllowsPasswordGrant() {
|
2021-08-18 00:50:02 +00:00
|
|
|
flows = append(flows, v1alpha1.IDPFlowCLIPassword)
|
2021-08-12 17:00:18 +00:00
|
|
|
}
|
2021-08-18 17:20:33 +00:00
|
|
|
r.PinnipedIDPs = append(r.PinnipedIDPs, v1alpha1.PinnipedIDP{
|
2023-05-08 21:07:38 +00:00
|
|
|
Name: federationDomainIdentityProvider.DisplayName,
|
2021-08-18 00:50:02 +00:00
|
|
|
Type: v1alpha1.IDPTypeOIDC,
|
2021-08-12 17:00:18 +00:00
|
|
|
Flows: flows,
|
|
|
|
})
|
2021-05-11 17:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Nobody like an API that changes the results unnecessarily. :)
|
2021-08-17 22:23:03 +00:00
|
|
|
sort.SliceStable(r.PinnipedIDPs, func(i, j int) bool {
|
|
|
|
return r.PinnipedIDPs[i].Name < r.PinnipedIDPs[j].Name
|
2021-05-11 17:31:33 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
encodeErr := json.NewEncoder(&b).Encode(&r)
|
|
|
|
encodedMetadata := b.Bytes()
|
|
|
|
|
|
|
|
return encodedMetadata, encodeErr
|
|
|
|
}
|