Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .agents/skills/designing-public-interfaces/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ We must aim for consistency so patterns that already exist in this codebase shou
The interior counterpart of this skill is `designing-internal-boundaries`, which governs how the types behind the exported surface keep their contracts compiler-enforced.

A new or changed convention should also trigger the `recording-decisions` skill.

## When not to add public API

An exported identifier is added only when an SDK user needs to call it directly.
Before writing one, state who outside the module calls it and why.
If the answer is another package of our own, it is not public API.

Reaching for an exported identifier to bridge a gap between two of our own packages means the shared thing belongs in a new internal package, or should be lifted into a common module.
17 changes: 10 additions & 7 deletions auth/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ const ErrMissingStore = authError("session store is required")
// holds nothing.
const ErrNoStoredSession = authError("no stored session")

// ErrSignedOut is reported by every operation on a [Session] that
// [Session.SignOut] has ended, and by every operation on the zero Session.
// ErrSignedOut is reported on a [Session] that [Session.SignOut] has ended,
// and on the zero Session, by every operation but a sign-out, and by a
// sign-out with [signout.WithOthersScope].
const ErrSignedOut = authError("session is signed out")

// ErrSessionExpired is reported by every operation on a [Session] whose
// access token expired after the server had refused to renew it, and by
// [Client.SessionFromStore] when the stored session can no longer be
// refreshed. It is always returned wrapping the server's rejection, an
// [*Error] carrying the code the server answered the refresh with.
// ErrSessionExpired is reported on a [Session] whose access token expired
// after the server had refused to renew it by every operation but a
// sign-out, by a sign-out with [signout.WithOthersScope] whose refresh the
// server rejected and by [Client.SessionFromStore] when the stored session
// can no longer be refreshed. It is always returned wrapping the server's
// rejection, an [*Error] carrying the code the server answered the refresh
// with.
const ErrSessionExpired = authError("session has expired")

