2 Commits
Author SHA1 Message Date
terminarandClaude Opus 5.5 376ae3c970 feat(yaiam): web sign-in at any OIDC provider behind another name
Web.PublicIssuer sends the browser to the provider's endpoints on a
public origin while the server keeps Issuer (container networks); the
callback's iss may name either. Client.AllowInsecure accepts plain http
off loopback for test installations on a closed network. email_verified
counts only as the JSON boolean true; a string no longer fails the
sign-in. First consumer: yaemp.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 17:32:32 +01:00
terminarandClaude Opus 5.5 538a6c8ae6 yaiam: user attributes in SignedIn and User (ADR-0044)
Web asks for the scope attributes; SignedIn.Attributes carries the
attributes yaiam released to the application (from UserInfo), and
Admin's User carries a user's attributes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 15:16:32 +01:00
9 changed files with 179 additions and 27 deletions

No files matched your search

+2 -1
View File
@@ -55,7 +55,8 @@ return without the state check. Client secrets (`Web`, `Admin`) go only
in the Authorization header to the token endpoint. Servers check tokens in the Authorization header to the token endpoint. Servers check tokens
with yaiam's `pkg/httpauth`. Never log a token or put one into an error, never follow with yaiam's `pkg/httpauth`. Never log a token or put one into an error, never follow
a redirect with one, never accept an endpoint that is not https (http a redirect with one, never accept an endpoint that is not https (http
only on loopback), and never send a stored token to another issuer or only on loopback, or anywhere with the explicit `AllowInsecure` of a test
installation), and never send a stored token to another issuer or
client than the one it came from. Tests use a fake provider client than the one it came from. Tests use a fake provider
(`httptest`); the end-to-end test against a real yaiam-authd is in (`httptest`); the end-to-end test against a real yaiam-authd is in
yaiam (`internal/oidcprovider/devicelogin_test.go`). yaiam (`internal/oidcprovider/devicelogin_test.go`).
+9
View File
@@ -170,6 +170,15 @@ out, _ := web.LogoutURL(ctx, u.IDToken)
and roles come from UserInfo, which must name the same subject. and roles come from UserInfo, which must name the same subject.
- The application keeps its own session and its own data under - The application keeps its own session and its own data under
`Subject` -- it stays when the user is renamed. `Subject` -- it stays when the user is renamed.
- Any OpenID Connect provider works, not only yaiam: set `Scopes` to
what it offers (e.g. `openid email profile`; the default asks for
yaiam's groups, roles and attributes). `EmailVerified` is true only
for the JSON boolean `true` (some providers send the string).
- `PublicIssuer`: when the application reaches the provider under
another name than the browser does (a container network), the browser
is sent to the endpoints on the public origin; the server keeps
`Issuer`. `AllowInsecure` (on `Client`) accepts plain http off
loopback, for a test installation on a closed network only.
`Admin` reads a tenant's users, groups and group members from yaiam's `Admin` reads a tenant's users, groups and group members from yaiam's
admin API with a service client's token (client credentials, cached admin API with a service client's token (client credentials, cached
+5
View File
@@ -12,6 +12,11 @@
endpoint over TLS (OpenID Connect Core 3.1.3.7), profile and groups endpoint over TLS (OpenID Connect Core 3.1.3.7), profile and groups
from UserInfo; `Admin` for the admin API. The application keeps its from UserInfo; `Admin` for the admin API. The application keeps its
own session. yaiam's `examples/webapp` uses it; yasta and yaais can. own session. yaiam's `examples/webapp` uses it; yasta and yaais can.
- [x] **yaiam: any OpenID Connect provider for a web sign-in**
(2026-11-20, first consumer yaemp, replacing go-oidc/oauth2):
`Web.PublicIssuer` (the browser reaches the provider under another
origin), `Client.AllowInsecure` (http off loopback, test installations
only), `email_verified` true only as JSON boolean.
- [ ] **yaiam: web refresh.** `Web` has no refresh for a confidential - [ ] **yaiam: web refresh.** `Web` has no refresh for a confidential
client (Client.Refresh sends no secret); add when an application keeps client (Client.Refresh sends no secret); add when an application keeps
access tokens beyond the sign-in. access tokens beyond the sign-in.
+4 -1
View File
@@ -70,6 +70,9 @@ type User struct {
Locale string `json:"locale"` Locale string `json:"locale"`
TimeZone string `json:"time_zone"` TimeZone string `json:"time_zone"`
State string `json:"state"` State string `json:"state"`
// Attributes are the user's attributes by name (department,
// cost_center, ...); none with a yaiam before them.
Attributes map[string]string `json:"attributes"`
} }
// Users lists the tenant's users. // Users lists the tenant's users.
@@ -150,7 +153,7 @@ func (a *Admin) token(ctx context.Context) (string, error) {
// get asks the API for path below the tenant and decodes the answer. // get asks the API for path below the tenant and decodes the answer.
// A refused token is fetched again once. // A refused token is fetched again once.
func (a *Admin) get(ctx context.Context, path string, out any) error { func (a *Admin) get(ctx context.Context, path string, out any) error {
if err := checkURL("admin API", a.API); err != nil { if err := checkURL("admin API", a.API, a.AllowInsecure); err != nil {
return err return err
} }
if a.Tenant == "" || strings.ContainsAny(a.Tenant, "/?#") { if a.Tenant == "" || strings.ContainsAny(a.Tenant, "/?#") {
+2 -2
View File
@@ -57,7 +57,7 @@ func newAdminFake(t *testing.T) *adminFake {
case "users": case "users":
fmt.Fprint(w, `{"items":[{"id":"u-1","ref":"alice","username":"alice","display_name":"Alice","user_state":"active","home":true}]}`) fmt.Fprint(w, `{"items":[{"id":"u-1","ref":"alice","username":"alice","display_name":"Alice","user_state":"active","home":true}]}`)
case "users/alice": case "users/alice":
fmt.Fprint(w, `{"id":"u-1","username":"alice","display_name":"Alice","email":"alice@example.org","email_verified":true,"state":"active"}`) fmt.Fprint(w, `{"id":"u-1","username":"alice","display_name":"Alice","email":"alice@example.org","email_verified":true,"state":"active","attributes":{"department":"IT"}}`)
case "groups": case "groups":
fmt.Fprint(w, `{"items":[{"id":"g-1","name":"staff","protected":false}]}`) fmt.Fprint(w, `{"items":[{"id":"g-1","name":"staff","protected":false}]}`)
case "groups/staff/members": case "groups/staff/members":
@@ -88,7 +88,7 @@ func TestAdmin(t *testing.T) {
t.Fatalf("users %+v, %v", users, err) t.Fatalf("users %+v, %v", users, err)
} }
u, err := a.User(ctx, "alice") u, err := a.User(ctx, "alice")
if err != nil || u.Email != "alice@example.org" || !u.EmailVerified { if err != nil || u.Email != "alice@example.org" || !u.EmailVerified || u.Attributes["department"] != "IT" {
t.Errorf("user %+v, %v", u, err) t.Errorf("user %+v, %v", u, err)
} }
groups, err := a.Groups(ctx) groups, err := a.Groups(ctx)
+2 -2
View File
@@ -55,11 +55,11 @@ func (c *Client) StartDevice(ctx context.Context) (*DeviceAuth, error) {
return nil, errors.New("yaiam: device_authorization: incomplete answer") return nil, errors.New("yaiam: device_authorization: incomplete answer")
} }
// The user opens these pages: only ones of the provider's kind. // The user opens these pages: only ones of the provider's kind.
if err := checkURL("verification_uri", out.VerificationURI); err != nil { if err := checkURL("verification_uri", out.VerificationURI, c.AllowInsecure); err != nil {
return nil, err return nil, err
} }
if out.VerificationURIComplete != "" { if out.VerificationURIComplete != "" {
if err := checkURL("verification_uri_complete", out.VerificationURIComplete); err != nil { if err := checkURL("verification_uri_complete", out.VerificationURIComplete, c.AllowInsecure); err != nil {
return nil, err return nil, err
} }
} }
+56 -12
View File
@@ -31,9 +31,11 @@ import (
) )
// WebScopes are asked for when Web.Scopes is nil: who the user is, their // WebScopes are asked for when Web.Scopes is nil: who the user is, their
// address, and their groups and roles at yaiam (the client must be // address, their groups and roles at yaiam (the client must be allowed
// allowed "groups" and "roles"). // "groups" and "roles") and the user attributes released to the client
var WebScopes = []string{"openid", "profile", "email", "groups", "roles"} // (yaiam allows "attributes" once some are). A scope the client is not
// allowed is left out, not refused.
var WebScopes = []string{"openid", "profile", "email", "groups", "roles", "attributes"}
// signInLifetime is how long a started sign-in may take. // signInLifetime is how long a started sign-in may take.
const signInLifetime = 10 * time.Minute const signInLifetime = 10 * time.Minute
@@ -69,6 +71,14 @@ type Web struct {
// PostLogoutRedirectURL is where the provider sends the browser // PostLogoutRedirectURL is where the provider sends the browser
// after a sign-out (registered at the client); "" for none. // after a sign-out (registered at the client); "" for none.
PostLogoutRedirectURL string PostLogoutRedirectURL string
// PublicIssuer is the issuer URL as the browser reaches it, when this
// application reaches the provider under another name (a container
// network: Issuer "http://oidc:8080/default", PublicIssuer
// "http://localhost:8180/default"): the same path on another origin.
// The endpoints the browser is sent to (authorization, end session)
// that are on Issuer's origin go to PublicIssuer's instead, and the
// callback's iss may name either. "" means Issuer.
PublicIssuer string
// CookieName names the short cookie of a running sign-in; "" means // CookieName names the short cookie of a running sign-in; "" means
// "yaiam_signin". On https it always gets the prefix "__Host-". One // "yaiam_signin". On https it always gets the prefix "__Host-". One
// sign-in per browser at a time: a second one started in another tab // sign-in per browser at a time: a second one started in another tab
@@ -88,6 +98,9 @@ type SignedIn struct {
// Groups and Roles with the scopes groups and roles: the names of the // Groups and Roles with the scopes groups and roles: the names of the
// user's groups (subgroups' members included) and roles in the tenant. // user's groups (subgroups' members included) and roles in the tenant.
Groups, Roles []string Groups, Roles []string
// Attributes with the scope attributes: the user's attributes yaiam
// releases to the client (department, cost_center, ...), by name.
Attributes map[string]string
// ACR is how strongly the user signed in ("pwd", "mfa", "phr"), AMR // ACR is how strongly the user signed in ("pwd", "mfa", "phr"), AMR
// with what; AuthTime when. // with what; AuthTime when.
ACR string ACR string
@@ -144,11 +157,11 @@ func (w *Web) scope() string {
// check refuses a Web that cannot sign anyone in safely. // check refuses a Web that cannot sign anyone in safely.
func (w *Web) check() error { func (w *Web) check() error {
if err := checkURL("redirect URL", w.RedirectURL); err != nil { if err := checkURL("redirect URL", w.RedirectURL, w.AllowInsecure); err != nil {
return err return err
} }
if w.PostLogoutRedirectURL != "" { if w.PostLogoutRedirectURL != "" {
if err := checkURL("post-logout redirect URL", w.PostLogoutRedirectURL); err != nil { if err := checkURL("post-logout redirect URL", w.PostLogoutRedirectURL, w.AllowInsecure); err != nil {
return err return err
} }
} }
@@ -156,6 +169,15 @@ func (w *Web) check() error {
if w.Scopes != nil { if w.Scopes != nil {
scopes = w.Scopes scopes = w.Scopes
} }
if w.PublicIssuer != "" {
if err := checkURL("public issuer", w.PublicIssuer, w.AllowInsecure); err != nil {
return err
}
pub, _ := url.Parse(w.PublicIssuer)
if iss, err := url.Parse(w.Issuer); err != nil || iss.Path != pub.Path {
return errors.New("yaiam: the public issuer must have the issuer's path")
}
}
if !slices.Contains(scopes, "openid") { if !slices.Contains(scopes, "openid") {
return errors.New("yaiam: a sign-in needs the scope openid") return errors.New("yaiam: a sign-in needs the scope openid")
} }
@@ -190,7 +212,7 @@ func (w *Web) SignIn(ctx context.Context, rw http.ResponseWriter, r *http.Reques
q := url.Values{"response_type": {"code"}, "client_id": {w.ClientID}, "redirect_uri": {w.RedirectURL}, q := url.Values{"response_type": {"code"}, "client_id": {w.ClientID}, "redirect_uri": {w.RedirectURL},
"scope": {w.scope()}, "state": {c.State}, "nonce": {c.Nonce}, "scope": {w.scope()}, "state": {c.State}, "nonce": {c.Nonce},
"code_challenge": {base64.RawURLEncoding.EncodeToString(challenge[:])}, "code_challenge_method": {"S256"}} "code_challenge": {base64.RawURLEncoding.EncodeToString(challenge[:])}, "code_challenge_method": {"S256"}}
target := m.AuthorizationEndpoint target := w.browser(m.AuthorizationEndpoint)
if strings.Contains(target, "?") { if strings.Contains(target, "?") {
target += "&" + q.Encode() target += "&" + q.Encode()
} else { } else {
@@ -249,7 +271,7 @@ func (w *Web) Callback(ctx context.Context, rw http.ResponseWriter, r *http.Requ
return nil, ErrSignInState return nil, ErrSignInState
} }
// RFC 9207: an iss parameter must name this issuer. // RFC 9207: an iss parameter must name this issuer.
if iss := q.Get("iss"); iss != "" && iss != w.Issuer { if iss := q.Get("iss"); iss != "" && iss != w.Issuer && (w.PublicIssuer == "" || iss != w.PublicIssuer) {
return nil, fmt.Errorf("yaiam: the callback comes from issuer %q, not %q", iss, w.Issuer) return nil, fmt.Errorf("yaiam: the callback comes from issuer %q, not %q", iss, w.Issuer)
} }
if code := q.Get("error"); code != "" { if code := q.Get("error"); code != "" {
@@ -281,7 +303,12 @@ func (w *Web) Callback(ctx context.Context, rw http.ResponseWriter, r *http.Requ
if u.Subject != claims.Subject { if u.Subject != claims.Subject {
return nil, errors.New("yaiam: UserInfo names another user than the ID token") return nil, errors.New("yaiam: UserInfo names another user than the ID token")
} }
out.Username, out.Name, out.Email, out.EmailVerified, out.Groups, out.Roles = u.Username, u.Name, u.Email, u.EmailVerified, u.Groups, u.Roles // Only the JSON boolean true confirms the address: some providers
// send the string "true", which a sign-in must not take for it
// (nor fail on).
out.Username, out.Name, out.Email, out.Groups, out.Roles = u.Username, u.Name, u.Email, u.Groups, u.Roles
out.EmailVerified = string(u.EmailVerified) == "true"
out.Attributes = u.Attributes
} }
return out, nil return out, nil
} }
@@ -389,9 +416,10 @@ func (w *Web) checkIDToken(idToken, nonce string) (*idClaims, error) {
// webUserInfo is UserInfo's answer with what a web sign-in also takes. // webUserInfo is UserInfo's answer with what a web sign-in also takes.
type webUserInfo struct { type webUserInfo struct {
UserInfo UserInfo
EmailVerified bool `json:"email_verified"` EmailVerified json.RawMessage `json:"email_verified"`
Groups []string `json:"groups"` Groups []string `json:"groups"`
Roles []string `json:"roles"` Roles []string `json:"roles"`
Attributes map[string]string `json:"attributes"`
} }
func (w *Web) userInfo(ctx context.Context, m *metadata, t *Token) (*webUserInfo, error) { func (w *Web) userInfo(ctx context.Context, m *metadata, t *Token) (*webUserInfo, error) {
@@ -438,5 +466,21 @@ func (w *Web) LogoutURL(ctx context.Context, idToken string) (string, error) {
if strings.Contains(m.EndSessionEndpoint, "?") { if strings.Contains(m.EndSessionEndpoint, "?") {
sep = "&" sep = "&"
} }
return m.EndSessionEndpoint + sep + q.Encode(), nil return w.browser(m.EndSessionEndpoint) + sep + q.Encode(), nil
}
// browser is endpoint as the browser reaches it: on PublicIssuer's
// origin when it is on Issuer's.
func (w *Web) browser(endpoint string) string {
if w.PublicIssuer == "" {
return endpoint
}
e, err := url.Parse(endpoint)
iss, err2 := url.Parse(w.Issuer)
pub, err3 := url.Parse(w.PublicIssuer)
if err != nil || err2 != nil || err3 != nil || e.Scheme != iss.Scheme || e.Host != iss.Host {
return endpoint
}
e.Scheme, e.Host = pub.Scheme, pub.Host
return e.String()
} }
+87 -3
View File
@@ -71,7 +71,7 @@ func newOP(t *testing.T) *op {
o.ts = httptest.NewServer(mux) o.ts = httptest.NewServer(mux)
t.Cleanup(o.ts.Close) t.Cleanup(o.ts.Close)
o.issuer = o.ts.URL + "/t/default" o.issuer = o.ts.URL + "/t/default"
o.userBody = `{"sub":"u-1","preferred_username":"alice","name":"Alice","email":"alice@example.org","email_verified":true,"groups":["staff","dev"],"roles":["editor"]}` o.userBody = `{"sub":"u-1","preferred_username":"alice","name":"Alice","email":"alice@example.org","email_verified":true,"groups":["staff","dev"],"roles":["editor"],"attributes":{"department":"IT"}}`
o.claims = func(nonce string) map[string]any { o.claims = func(nonce string) map[string]any {
now := time.Now() now := time.Now()
return map[string]any{"iss": o.issuer, "sub": "u-1", "aud": "web", "exp": now.Add(5 * time.Minute).Unix(), "iat": now.Unix(), return map[string]any{"iss": o.issuer, "sub": "u-1", "aud": "web", "exp": now.Add(5 * time.Minute).Unix(), "iat": now.Unix(),
@@ -137,7 +137,7 @@ func TestWebSignIn(t *testing.T) {
w := o.web() w := o.web()
q, cookie := o.start(t, w, "/staff?x=1") q, cookie := o.start(t, w, "/staff?x=1")
if q.Get("response_type") != "code" || q.Get("client_id") != "web" || q.Get("redirect_uri") != w.RedirectURL || if q.Get("response_type") != "code" || q.Get("client_id") != "web" || q.Get("redirect_uri") != w.RedirectURL ||
q.Get("scope") != "openid profile email groups roles" || q.Get("code_challenge_method") != "S256" || q.Get("state") == "" || q.Get("nonce") == "" { q.Get("scope") != "openid profile email groups roles attributes" || q.Get("code_challenge_method") != "S256" || q.Get("state") == "" || q.Get("nonce") == "" {
t.Errorf("authorization request %v", q) t.Errorf("authorization request %v", q)
} }
if !cookie.HttpOnly || cookie.SameSite != http.SameSiteLaxMode || cookie.Name != "yaiam_signin" || cookie.Secure || cookie.MaxAge != 600 { if !cookie.HttpOnly || cookie.SameSite != http.SameSiteLaxMode || cookie.Name != "yaiam_signin" || cookie.Secure || cookie.MaxAge != 600 {
@@ -148,7 +148,7 @@ func TestWebSignIn(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if s.Subject != "u-1" || s.Username != "alice" || s.Name != "Alice" || s.Email != "alice@example.org" || !s.EmailVerified || if s.Subject != "u-1" || s.Username != "alice" || s.Name != "Alice" || s.Email != "alice@example.org" || !s.EmailVerified ||
strings.Join(s.Groups, ",") != "staff,dev" || strings.Join(s.Roles, ",") != "editor" || s.ACR != "mfa" || s.AuthTime.IsZero() || strings.Join(s.Groups, ",") != "staff,dev" || strings.Join(s.Roles, ",") != "editor" || s.Attributes["department"] != "IT" || len(s.Attributes) != 1 || s.ACR != "mfa" || s.AuthTime.IsZero() ||
s.ReturnTo != "/staff?x=1" || s.Token.AccessToken != "at-1" || s.IDToken == "" { s.ReturnTo != "/staff?x=1" || s.Token.AccessToken != "at-1" || s.IDToken == "" {
t.Errorf("signed in %+v", s) t.Errorf("signed in %+v", s)
} }
@@ -327,3 +327,87 @@ func TestDeviceNeedsItsEndpoint(t *testing.T) {
t.Errorf("start device: %v", err) t.Errorf("start device: %v", err)
} }
} }
// TestWebPublicIssuer: with PublicIssuer the browser goes to the
// provider's endpoints on the public origin, the server keeps using
// Issuer, and the callback's iss may name either; a public issuer with
// another path is refused.
func TestWebPublicIssuer(t *testing.T) {
o := newOP(t)
w := o.web()
pubOrigin := strings.Replace(o.ts.URL, "127.0.0.1", "localhost", 1)
w.PublicIssuer = pubOrigin + "/t/default"
rec := httptest.NewRecorder()
if err := w.SignIn(context.Background(), rec, httptest.NewRequest(http.MethodGet, "/login", nil), "/"); err != nil {
t.Fatal(err)
}
loc, _ := url.Parse(rec.Header().Get("Location"))
if loc.Scheme+"://"+loc.Host+loc.Path != pubOrigin+"/authorize" {
t.Fatalf("redirect to %s", loc)
}
o.mu.Lock()
o.nonce = loc.Query().Get("nonce")
o.mu.Unlock()
cookie := rec.Result().Cookies()[0]
cb := url.Values{"code": {"the-code"}, "state": {loc.Query().Get("state")}, "iss": {w.PublicIssuer}}
if s, _, err := callback(w, cb.Encode(), cookie); err != nil || s.Subject != "u-1" {
t.Fatalf("iss = public issuer: %+v, %v", s, err)
}
if u, err := w.LogoutURL(context.Background(), ""); err != nil || !strings.HasPrefix(u, pubOrigin+"/logout?") {
t.Errorf("logout URL %q, %v", u, err)
}
other := o.web()
other.PublicIssuer = pubOrigin + "/t/other"
if err := other.SignIn(context.Background(), httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/login", nil), "/"); err == nil {
t.Error("public issuer with another path taken")
}
}
// TestWebEmailVerified: only the JSON boolean true confirms the address;
// the string "true" or no claim leaves it unconfirmed without failing.
func TestWebEmailVerified(t *testing.T) {
o := newOP(t)
w := o.web()
for body, want := range map[string]bool{
`{"sub":"u-1","email":"a@example.org","email_verified":true}`: true,
`{"sub":"u-1","email":"a@example.org","email_verified":"true"}`: false,
`{"sub":"u-1","email":"a@example.org","email_verified":1}`: false,
`{"sub":"u-1","email":"a@example.org"}`: false,
} {
o.userBody = body
q, c := o.start(t, w, "/")
s, _, err := callback(w, url.Values{"code": {"the-code"}, "state": {q.Get("state")}}.Encode(), c)
if err != nil || s.EmailVerified != want || s.Email != "a@example.org" {
t.Errorf("%s: %+v, %v", body, s, err)
}
}
}
// TestAllowInsecure: plain http off loopback only with AllowInsecure.
func TestAllowInsecure(t *testing.T) {
for _, c := range []struct {
url string
insecure bool
ok bool
}{
{"http://oidc:8080/default", false, false},
{"http://oidc:8080/default", true, true},
{"http://127.0.0.1:8080/default", false, true},
{"https://login.example.org/t/default", false, true},
{"ftp://oidc/default", true, false},
{"/default", true, false},
} {
if err := checkURL("issuer", c.url, c.insecure); (err == nil) != c.ok {
t.Errorf("%s (insecure %v): %v", c.url, c.insecure, err)
}
}
w := &Web{Client: Client{Issuer: "http://oidc:8080/default", ClientID: "web", AllowInsecure: true},
RedirectURL: "http://app:8787/auth/callback"}
if err := w.check(); err != nil {
t.Errorf("insecure web: %v", err)
}
w.AllowInsecure = false
if err := w.check(); err == nil {
t.Error("http redirect URL off loopback taken without AllowInsecure")
}
}
+12 -6
View File
@@ -92,6 +92,12 @@ type Client struct {
// TLS certificates: a web sign-in trusts the ID token because it // TLS certificates: a web sign-in trusts the ID token because it
// comes from the token endpoint over TLS. // comes from the token endpoint over TLS.
HTTPClient *http.Client HTTPClient *http.Client
// AllowInsecure accepts plain http on any host, not only on loopback:
// for a test installation whose provider sits on a closed network
// (e.g. a compose network, "http://oidc:8080/default"). Without TLS a
// web sign-in's ID token is only as trustworthy as that network --
// never set it for a provider reached over the internet.
AllowInsecure bool
mu sync.Mutex mu sync.Mutex
meta *metadata meta *metadata
@@ -136,7 +142,7 @@ func (c *Client) discover(ctx context.Context) (*metadata, error) {
if m != nil { if m != nil {
return m, nil return m, nil
} }
if err := checkURL("issuer", c.Issuer); err != nil { if err := checkURL("issuer", c.Issuer, c.AllowInsecure); err != nil {
return nil, err return nil, err
} }
if c.ClientID == "" { if c.ClientID == "" {
@@ -161,7 +167,7 @@ func (c *Client) discover(ctx context.Context) (*metadata, error) {
if m.Issuer != c.Issuer { if m.Issuer != c.Issuer {
return nil, fmt.Errorf("yaiam: discovery names the issuer %q, not %q", m.Issuer, c.Issuer) return nil, fmt.Errorf("yaiam: discovery names the issuer %q, not %q", m.Issuer, c.Issuer)
} }
if err := checkURL("token_endpoint", m.TokenEndpoint); err != nil { if err := checkURL("token_endpoint", m.TokenEndpoint, c.AllowInsecure); err != nil {
return nil, err return nil, err
} }
// The others only where a flow needs them (device: StartDevice; web: // The others only where a flow needs them (device: StartDevice; web:
@@ -170,7 +176,7 @@ func (c *Client) discover(ctx context.Context) (*metadata, error) {
"device_authorization_endpoint": m.DeviceAuthorizationEndpoint, "authorization_endpoint": m.AuthorizationEndpoint, "device_authorization_endpoint": m.DeviceAuthorizationEndpoint, "authorization_endpoint": m.AuthorizationEndpoint,
"end_session_endpoint": m.EndSessionEndpoint} { "end_session_endpoint": m.EndSessionEndpoint} {
if u != "" { if u != "" {
if err := checkURL(name, u); err != nil { if err := checkURL(name, u, c.AllowInsecure); err != nil {
return nil, err return nil, err
} }
} }
@@ -182,8 +188,8 @@ func (c *Client) discover(ctx context.Context) (*metadata, error) {
} }
// checkURL refuses anything but an absolute https URL, or http on a // checkURL refuses anything but an absolute https URL, or http on a
// loopback host. // loopback host (any host with insecure).
func checkURL(name, s string) error { func checkURL(name, s string, insecure bool) error {
u, err := url.Parse(s) u, err := url.Parse(s)
if err != nil || u.Host == "" { if err != nil || u.Host == "" {
return fmt.Errorf("yaiam: %s %q is not an absolute URL", name, s) return fmt.Errorf("yaiam: %s %q is not an absolute URL", name, s)
@@ -191,7 +197,7 @@ func checkURL(name, s string) error {
switch { switch {
case u.Scheme == "https": case u.Scheme == "https":
return nil return nil
case u.Scheme == "http" && isLoopback(u.Hostname()): case u.Scheme == "http" && (insecure || isLoopback(u.Hostname())):
return nil return nil
} }
return fmt.Errorf("yaiam: %s %q is not https", name, s) return fmt.Errorf("yaiam: %s %q is not https", name, s)