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
@@ -327,3 +327,87 @@ func TestDeviceNeedsItsEndpoint(t *testing.T) {
|
||||
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")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user