|
|
@@ -12,7 +12,7 @@ import (
|
|
|
"github.com/coreos/go-oidc/v3/oidc"
|
|
|
)
|
|
|
|
|
|
-type ProviderJSON struct {
|
|
|
+type Provider struct {
|
|
|
IssuerURL string `json:"issuer,omitempty"`
|
|
|
AuthURL string `json:"authorization_endpoint,omitempty"`
|
|
|
TokenURL string `json:"token_endpoint,omitempty"`
|
|
|
@@ -21,56 +21,80 @@ type ProviderJSON struct {
|
|
|
Algorithms []string `json:"id_token_signing_alg_values_supported,omitempty"`
|
|
|
}
|
|
|
|
|
|
-type ConfigJSON struct {
|
|
|
- ClientID string `json:"client-id,omitempty"`
|
|
|
- SupportedSigningAlgs []string `json:"support-signing-algs,omitempty"`
|
|
|
+type Config struct {
|
|
|
+ ClientID string `json:"clientId,omitempty"`
|
|
|
+ SignatureAlgorithms []string `json:"signatureAlgorithms,omitempty"`
|
|
|
SkipClientIDCheck bool `json:"-"`
|
|
|
SkipExpiryCheck bool `json:"-"`
|
|
|
SkipIssuerCheck bool `json:"-"`
|
|
|
- Now func() time.Time `json:"-"`
|
|
|
InsecureSkipSignatureCheck bool `json:"-"`
|
|
|
+ Now func() time.Time `json:"-"`
|
|
|
}
|
|
|
|
|
|
type OIDCOptions struct {
|
|
|
- Provider ProviderJSON `json:"provider,omitempty"`
|
|
|
- Config ConfigJSON `json:"config,omitempty"`
|
|
|
+ Provider *Provider `json:"provider,omitempty"`
|
|
|
+ Config *Config `json:"config,omitempty"`
|
|
|
+
|
|
|
+ oidcProviderConfig *oidc.ProviderConfig
|
|
|
+ target *template.Template
|
|
|
}
|
|
|
|
|
|
func (o *OIDCOptions) GetProvider(ctx context.Context) *oidc.Provider {
|
|
|
- if o == nil {
|
|
|
+ if o == nil || o.Provider == nil || o.oidcProviderConfig == nil {
|
|
|
return nil
|
|
|
}
|
|
|
- return toProviderConfig(o.Provider).NewProvider(ctx)
|
|
|
+ return o.oidcProviderConfig.NewProvider(ctx)
|
|
|
}
|
|
|
|
|
|
func (o *OIDCOptions) GetConfig() *oidc.Config {
|
|
|
- if o == nil {
|
|
|
+ if o == nil || o.Config == nil {
|
|
|
return &oidc.Config{}
|
|
|
}
|
|
|
- config := oidc.Config(o.Config)
|
|
|
- return &config
|
|
|
+
|
|
|
+ return &oidc.Config{
|
|
|
+ ClientID: o.Config.ClientID,
|
|
|
+ SupportedSigningAlgs: o.Config.SignatureAlgorithms,
|
|
|
+ SkipClientIDCheck: o.Config.SkipClientIDCheck,
|
|
|
+ SkipExpiryCheck: o.Config.SkipExpiryCheck,
|
|
|
+ SkipIssuerCheck: o.Config.SkipIssuerCheck,
|
|
|
+ Now: o.Config.Now,
|
|
|
+ InsecureSkipSignatureCheck: o.Config.InsecureSkipSignatureCheck,
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-func (o *OIDCOptions) GetTarget(deviceID string) (string, error) {
|
|
|
- if o == nil {
|
|
|
- return "", errors.New("misconfigured target template configuration")
|
|
|
+func (o *OIDCOptions) validateAndInitialize() (err error) {
|
|
|
+ if o.Provider == nil {
|
|
|
+ return errors.New("provider not set")
|
|
|
+ }
|
|
|
+ if o.Provider.IssuerURL == "" {
|
|
|
+ return errors.New("issuer URL must not be empty")
|
|
|
+ }
|
|
|
+
|
|
|
+ o.oidcProviderConfig, err = toOIDCProviderConfig(o.Provider)
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("failed creationg OIDC provider config: %w", err)
|
|
|
}
|
|
|
- targetTemplate := o.Provider.IssuerURL
|
|
|
- tmpl, err := template.New("DeviceId").Parse(targetTemplate)
|
|
|
+
|
|
|
+ o.target, err = template.New("DeviceID").Parse(o.Provider.IssuerURL)
|
|
|
if err != nil {
|
|
|
- return "", fmt.Errorf("failed parsing oidc template: %w", err)
|
|
|
+ return fmt.Errorf("failed parsing OIDC template: %w", err)
|
|
|
}
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func (o *OIDCOptions) EvaluateTarget(deviceID string) (string, error) {
|
|
|
buf := new(bytes.Buffer)
|
|
|
- if err = tmpl.Execute(buf, struct{ DeviceId string }{DeviceId: deviceID}); err != nil { //nolint:revive,stylecheck // TODO(hs): this requires changes in configuration
|
|
|
- return "", fmt.Errorf("failed executing oidc template: %w", err)
|
|
|
+ if err := o.target.Execute(buf, struct{ DeviceID string }{DeviceID: deviceID}); err != nil {
|
|
|
+ return "", fmt.Errorf("failed executing OIDC template: %w", err)
|
|
|
}
|
|
|
return buf.String(), nil
|
|
|
}
|
|
|
|
|
|
-func toProviderConfig(in ProviderJSON) *oidc.ProviderConfig {
|
|
|
+func toOIDCProviderConfig(in *Provider) (*oidc.ProviderConfig, error) {
|
|
|
issuerURL, err := url.Parse(in.IssuerURL)
|
|
|
if err != nil {
|
|
|
- panic(err) // config error, it's ok to panic here
|
|
|
+ return nil, fmt.Errorf("failed parsing issuer URL: %w", err)
|
|
|
}
|
|
|
// Removes query params from the URL because we use it as a way to notify client about the actual OAuth ClientId
|
|
|
// for this provisioner.
|
|
|
@@ -86,5 +110,5 @@ func toProviderConfig(in ProviderJSON) *oidc.ProviderConfig {
|
|
|
UserInfoURL: in.UserInfoURL,
|
|
|
JWKSURL: in.JWKSURL,
|
|
|
Algorithms: in.Algorithms,
|
|
|
- }
|
|
|
+ }, nil
|
|
|
}
|