94 lines
3.2 KiB
Go
94 lines
3.2 KiB
Go
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)
|
||
|
|
}
|