Files
terminarandClaude Opus 5.5 c2a5e3928a feat(yaiam): refresh and revoke a web sign-in's tokens
Web.Refresh trades the refresh token from an offline_access sign-in for
the next access token, authenticating a confidential client with its
secret (Client.Refresh only sends client_id). Web.Revoke ends the refresh
and access token at sign-out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 00:03:17 +01:00

86 lines
3.0 KiB
Go

package yaiam
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"sync"
"testing"
"time"
)
// TestWebRefreshAndRevoke: a confidential client refreshes and revokes
// with its secret in a Basic header and no client_id in the form; a
// refused refresh token is ErrLoginRequired.
func TestWebRefreshAndRevoke(t *testing.T) {
var mu sync.Mutex
var forms []url.Values
var auths []string
var ts *httptest.Server
mux := http.NewServeMux()
mux.HandleFunc("GET /t/default/.well-known/openid-configuration", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]string{"issuer": ts.URL + "/t/default", "token_endpoint": ts.URL + "/token",
"revocation_endpoint": ts.URL + "/revoke"})
})
record := func(r *http.Request) {
r.ParseForm()
mu.Lock()
forms = append(forms, r.PostForm)
auths = append(auths, r.Header.Get("Authorization"))
mu.Unlock()
}
mux.HandleFunc("POST /token", func(w http.ResponseWriter, r *http.Request) {
record(r)
if r.PostForm.Get("refresh_token") != "rt-1" {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, `{"error":"invalid_grant"}`)
return
}
fmt.Fprint(w, `{"access_token":"at-2","token_type":"Bearer","expires_in":300,"refresh_token":"rt-2","scope":"openid offline_access"}`)
})
mux.HandleFunc("POST /revoke", func(w http.ResponseWriter, r *http.Request) { record(r) })
ts = httptest.NewServer(mux)
defer ts.Close()
w := &Web{Client: Client{Issuer: ts.URL + "/t/default", ClientID: "web"}, ClientSecret: "s3cret", RedirectURL: "http://localhost/cb"}
nt, err := w.Refresh(context.Background(), &Token{AccessToken: "at-1", RefreshToken: "rt-1"})
if err != nil {
t.Fatal(err)
}
if nt.AccessToken != "at-2" || nt.RefreshToken != "rt-2" || nt.ClientID != "web" || time.Until(nt.Expiry) < 4*time.Minute {
t.Fatalf("token %+v", nt)
}
if f := forms[0]; f.Get("grant_type") != "refresh_token" || f.Has("client_id") || auths[0] != "Basic d2ViOnMzY3JldA==" {
t.Fatalf("form %v auth %q", f, auths[0])
}
if _, err := w.Refresh(context.Background(), &Token{RefreshToken: "stale"}); !errors.Is(err, ErrLoginRequired) {
t.Fatalf("stale: %v", err)
}
if _, err := w.Refresh(context.Background(), &Token{AccessToken: "at"}); !errors.Is(err, ErrLoginRequired) {
t.Fatalf("no refresh token: %v", err)
}
forms, auths = nil, nil
if err := w.Revoke(context.Background(), nt); err != nil {
t.Fatal(err)
}
if len(forms) != 2 || forms[0].Get("token") != "rt-2" || forms[0].Get("token_type_hint") != "refresh_token" ||
forms[1].Get("token") != "at-2" || auths[1] != "Basic d2ViOnMzY3JldA==" {
t.Fatalf("revoke forms %v", forms)
}
// A public client sends client_id instead.
pub := &Web{Client: Client{Issuer: ts.URL + "/t/default", ClientID: "web"}, RedirectURL: "http://localhost/cb"}
forms, auths = nil, nil
if _, err := pub.Refresh(context.Background(), &Token{RefreshToken: "rt-1"}); err != nil {
t.Fatal(err)
}
if forms[0].Get("client_id") != "web" || auths[0] != "" {
t.Fatalf("public: %v %q", forms[0], auths[0])
}
}