package http import ( "bytes" "context" "encoding/json" "io" "net/http" "os" "testing" "time" "gitea.federated.computer/Federated_Computer/brain/internal/db" "github.com/golang-jwt/jwt/v5" ) func oidcServiceToken(t *testing.T, typ string, expires time.Time) string { t.Helper() claims := jwt.MapClaims{ "typ": typ, "iss": "superbrain", "aud": "superbrain-api", "iat": time.Now().Add(-time.Minute).Unix(), "exp": expires.Unix(), } tok := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) signed, err := tok.SignedString([]byte(os.Getenv("NEXTAUTH_SECRET"))) if err != nil { t.Fatal(err) } return signed } func postOIDCIdentity(t *testing.T, url, token string, body map[string]any) (int, map[string]any) { t.Helper() raw, _ := json.Marshal(body) req, err := http.NewRequest(http.MethodPost, url+"/api/auth/oidc/identity", bytes.NewReader(raw)) if err != nil { t.Fatal(err) } req.Header.Set("Content-Type", "application/json") if token != "" { req.Header.Set("Authorization", "Bearer "+token) } res, err := http.DefaultClient.Do(req) if err != nil { t.Fatal(err) } defer res.Body.Close() out, _ := io.ReadAll(res.Body) var decoded map[string]any _ = json.Unmarshal(out, &decoded) return res.StatusCode, decoded } func TestOIDCIdentityProvisioning(t *testing.T) { t.Setenv("OIDC_ISSUER", "https://auth.test") t.Setenv("ALLOW_BOOTSTRAP_SIGNUP", "1") srv, database := testServerDB(t) // A browser/session token must never authenticate this endpoint. if status, _ := postOIDCIdentity(t, srv.URL, oidcServiceToken(t, "assertion", time.Now().Add(time.Minute)), map[string]any{ "issuer": "https://auth.test", "subject": "sub-a", "email": "a@example.test", "emailVerified": true, }); status != http.StatusUnauthorized { t.Fatalf("assertion token: wanted 401, got %d", status) } // An expired provisioning token is rejected. if status, _ := postOIDCIdentity(t, srv.URL, oidcServiceToken(t, "oidc-provision", time.Now().Add(-time.Minute)), map[string]any{ "issuer": "https://auth.test", "subject": "sub-a", "email": "a@example.test", "emailVerified": true, }); status != http.StatusUnauthorized { t.Fatalf("expired token: wanted 401, got %d", status) } token := oidcServiceToken(t, "oidc-provision", time.Now().Add(time.Minute)) // Issuer must match the configured value. if status, _ := postOIDCIdentity(t, srv.URL, token, map[string]any{ "issuer": "https://other.test", "subject": "sub-a", "email": "a@example.test", "emailVerified": true, }); status != http.StatusBadRequest { t.Fatalf("unknown issuer: wanted 400, got %d", status) } body := map[string]any{ "issuer": "https://auth.test", "subject": "sub-a", "email": "a@example.test", "emailVerified": true, "name": "Alice", "image": "https://img.test/a.png", } status, created := postOIDCIdentity(t, srv.URL, token, body) if status != http.StatusOK { t.Fatalf("new identity: wanted 200, got %d: %v", status, created) } user := created["user"].(map[string]any) id := user["id"].(string) if id == "" { t.Fatal("new identity returned no local id") } if user["role"] != "ADMIN" { t.Fatalf("first external user should bootstrap as ADMIN, got %v", user["role"]) } if created["tokenVersion"].(float64) < 1 { t.Fatalf("missing tokenVersion: %v", created) } repo := db.NewUserRepo(database) linked, err := repo.FindByAccount(context.Background(), "authelia", "sub-a") if err != nil { t.Fatal(err) } if linked.ID != id || linked.Password != nil || linked.EmailVerified == nil { t.Fatalf("unexpected provisioned user: %+v", linked) } // Returning identity resolves to the same local user, never a duplicate. status, again := postOIDCIdentity(t, srv.URL, token, body) if status != http.StatusOK { t.Fatalf("returning identity: wanted 200, got %d: %v", status, again) } if again["user"].(map[string]any)["id"] != id { t.Fatal("returning identity created a different local user") } // A matching local password account is not auto-linked by email. localID, err := repo.Create(context.Background(), &db.NewUserInput{ ID: db.NewID(), Name: "Local", Email: "password@example.test", Password: "hash", Role: "USER", }) if err != nil { t.Fatal(err) } status, _ = postOIDCIdentity(t, srv.URL, token, map[string]any{ "issuer": "https://auth.test", "subject": "sub-password", "email": "password@example.test", "emailVerified": true, }) if status != http.StatusConflict { t.Fatalf("password account: wanted 409, got %d", status) } // An operator can link that account explicitly; the same subject then // resolves to it without creating a duplicate. if err := repo.LinkExternal(context.Background(), localID, "authelia", "sub-password"); err != nil { t.Fatal(err) } status, linkedAgain := postOIDCIdentity(t, srv.URL, token, map[string]any{ "issuer": "https://auth.test", "subject": "sub-password", "email": "password@example.test", "emailVerified": true, }) if status != http.StatusOK || linkedAgain["user"].(map[string]any)["id"] != localID { t.Fatalf("explicit link: wanted local user %s, got %d %v", localID, status, linkedAgain) } }