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>
This commit is contained in:
1 parent
538a6c8ae6
commit
376ae3c970
8 files changed
+159
-17
No files matched your search
+44
-7
@@ -71,6 +71,14 @@ type Web struct {
|
||||
// PostLogoutRedirectURL is where the provider sends the browser
|
||||
// after a sign-out (registered at the client); "" for none.
|
||||
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
|
||||
// "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
|
||||
@@ -149,11 +157,11 @@ func (w *Web) scope() string {
|
||||
|
||||
// check refuses a Web that cannot sign anyone in safely.
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -161,6 +169,15 @@ func (w *Web) check() error {
|
||||
if w.Scopes != nil {
|
||||
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") {
|
||||
return errors.New("yaiam: a sign-in needs the scope openid")
|
||||
}
|
||||
@@ -195,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},
|
||||
"scope": {w.scope()}, "state": {c.State}, "nonce": {c.Nonce},
|
||||
"code_challenge": {base64.RawURLEncoding.EncodeToString(challenge[:])}, "code_challenge_method": {"S256"}}
|
||||
target := m.AuthorizationEndpoint
|
||||
target := w.browser(m.AuthorizationEndpoint)
|
||||
if strings.Contains(target, "?") {
|
||||
target += "&" + q.Encode()
|
||||
} else {
|
||||
@@ -254,7 +271,7 @@ func (w *Web) Callback(ctx context.Context, rw http.ResponseWriter, r *http.Requ
|
||||
return nil, ErrSignInState
|
||||
}
|
||||
// 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)
|
||||
}
|
||||
if code := q.Get("error"); code != "" {
|
||||
@@ -286,7 +303,11 @@ func (w *Web) Callback(ctx context.Context, rw http.ResponseWriter, r *http.Requ
|
||||
if u.Subject != claims.Subject {
|
||||
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
|
||||
@@ -395,7 +416,7 @@ func (w *Web) checkIDToken(idToken, nonce string) (*idClaims, error) {
|
||||
// webUserInfo is UserInfo's answer with what a web sign-in also takes.
|
||||
type webUserInfo struct {
|
||||
UserInfo
|
||||
EmailVerified bool `json:"email_verified"`
|
||||
EmailVerified json.RawMessage `json:"email_verified"`
|
||||
Groups []string `json:"groups"`
|
||||
Roles []string `json:"roles"`
|
||||
Attributes map[string]string `json:"attributes"`
|
||||
@@ -445,5 +466,21 @@ func (w *Web) LogoutURL(ctx context.Context, idToken string) (string, error) {
|
||||
if strings.Contains(m.EndSessionEndpoint, "?") {
|
||||
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()
|
||||
}
|
||||
Reference in new issue
Block a user