Phase F follow-ups (ADR-16): - Calendars: events are now stored in calendar_events by a calendars.sync job (every 15 minutes per account, 90 days back and 400 ahead), so journals open at once and keep showing events while a source is down. Google accounts connect with OAuth (calendar.readonly) through DOCWELL_GOOGLE_CLIENT_ID/SECRET; reconnecting renews access. - OneNote: whole notebooks import through Microsoft Graph (Notes.Read, DOCWELL_MICROSOFT_CLIENT_ID/SECRET/TENANT) in the background, with section groups, sections, subpages, pictures and attached files. The Microsoft sign-in lives in memory for an hour and is never stored. - Readwise: an hourly sync with the person's sealed access token; new books get a page under "Readwise", new highlights are appended once. New readwise_syncs collection. - internal/oauth: signed states and one callback route per provider, routed by purpose. directory gains ImportConverted and CallerOf. - Phone app: "Today's journal" on the home screen and calendar events on journal pages. - Web clipper: `npm run package` builds Chrome/Edge, Firefox and source zips; store/ holds listing texts, permission notes and a privacy policy. - OpenAPI, generated clients, translations, compose and .env examples, and user docs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
84 lines
2.9 KiB
Go
84 lines
2.9 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package oauth
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestStates(t *testing.T) {
|
|
st := NewStates("secret")
|
|
raw := st.Sign(State{User: "u1", Purpose: "google-calendar", Data: map[string]string{"x": "1"}})
|
|
s, err := st.Verify(raw, "google-calendar")
|
|
if err != nil || s.User != "u1" || s.Data["x"] != "1" {
|
|
t.Fatalf("verify = %+v, %v", s, err)
|
|
}
|
|
if p, err := st.Peek(raw); err != nil || p.Purpose != "google-calendar" {
|
|
t.Fatalf("peek = %+v, %v", p, err)
|
|
}
|
|
if _, err := st.Verify(raw, "onenote"); !errors.Is(err, ErrBadState) {
|
|
t.Error("state accepted for another purpose")
|
|
}
|
|
if _, err := NewStates("other").Verify(raw, "google-calendar"); !errors.Is(err, ErrBadState) {
|
|
t.Error("state accepted with another secret")
|
|
}
|
|
tampered := "x" + raw
|
|
if _, err := st.Peek(tampered); err == nil {
|
|
t.Error("tampered state accepted")
|
|
}
|
|
}
|
|
|
|
func TestExchangeAndRefresh(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_ = r.ParseForm()
|
|
if r.Form.Get("client_secret") != "s" {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
switch r.Form.Get("grant_type") {
|
|
case "authorization_code":
|
|
if r.Form.Get("code") != "good" || r.Form.Get("redirect_uri") != "https://d/cb" {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
_, _ = w.Write([]byte(`{"error":"invalid_request","error_description":"bad code"}`))
|
|
return
|
|
}
|
|
_, _ = w.Write([]byte(`{"access_token":"a1","refresh_token":"r1","expires_in":3600}`))
|
|
case "refresh_token":
|
|
if r.Form.Get("refresh_token") != "r1" {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
_, _ = w.Write([]byte(`{"error":"invalid_grant"}`))
|
|
return
|
|
}
|
|
_, _ = w.Write([]byte(`{"access_token":"a2","expires_in":3600}`))
|
|
}
|
|
}))
|
|
defer srv.Close()
|
|
p := Provider{Name: "test", ClientID: "c", ClientSecret: "s", AuthURL: "https://auth/x", TokenURL: srv.URL}
|
|
ctx := context.Background()
|
|
tok, err := p.Exchange(ctx, srv.Client(), "good", "https://d/cb")
|
|
if err != nil || tok.AccessToken != "a1" || tok.RefreshToken != "r1" {
|
|
t.Fatalf("exchange = %+v, %v", tok, err)
|
|
}
|
|
if _, err := p.Exchange(ctx, srv.Client(), "bad", "https://d/cb"); err == nil || !strings.Contains(err.Error(), "bad code") {
|
|
t.Fatalf("bad code: %v", err)
|
|
}
|
|
tok, err = p.Refresh(ctx, srv.Client(), "r1", nil)
|
|
if err != nil || tok.AccessToken != "a2" || tok.RefreshToken != "r1" {
|
|
t.Fatalf("refresh = %+v, %v", tok, err)
|
|
}
|
|
if _, err := p.Refresh(ctx, srv.Client(), "gone", nil); !errors.Is(err, ErrRevoked) {
|
|
t.Fatalf("revoked = %v", err)
|
|
}
|
|
u, _ := url.Parse(Google("id", "sec").AuthCodeURL("st", "https://d/cb", []string{"a", "b"}))
|
|
if q := u.Query(); q.Get("scope") != "a b" || q.Get("access_type") != "offline" || q.Get("state") != "st" {
|
|
t.Errorf("auth url = %s", u)
|
|
}
|
|
}
|