Enable CLI language-aware editing and reuse OIDC authentication
This commit is contained in:
1 parent
5821254535
commit
93f4ecb8f4
11 files changed
+264
-84
No files matched your search
@@ -164,7 +164,6 @@ func (s *Server) verifyCLIAuth(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
data["CLIUsername"] = principal.Username
|
||||
data["CLICSRF"] = cliConsentCSRF(token, id)
|
||||
data["CLITOTP"] = s.store.HasTOTP(principal.UUID)
|
||||
if r.Method == http.MethodPost {
|
||||
if !validBrowserMutationOrigin(r) || subtle.ConstantTimeCompare([]byte(r.PostForm.Get("csrf")), []byte(cliConsentCSRF(token, id))) != 1 {
|
||||
http.Error(w, "invalid approval origin or token", 403)
|
||||
|
||||
@@ -11,7 +11,7 @@ func init() {
|
||||
"auth.cli_approve": "このCLIを承認する",
|
||||
"auth.cli_deny": "拒否する",
|
||||
"auth.cli_done": "リクエストは処理済みです。ターミナルに戻って結果を確認してください。この画面は閉じられます。",
|
||||
"auth.cli_invalid": "確認コードまたはTOTPが正しくないか、リクエストが失効しました。5回失敗した場合はCLIでログインをやり直してください。",
|
||||
"auth.cli_invalid": "確認コードが正しくないか、リクエストが失効しました。5回失敗した場合はCLIでログインをやり直してください。",
|
||||
},
|
||||
"en": {
|
||||
"auth.cli_title": "Approve terminal access",
|
||||
@@ -22,7 +22,7 @@ func init() {
|
||||
"auth.cli_approve": "Approve this CLI",
|
||||
"auth.cli_deny": "Deny",
|
||||
"auth.cli_done": "This request has been processed. Return to your terminal to check the result. You may close this page.",
|
||||
"auth.cli_invalid": "Incorrect confirmation code or TOTP, or the request expired. After five failed attempts, restart login from the CLI.",
|
||||
"auth.cli_invalid": "Incorrect confirmation code or expired request. After five failed attempts, restart login from the CLI.",
|
||||
},
|
||||
} {
|
||||
for key, value := range entries {
|
||||
|
||||
@@ -28,7 +28,12 @@ func cliForm(s *Server, path string, form url.Values, cookie, origin string) *ht
|
||||
}
|
||||
|
||||
func TestCLIAuthOIDCFlow(t *testing.T) {
|
||||
s, _, domain := configFixture(t)
|
||||
s, api, domain := configFixture(t)
|
||||
principal := s.mustCLIPrincipal(t, domain, api)
|
||||
// An enrolled local factor must not add a second challenge to OIDC consent.
|
||||
if _, err := s.store.DB.Exec(`INSERT INTO auth_credentials(uuid,principal_uuid,kind,identifier,secret_hash,created_at_ms,updated_at_ms,enabled) VALUES('cli-test-totp',?,'totp',?,'synthetic-invalid',1,1,1)`, principal, principal); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p := newOIDCTestProvider(t)
|
||||
previous := oidcVerifier
|
||||
oidcVerifier = p.verifier
|
||||
@@ -93,6 +98,9 @@ func TestCLIAuthOIDCFlow(t *testing.T) {
|
||||
if w.Code != 200 || !strings.Contains(w.Body.String(), `name="user_code"`) || !strings.Contains(w.Body.String(), cliConsentCSRF(browser, id)) {
|
||||
t.Fatalf("consent %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
if strings.Contains(w.Body.String(), `name="totp"`) {
|
||||
t.Fatal("OIDC consent repeated local TOTP")
|
||||
}
|
||||
form := url.Values{"request": {id}, "csrf": {cliConsentCSRF(browser, id)}, "user_code": {grant.Code}, "decision": {"approve"}}
|
||||
if w := cliForm(s, "/_/auth/cli/verify", form, browser, "https://evil.example"); w.Code != 403 {
|
||||
t.Fatal("cross-origin approval accepted")
|
||||
|
||||
@@ -17,7 +17,14 @@ func TestRemoteCLIHTTPIntegration(t *testing.T) {
|
||||
if _, err := exec.LookPath("python3"); err != nil {
|
||||
t.Skip("python3 not installed")
|
||||
}
|
||||
s, _, _, _ := editorFixture(t)
|
||||
s, _, domain, _ := editorFixture(t)
|
||||
object, err := s.store.CreatePageAtPathInDomain(domain, "/ja-only")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.store.SaveRevisionForLanguage(object, "<h1>日本語のページ</h1><p>{{children}}</p>", "", "ja"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.mux.HandleFunc("/_/auth/", s.handleAuth)
|
||||
s.mux.HandleFunc("/v/raw/", s.handleRawView)
|
||||
s.mux.HandleFunc("/_/api/v1/path-picker", s.handlePathPickerAPI)
|
||||
@@ -36,6 +43,39 @@ func TestRemoteCLIHTTPIntegration(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoteCLILanguageForkRequiresWritePermission(t *testing.T) {
|
||||
s, token, domain, object := editorFixture(t)
|
||||
principal := s.mustCLIPrincipal(t, domain, token)
|
||||
if err := s.store.SetPrincipalState(domain, principal, "readonly"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Changing account state revokes old sessions. Use a newly authenticated
|
||||
// readonly session so this exercises write authorization, not expiration.
|
||||
token, err := s.store.CreateAuthSession(domain, principal)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body, err := json.Marshal(map[string]any{
|
||||
"object_uuid": object, "body": "<h1>New language</h1>", "lang": "ja",
|
||||
"content_type": "text/html", "base_revision_uuid": "", "editor_principal_uuid": principal,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r := httptest.NewRequest("POST", "http://localhost/_/api/v1/revisions/save", bytes.NewReader(body))
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
if w.Code != http.StatusForbidden {
|
||||
t.Fatalf("readonly language fork: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
revision, err := s.store.GetRevisionMetadataForExactLanguage(object, "ja")
|
||||
if err != nil || revision.UUID != "" {
|
||||
t.Fatalf("denied language fork persisted: %s %v", revision.UUID, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoteCLIOIDCIntegration(t *testing.T) {
|
||||
if _, err := exec.LookPath("python3"); err != nil {
|
||||
t.Skip("python3 not installed")
|
||||
|
||||
Reference in new issue
Block a user