2020-10-08 02:18:34 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
2020-10-08 18:28:21 +00:00
|
|
|
"fmt"
|
2020-10-08 02:18:34 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"go.pinniped.dev/internal/constable"
|
|
|
|
)
|
|
|
|
|
2020-12-17 19:34:49 +00:00
|
|
|
// FederationDomainIssuer represents all of the settings and state for a downstream OIDC provider
|
|
|
|
// as defined by a FederationDomain.
|
|
|
|
type FederationDomainIssuer struct {
|
2020-10-08 18:28:21 +00:00
|
|
|
issuer string
|
|
|
|
issuerHost string
|
|
|
|
issuerPath string
|
2020-10-08 02:18:34 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 19:34:49 +00:00
|
|
|
func NewFederationDomainIssuer(issuer string) (*FederationDomainIssuer, error) {
|
|
|
|
p := FederationDomainIssuer{issuer: issuer}
|
2020-10-08 18:28:21 +00:00
|
|
|
err := p.validate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &p, nil
|
|
|
|
}
|
|
|
|
|
2020-12-17 19:34:49 +00:00
|
|
|
func (p *FederationDomainIssuer) validate() error {
|
2020-10-08 18:28:21 +00:00
|
|
|
if p.issuer == "" {
|
2020-12-17 19:34:49 +00:00
|
|
|
return constable.Error("federation domain must have an issuer")
|
2020-10-08 02:18:34 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 18:28:21 +00:00
|
|
|
issuerURL, err := url.Parse(p.issuer)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not parse issuer as URL: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-10-28 19:49:41 +00:00
|
|
|
if issuerURL.Scheme != "https" {
|
2020-10-08 02:18:34 +00:00
|
|
|
return constable.Error(`issuer must have "https" scheme`)
|
|
|
|
}
|
|
|
|
|
2020-10-08 18:28:21 +00:00
|
|
|
if issuerURL.User != nil {
|
2020-10-08 02:18:34 +00:00
|
|
|
return constable.Error(`issuer must not have username or password`)
|
|
|
|
}
|
|
|
|
|
2020-10-08 18:28:21 +00:00
|
|
|
if strings.HasSuffix(issuerURL.Path, "/") {
|
2020-10-08 02:18:34 +00:00
|
|
|
return constable.Error(`issuer must not have trailing slash in path`)
|
|
|
|
}
|
|
|
|
|
2020-10-08 18:28:21 +00:00
|
|
|
if issuerURL.RawQuery != "" {
|
2020-10-08 02:18:34 +00:00
|
|
|
return constable.Error(`issuer must not have query`)
|
|
|
|
}
|
|
|
|
|
2020-10-08 18:28:21 +00:00
|
|
|
if issuerURL.Fragment != "" {
|
2020-10-08 02:18:34 +00:00
|
|
|
return constable.Error(`issuer must not have fragment`)
|
|
|
|
}
|
|
|
|
|
2020-10-08 18:28:21 +00:00
|
|
|
p.issuerHost = issuerURL.Host
|
|
|
|
p.issuerPath = issuerURL.Path
|
|
|
|
|
2020-10-08 02:18:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-17 19:34:49 +00:00
|
|
|
func (p *FederationDomainIssuer) Issuer() string {
|
2020-10-08 18:28:21 +00:00
|
|
|
return p.issuer
|
|
|
|
}
|
|
|
|
|
2020-12-17 19:34:49 +00:00
|
|
|
func (p *FederationDomainIssuer) IssuerHost() string {
|
2020-10-08 18:28:21 +00:00
|
|
|
return p.issuerHost
|
|
|
|
}
|
|
|
|
|
2020-12-17 19:34:49 +00:00
|
|
|
func (p *FederationDomainIssuer) IssuerPath() string {
|
2020-10-08 18:28:21 +00:00
|
|
|
return p.issuerPath
|
|
|
|
}
|