2021-09-11 11:37:31 +00:00
|
|
|
/*
|
2023-07-29 15:46:33 +00:00
|
|
|
Copyright 2020-2023 CAcert Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
https://www.apache.org/licenses/LICENSE-2.0
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
2021-09-11 11:37:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2023-07-29 15:46:33 +00:00
|
|
|
"fmt"
|
2021-09-11 11:37:31 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2023-07-29 15:46:33 +00:00
|
|
|
"time"
|
2021-09-11 11:37:31 +00:00
|
|
|
|
|
|
|
"github.com/lestrrat-go/jwx/jwk"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
|
2023-07-29 15:53:26 +00:00
|
|
|
"code.cacert.org/cacert/oidc-demo-app/internal/models"
|
2021-09-11 11:37:31 +00:00
|
|
|
)
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
// OidcParams defines the parameters for DiscoverOIDC
|
2021-09-11 11:37:31 +00:00
|
|
|
type OidcParams struct {
|
|
|
|
OidcServer string
|
2023-07-29 15:46:33 +00:00
|
|
|
OidcClientID string
|
2021-09-11 11:37:31 +00:00
|
|
|
OidcClientSecret string
|
|
|
|
APIClient *http.Client
|
|
|
|
}
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
type OIDCInformation struct {
|
|
|
|
KeySet jwk.Set
|
|
|
|
OAuth2Config *oauth2.Config
|
|
|
|
OIDCConfiguration *models.OpenIDConfiguration
|
|
|
|
}
|
|
|
|
|
|
|
|
// DiscoverOIDC gets OpenID Connect parameters from the discovery endpoint and the
|
2021-09-11 11:37:31 +00:00
|
|
|
// JSON Web Key Set from the discovered jwksUri.
|
|
|
|
//
|
|
|
|
// The subset of values specified by models.OpenIDConfiguration is stored in
|
|
|
|
// the given context and can be retrieved from the context by GetOidcConfig.
|
|
|
|
//
|
|
|
|
// OAuth2 specific values are stored in another context object and can be
|
|
|
|
// retrieved by GetOAuth2Config.
|
|
|
|
//
|
|
|
|
// The JSON Web Key Set can be retrieved by GetJwkSet.
|
2023-07-29 15:46:33 +00:00
|
|
|
func DiscoverOIDC(logger *log.Logger, params *OidcParams) (*OIDCInformation, error) {
|
|
|
|
discoveryURL, err := url.Parse(params.OidcServer)
|
2021-09-11 11:37:31 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf("could not parse oidc.server parameter value %s: %s", params.OidcServer, err)
|
|
|
|
} else {
|
2023-07-29 15:46:33 +00:00
|
|
|
discoveryURL.Path = "/.well-known/openid-configuration"
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
var (
|
|
|
|
body []byte
|
|
|
|
req *http.Request
|
|
|
|
)
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodGet, discoveryURL.String(), bytes.NewBuffer(body))
|
2021-09-11 11:37:31 +00:00
|
|
|
if err != nil {
|
2023-07-29 15:46:33 +00:00
|
|
|
return nil, fmt.Errorf("could not create OIDC discovery request: %w", err)
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
req.Header = map[string][]string{
|
|
|
|
"Accept": {"application/json"},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := params.APIClient.Do(req)
|
|
|
|
if err != nil {
|
2023-07-29 15:46:33 +00:00
|
|
|
return nil, fmt.Errorf("call to OIDC discovery endpoint failed: %w", err)
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
dec := json.NewDecoder(resp.Body)
|
|
|
|
discoveryResponse := &models.OpenIDConfiguration{}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
err = dec.Decode(discoveryResponse)
|
|
|
|
if err != nil {
|
2023-07-29 15:46:33 +00:00
|
|
|
return nil, fmt.Errorf("could not decode OIDC discovery response: %w", err)
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
oauth2Config := &oauth2.Config{
|
2023-07-29 15:46:33 +00:00
|
|
|
ClientID: params.OidcClientID,
|
2021-09-11 11:37:31 +00:00
|
|
|
ClientSecret: params.OidcClientSecret,
|
|
|
|
Endpoint: oauth2.Endpoint{
|
|
|
|
AuthURL: discoveryResponse.AuthorizationEndpoint,
|
|
|
|
TokenURL: discoveryResponse.TokenEndpoint,
|
|
|
|
},
|
|
|
|
Scopes: []string{"openid", "offline"},
|
|
|
|
}
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
const jwkFetchTimeout = 10 * time.Second
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), jwkFetchTimeout)
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
defer cancel()
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
keySet, err := jwk.Fetch(ctx, discoveryResponse.JwksURI, jwk.WithHTTPClient(params.APIClient))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not fetch JWKs: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &OIDCInformation{
|
|
|
|
KeySet: keySet,
|
|
|
|
OAuth2Config: oauth2Config,
|
|
|
|
OIDCConfiguration: discoveryResponse,
|
|
|
|
}, nil
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|