Admins make the client's secret under Settings → Sign-in, which shows the entry for the Core's idproviders.yml (the secret's PBKDF2-SHA512 hash, the redirect address, PKCE). People sign in with their Core account: invited ones are matched by verified email, then by subject; optionally anyone at the Core's domain joins. Passwords can be turned off but for the owner's, and signing out ends the Core's session where it can. On multi-tenant, the team is found by its proved domain. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
132 lines
4.7 KiB
Go
132 lines
4.7 KiB
Go
package oidc
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestHashSecret(t *testing.T) {
|
|
// The same as Python's hashlib.pbkdf2_hmac, in passlib's base64.
|
|
if got := hashWith("correct horse", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, 1000); got != "$pbkdf2-sha512$1000$AAECAwQFBgcICQoLDA0ODw$Xpx07WjVx4vCIvrmBRj8uOoVVtGqJqtUv2J5bhizSQs7osCteF7W4A61dZDqSIqQjO.dxO6p5FT/Uy7QRBXSXA" {
|
|
t.Errorf("hash %s", got)
|
|
}
|
|
h, err := HashSecret("s3cret")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Shaped like the Core's own clients' secrets.
|
|
if !regexp.MustCompile(`^\$pbkdf2-sha512\$310000\$[A-Za-z0-9./]{22}\$[A-Za-z0-9./]{86}$`).MatchString(h) {
|
|
t.Errorf("shape %s", h)
|
|
}
|
|
if !CheckHash("s3cret", h) || CheckHash("s3creT", h) || CheckHash("s3cret", "$pbkdf2-sha256$1$a$b") {
|
|
t.Error("check")
|
|
}
|
|
if s, _ := NewSecret(); len(s) != 48 {
|
|
t.Errorf("secret %q", s)
|
|
}
|
|
}
|
|
|
|
// fakeAuthelia is a provider for tests.
|
|
func fakeAuthelia(t *testing.T, claims map[string]any) (*httptest.Server, *string) {
|
|
t.Helper()
|
|
var challenge string
|
|
var srv *httptest.Server
|
|
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.URL.Path {
|
|
case "/.well-known/openid-configuration":
|
|
json.NewEncoder(w).Encode(map[string]any{"issuer": srv.URL, "authorization_endpoint": srv.URL + "/api/oidc/authorization",
|
|
"token_endpoint": srv.URL + "/api/oidc/token", "userinfo_endpoint": srv.URL + "/api/oidc/userinfo"})
|
|
case "/api/oidc/token":
|
|
r.ParseForm()
|
|
sum := sha256.Sum256([]byte(r.PostForm.Get("code_verifier")))
|
|
if r.PostForm.Get("client_secret") != "s3cret" || r.PostForm.Get("code") != "c0de" ||
|
|
base64.RawURLEncoding.EncodeToString(sum[:]) != challenge {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
json.NewEncoder(w).Encode(map[string]string{"error": "invalid_grant"})
|
|
return
|
|
}
|
|
c := map[string]any{"iss": srv.URL, "aud": []string{"supermarket"}, "exp": time.Now().Add(time.Minute).Unix(), "iat": time.Now().Unix()}
|
|
for k, v := range claims {
|
|
c[k] = v
|
|
}
|
|
payload, _ := json.Marshal(c)
|
|
json.NewEncoder(w).Encode(map[string]string{"access_token": "at", "id_token": "e30." + base64.RawURLEncoding.EncodeToString(payload) + ".sig"})
|
|
case "/api/oidc/userinfo":
|
|
if r.Header.Get("Authorization") != "Bearer at" {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
json.NewEncoder(w).Encode(map[string]any{"sub": claims["sub"], "email": "Pat@Example.com", "email_verified": true, "groups": []string{"admins"}})
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
return srv, &challenge
|
|
}
|
|
|
|
func TestCodeFlow(t *testing.T) {
|
|
ctx := context.Background()
|
|
claims := map[string]any{"sub": "abc-123", "name": "Pat"}
|
|
srv, challenge := fakeAuthelia(t, claims)
|
|
if _, err := Discover(ctx, srv.URL+"/other"); err == nil {
|
|
t.Error("discovered a wrong issuer")
|
|
}
|
|
p, err := Discover(ctx, srv.URL+"/")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c := Client{ID: "supermarket", Secret: "s3cret", RedirectURI: "https://sm.example.com/auth/core/callback"}
|
|
addr, f, err := Start(p, c, "7.")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
u, _ := url.Parse(addr)
|
|
q := u.Query()
|
|
if q.Get("code_challenge_method") != "S256" || q.Get("scope") != Scopes || q.Get("nonce") != f.Nonce || !strings.HasPrefix(f.State, "7.") || q.Get("state") != f.State {
|
|
t.Errorf("authorization address %s", addr)
|
|
}
|
|
*challenge = q.Get("code_challenge")
|
|
|
|
claims["nonce"] = f.Nonce
|
|
id, err := Finish(ctx, p, c, f, "c0de", time.Now())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if id.Subject != "abc-123" || id.Email != "pat@example.com" || !id.Verified || id.Name != "Pat" || len(id.Groups) != 1 {
|
|
t.Errorf("identity %+v", id)
|
|
}
|
|
// What must fail.
|
|
if _, err := Finish(ctx, p, Client{ID: "supermarket", Secret: "wrong", RedirectURI: c.RedirectURI}, f, "c0de", time.Now()); err == nil {
|
|
t.Error("a wrong secret")
|
|
}
|
|
bad := *f
|
|
bad.Verifier = "other"
|
|
if _, err := Finish(ctx, p, c, &bad, "c0de", time.Now()); err == nil {
|
|
t.Error("a wrong verifier")
|
|
}
|
|
claims["nonce"] = "replayed"
|
|
if _, err := Finish(ctx, p, c, f, "c0de", time.Now()); err == nil || !strings.Contains(err.Error(), "match") {
|
|
t.Errorf("a wrong nonce: %v", err)
|
|
}
|
|
claims["nonce"] = f.Nonce
|
|
if _, err := Finish(ctx, p, Client{ID: "other", Secret: "s3cret", RedirectURI: c.RedirectURI}, f, "c0de", time.Now()); err == nil {
|
|
t.Error("another client's token")
|
|
}
|
|
if _, err := Finish(ctx, p, c, f, "c0de", time.Now().Add(10*time.Minute)); err == nil || !strings.Contains(err.Error(), "expired") {
|
|
t.Errorf("expired: %v", err)
|
|
}
|
|
if SignOutURL(p, "x", "https://sm.example.com/signin") != "" {
|
|
t.Error("sign-out without an endpoint")
|
|
}
|
|
}
|