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>
This commit is contained in:
1 parent
560a1ead8a
commit
c2a5e3928a
3 files changed
+185
-1
No files matched your search
@@ -0,0 +1,94 @@
|
||||
package yaiam
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Refresh gets a new access token for a signed-in user with t's refresh
|
||||
// token (there is one only when the scopes asked for offline_access).
|
||||
// Unlike Client.Refresh it authenticates a confidential client with its
|
||||
// secret. The answer's refresh token replaces t's; keep the result, not
|
||||
// t. A refused refresh token is ErrLoginRequired.
|
||||
func (w *Web) Refresh(ctx context.Context, t *Token) (*Token, error) {
|
||||
if t == nil || t.RefreshToken == "" {
|
||||
return nil, ErrLoginRequired
|
||||
}
|
||||
m, err := w.discover(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var out struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresIn int64 `json:"expires_in"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
Scope string `json:"scope"`
|
||||
}
|
||||
err = w.postClient(ctx, "token", m.TokenEndpoint, url.Values{"grant_type": {"refresh_token"}, "refresh_token": {t.RefreshToken}}, &out)
|
||||
if isCode(err, "invalid_grant") {
|
||||
return nil, fmt.Errorf("%w (%w)", ErrLoginRequired, err)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if out.AccessToken == "" || !strings.EqualFold(out.TokenType, "Bearer") || out.ExpiresIn <= 0 {
|
||||
return nil, errors.New("yaiam: token: no bearer token with a lifetime in the answer")
|
||||
}
|
||||
nt := &Token{Issuer: w.Issuer, ClientID: w.ClientID, AccessToken: out.AccessToken, RefreshToken: out.RefreshToken,
|
||||
Expiry: time.Now().Add(time.Duration(out.ExpiresIn) * time.Second), Scope: out.Scope}
|
||||
if nt.RefreshToken == "" {
|
||||
nt.RefreshToken = t.RefreshToken
|
||||
}
|
||||
return nt, nil
|
||||
}
|
||||
|
||||
// Revoke ends a signed-in user's tokens at the provider (RFC 7009): the
|
||||
// refresh token first, which ends the access tokens it made too, then
|
||||
// the access token. For a sign-out; the browser's session at the
|
||||
// provider ends only through LogoutURL.
|
||||
func (w *Web) Revoke(ctx context.Context, t *Token) error {
|
||||
if t == nil || (t.RefreshToken == "" && t.AccessToken == "") {
|
||||
return nil
|
||||
}
|
||||
m, err := w.discover(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if m.RevocationEndpoint == "" {
|
||||
return errors.New("yaiam: the provider offers no revocation")
|
||||
}
|
||||
for _, tok := range []struct{ value, hint string }{{t.RefreshToken, "refresh_token"}, {t.AccessToken, "access_token"}} {
|
||||
if tok.value == "" {
|
||||
continue
|
||||
}
|
||||
if err := w.postClient(ctx, "revocation", m.RevocationEndpoint, url.Values{"token": {tok.value}, "token_type_hint": {tok.hint}}, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// postClient posts form as this client: a confidential one with its ID
|
||||
// and secret in a Basic header (RFC 6749 §2.3.1), a public one with
|
||||
// client_id in the form.
|
||||
func (w *Web) postClient(ctx context.Context, name, endpoint string, form url.Values, out any) error {
|
||||
if w.ClientSecret == "" {
|
||||
form.Set("client_id", w.ClientID)
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, strings.NewReader(form.Encode()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
req.Header.Set("Accept", "application/json")
|
||||
if w.ClientSecret != "" {
|
||||
req.SetBasicAuth(url.QueryEscape(w.ClientID), url.QueryEscape(w.ClientSecret))
|
||||
}
|
||||
return w.send(name, req, out)
|
||||
}
|
||||
Reference in new issue
Block a user