// ErrMalformedJWT is reported by [Client.GetClaims] and
Expand Down
20 changes: 15 additions & 5 deletions auth/integrationtest/asproject/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ var definitiveRefreshCodes = []string{"refresh_token_not_found", "session_not_fo
// TestRefreshAfterRevocation is the boundary between the project and a
// signed-in user: the project revokes a session with the secret key and the
// user's Session observes it. A refresh is rejected definitively while the
// unexpired access token still reads, a bare SignOut of a revoked session
// reports success because its goal state is already reached and
// unexpired access token still reads, a bare SignOut of a revoked session is
// refused, its session kept for the local sign-out that then ends it, and
// WithOthersScope surfaces the same answer as the error it is, leaving its
// session live.
func TestRefreshAfterRevocation(t *testing.T) {
Expand All @@ -41,6 +41,10 @@ func TestRefreshAfterRevocation(t *testing.T) {
return result.Session()
}
first, second := signIn(), signIn()
firstAccessToken, err := first.AccessToken(t.Context())
if err != nil {
t.Fatalf("AccessToken of the first session: %v", err)
}
secondAccessToken, err := second.AccessToken(t.Context())
if err != nil {
t.Fatalf("AccessToken of the second session: %v", err)
Expand Down Expand Up @@ -68,11 +72,17 @@ func TestRefreshAfterRevocation(t *testing.T) {
t.Errorf("the rejected session holds (%+v, %q), want the adopted user and refresh token kept", profile, session.RefreshToken())
}

if result, err := first.SignOut(t.Context()); err != nil || result == nil {
t.Errorf("a bare SignOut of a revoked session = (%v, %v), want the empty result", result, err)
if result, err := first.SignOut(t.Context()); result != nil || !errors.As(err, &serverError) || serverError.HTTPStatus != http.StatusForbidden || serverError.Code != "session_not_found" {
t.Errorf("a bare SignOut of a revoked session = (%v, %v), want *auth.Error with HTTP 403 and code session_not_found", result, err)
}
if accessToken, err := first.AccessToken(t.Context()); err != nil || accessToken != firstAccessToken {
t.Errorf("AccessToken() after that SignOut = (%q, %v), want the session kept live", accessToken, err)
}
if result, err := first.SignOut(t.Context(), signout.WithLocalScope()); err != nil || result == nil {
t.Errorf("SignOut with WithLocalScope of a revoked session = (%v, %v), want the empty result", result, err)
}
if accessToken, err := first.AccessToken(t.Context()); accessToken != "" || !errors.Is(err, auth.ErrSignedOut) {
t.Errorf("AccessToken() after that SignOut = (%q, %v), want ErrSignedOut", accessToken, err)
t.Errorf("AccessToken() after the local SignOut = (%q, %v), want ErrSignedOut", accessToken, err)
}
if first.RefreshToken() != "" || first.User() != nil {
t.Error("the signed-out session still holds credentials")
Expand Down
13 changes: 13 additions & 0 deletions auth/internal/scopes/scopes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Package scopes holds the sign-out scope vocabulary: the values the Auth
// server's logout endpoint takes as its scope query parameter.
package scopes

const (
// Global revokes every session the user holds.
Global = "global"
// Local revokes only the session the presented token belongs to.
Local = "local"
// Others revokes every session except the one the presented token
// belongs to.
Others = "others"
)
108 changes: 78 additions & 30 deletions auth/internal/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ const (
)

// EndedError is the error every operation on an ended [Session] reports.
// Status is why the session ended and Cause, for [Expired], the refresh
// failure the server answered with.
// Status is why the session ended and Cause the refresh failure that ended
// it, when one did: always for [Expired], and for [SignedOut] when the
// refresh a sign-out waited on was rejected.
type EndedError struct {
Status Status
Cause error
Expand Down Expand Up @@ -116,11 +117,14 @@ type Session struct {
// flight is one in-flight refresh: done is closed once it has finished, and
// err and abandoned, read only after that, are its failure and whether it was
// abandoned, by its starter giving up or by a panic, in which case its waiters
// start a refresh of their own.
// start a refresh of their own. ending is the status a definitive rejection
// ends the session with, [SignedOut] once any caller waiting on the refresh
// asked for it, and is read and written with the session's mutex held.
type flight struct {
done chan struct{}
err error
abandoned bool
ending Status
}

// write is one store operation captured under the state lock: the document
Expand Down Expand Up @@ -168,10 +172,28 @@ func (s *Session) State() State {
// Token returns an [*EndedError] once the session has ended and ctx's error
// when ctx ends while waiting on a refresh.
func (s *Session) Token(ctx context.Context) (string, error) {
var token string
var err error
token, _, err := s.tokenEnding(ctx, Expired)
return token, s.endedOr(err)
}

// SignOutToken returns the access token for the request that signs the
// session out everywhere, as [Session.Token] does, except in what a
// definitive rejection of an expired access token does: the session ends as
// [SignedOut] rather than [Expired], and the rejection is returned as it is
// rather than as the [*EndedError] it caused. clearErr is the failure of the
// clear that ending caused, when this call performed it. A session that had
// already ended returns its [*EndedError].
func (s *Session) SignOutToken(ctx context.Context) (token string, clearErr, err error) {
return s.tokenEnding(ctx, SignedOut)
}

// tokenEnding is [Session.Token] with ending the status a definitive
// rejection of an expired access token ends the session with, that
// rejection returned as it is, and clearErr the failure of the clear this
// call performed for that ending.
func (s *Session) tokenEnding(ctx context.Context, ending Status) (token string, clearErr, err error) {
var clearing write
var ending, refresh bool
var endingNow, refresh bool
s.locked(func() {
now := s.now()
switch {
Expand All @@ -184,35 +206,56 @@ func (s *Session) Token(ctx context.Context) (string, error) {
case now.Before(s.state.expiresAt):
token = s.state.accessToken
default:
clearing, ending = s.endLocked(Expired, s.rejection), true
err = s.endedErrorLocked()
clearing, endingNow = s.endLocked(ending, s.rejection), true
err = s.rejection
}
})
if ending {
_ = s.perform(ctx, clearing)
if endingNow {
clearErr = s.perform(ctx, clearing)
}
if !refresh {
return token, err
return token, clearErr, err
}

_, err = s.renew(ctx)
writeErr, err := s.renew(ctx, ending)
var ended *EndedError
if errors.As(err, &ended) {
return "", err
return "", nil, err
}
if err != nil && ctx.Err() != nil {
return "", err
return "", writeErr, err
}

s.locked(func() {
switch {
case s.status != Live:
case s.status != Live && (err == nil || !s.definitive(err)):
// Committed and then ended, or failed transiently on a session
// that has ended since.
err = s.endedErrorLocked()
case s.status != Live:
// Rejected, and the session has ended: the rejection stands,
// with the failure of the clear renew performed when the
// rejection ended it.
clearErr = writeErr
case err == nil || s.now().Before(s.state.expiresAt):
token, err = s.state.accessToken, nil
}
})
return token, err
return token, clearErr, err
}

// endedOr returns the [*EndedError] when the session has ended, and err
// otherwise, a nil err included.
func (s *Session) endedOr(err error) error {
if err == nil {
return nil
}
s.locked(func() {
if s.status != Live {
err = s.endedErrorLocked()
}
})
return err
}

// Refresh renews the session now, joining a refresh already in flight. It
Expand All @@ -222,10 +265,10 @@ func (s *Session) Token(ctx context.Context) (string, error) {
// call's definitive rejection ended it. The failure of the clear an ending
// causes is returned to no caller.
func (s *Session) Refresh(ctx context.Context) error {
writeErr, err := s.renew(ctx)
writeErr, err := s.renew(ctx, Expired)
switch {
case err != nil:
return err
return s.endedOr(err)
case writeErr != nil:
return &WriteError{Err: writeErr}
}
Expand All @@ -235,15 +278,18 @@ func (s *Session) Refresh(ctx context.Context) error {
// renew joins the in-flight refresh or starts one with the current refresh
// token, waits for it and reports its outcome: err is nil when renewed state
// was committed, the refresh failure otherwise and an [*EndedError] when the
// session has ended, and writeErr is the failure of a store write this call
// performed. A refresh whose starter's context ended before it succeeded is
// abandoned rather than shared, and its waiters start one of their own, as
// they do when a panic interrupts the refresh or the commit of its result
// before the panic carries on up the starter's stack. The caller that starts
// a refresh commits its result only if the session's generation is unchanged
// when it returns, and it alone writes the store, after releasing the
// waiters.
func (s *Session) renew(ctx context.Context) (writeErr, err error) {
// session had ended before the refresh ran or its result was discarded, and
// writeErr is the failure of a store write this call performed. A refresh
// whose starter's context ended before it succeeded is abandoned rather than
// shared, and its waiters start one of their own, as they do when a panic
// interrupts the refresh or the commit of its result before the panic carries
// on up the starter's stack. The caller that starts a refresh commits its
// result only if the session's generation is unchanged when it returns, and
// it alone writes the store, after releasing the waiters. A definitive
// rejection of an access token that has expired ends the session, with
// status ending or [SignedOut] once any caller waiting on the refresh asked
// for it and the rejection as cause, and is returned as it is.
func (s *Session) renew(ctx context.Context, ending Status) (writeErr, err error) {
var started *flight
var refreshToken string
var generation uint64
Expand All @@ -255,8 +301,11 @@ func (s *Session) renew(ctx context.Context) (writeErr, err error) {
err = s.endedErrorLocked()
case s.inflight != nil:
joined = s.inflight
if ending == SignedOut {
joined.ending = SignedOut
}
default:
started = &flight{done: make(chan struct{})}
started = &flight{done: make(chan struct{}), ending: ending}
s.inflight = started
refreshToken = s.state.refreshToken
generation = s.generation
Expand Down Expand Up @@ -308,8 +357,7 @@ func (s *Session) renew(ctx context.Context) (writeErr, err error) {
s.rejection = refreshErr
started.err = refreshErr
if !s.now().Before(s.state.expiresAt) {
pending, hasPending = s.endLocked(Expired, refreshErr), true
started.err = s.endedErrorLocked()
pending, hasPending = s.endLocked(started.ending, refreshErr), true
}
case ctx.Err() != nil:
started.abandoned = true
Expand Down
Loading
Loading