package sso_test import ( "context" "crypto/rand" "errors" "net/http" "net/url" "testing" "git.zb-server.de/ZB-Server/uptimesebi/internal/sso" "git.zb-server.de/ZB-Server/uptimesebi/internal/sso/ssotest" ) const callback = "https://uptime.example/sso/callback" // authorize follows the IdP redirect and returns the code and state it // would deliver to the callback. func authorize(t *testing.T, authURL string) (code, state string) { t.Helper() noFollow := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }} resp, err := noFollow.Get(authURL) if err != nil { t.Fatalf("GET %s failed: %v", authURL, err) } _ = resp.Body.Close() loc, err := url.Parse(resp.Header.Get("Location")) if err != nil || resp.StatusCode != http.StatusFound { t.Fatalf("authorize = %d %q, want 302 to callback", resp.StatusCode, resp.Header.Get("Location")) } return loc.Query().Get("code"), loc.Query().Get("state") } func setup(t *testing.T) (*ssotest.IDP, *sso.Client, sso.Provider) { t.Helper() idp := ssotest.New(t) c := sso.New(sso.SafeHTTPClient(true), callback, true) p := sso.Provider{Issuer: idp.Issuer(), ClientID: idp.ClientID, ClientSecret: idp.Secret, Scopes: []string{"email"}} return idp, c, p } func TestFlow(t *testing.T) { idp, c, p := setup(t) idp.SetUser(ssotest.User{Subject: "u-1", Email: "a@corp.test", EmailVerified: true, Name: "A"}) ctx := context.Background() flow := sso.NewFlow(rand.Text) authURL, err := c.AuthURL(ctx, p, flow) if err != nil { t.Fatalf("AuthURL() failed: %v", err) } code, state := authorize(t, authURL) if state != flow.State { t.Fatalf("state = %q, want %q", state, flow.State) } got, err := c.Exchange(ctx, p, flow, code) if err != nil { t.Fatalf("Exchange() failed: %v", err) } want := sso.Claims{Subject: "u-1", Email: "a@corp.test", EmailVerified: true, Name: "A"} if got != want { t.Errorf("Exchange() = %+v, want %+v", got, want) } } func TestExchangeRejects(t *testing.T) { tests := []struct { name string tamper func(*ssotest.IDP, *sso.Flow) }{ {"wrong nonce", func(i *ssotest.IDP, _ *sso.Flow) { i.NonceOverride = "other" }}, {"wrong audience", func(i *ssotest.IDP, _ *sso.Flow) { i.AudienceOverride = "someone-else" }}, {"wrong pkce verifier", func(_ *ssotest.IDP, f *sso.Flow) { // Replacing the first character must change it, whatever it is. first := "x" if f.Verifier[0] == 'x' { first = "y" } f.Verifier = first + f.Verifier[1:] }}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { idp, c, p := setup(t) idp.SetUser(ssotest.User{Subject: "u-1"}) ctx := context.Background() flow := sso.NewFlow(rand.Text) authURL, err := c.AuthURL(ctx, p, flow) if err != nil { t.Fatalf("AuthURL() failed: %v", err) } code, _ := authorize(t, authURL) tt.tamper(idp, &flow) if _, err := c.Exchange(ctx, p, flow, code); err == nil { t.Error("Exchange() error = nil, want rejection") } }) } } func TestValidateIssuer(t *testing.T) { idp, _, _ := setup(t) strict := sso.New(sso.SafeHTTPClient(false), callback, false) tests := []struct { issuer string client *sso.Client want error }{ {"ftp://idp.test", strict, sso.ErrInvalidIssuer}, {"https://idp.test/?q=1", strict, sso.ErrInvalidIssuer}, {"https://user@idp.test", strict, sso.ErrInvalidIssuer}, {idp.Issuer(), strict, sso.ErrInvalidIssuer}, {"https://127.0.0.1:1", strict, sso.ErrForbiddenAddress}, {idp.Issuer(), sso.New(sso.SafeHTTPClient(true), callback, true), nil}, } for _, tt := range tests { err := tt.client.ValidateIssuer(context.Background(), tt.issuer) if !errors.Is(err, tt.want) { t.Errorf("ValidateIssuer(%q) = %v, want %v", tt.issuer, err, tt.want) } } }