Files
fn_registry/functions/infra/oauth2_refresh.md
T
egutierrez 1e5dfa5193 feat: oauth2_auth_url (pure), oauth2_exchange, oauth2_refresh
Fase 4 del issue 0010 — cliente OAuth2 sin golang.org/x/oauth2.
- Oauth2AuthURL es pura: solo construye la URL con net/url
- Oauth2Exchange/Refresh hacen POST application/x-www-form-urlencoded
- ExpiresAt calculado como now + expires_in del proveedor
- Refresh conserva el token original si el proveedor no devuelve uno nuevo
- Tests con httptest.NewServer como mock del proveedor
2026-04-18 17:41:42 +02:00

2.0 KiB

name, kind, lang, domain, version, purity, signature, description, tags, uses_functions, uses_types, returns, returns_optional, error_type, imports, params, output, tested, tests, test_file_path, file_path
name kind lang domain version purity signature description tags uses_functions uses_types returns returns_optional error_type imports params output tested tests test_file_path file_path
oauth2_refresh function go infra 1.0.0 impure func Oauth2Refresh(config OAuthConfig, refreshToken string) (OAuthTokens, error) Renueva un access token OAuth2 usando el refresh token. POST al TokenURL con grant_type=refresh_token. Conserva el refresh token original si el proveedor no devuelve uno nuevo.
oauth
oauth2
auth
token
refresh
http
infra
OAuthConfig_go_infra
OAuthTokens_go_infra
OAuthTokens_go_infra
false error_go_core
fmt
net/url
name desc
config OAuthConfig del proveedor con ClientID, ClientSecret y TokenURL
name desc
refreshToken refresh token obtenido previamente de Oauth2Exchange
OAuthTokens con nuevo AccessToken. Si el proveedor no devuelve RefreshToken se conserva el original true
renueva tokens contra mock server
conserva refresh token si el proveedor no devuelve uno nuevo
rechaza refresh vacio
functions/infra/oauth2_refresh_test.go functions/infra/oauth2_refresh.go

Ejemplo

tokens, err := Oauth2Refresh(googleConfig, storedRefreshToken)
if err != nil {
    // El refresh token tambien puede haber caducado — forzar relogin
    HTTPErrorResponse(w, HTTPError{Status: 401, Code: "refresh_failed", Message: err.Error()})
    return
}
saveTokens(tokens) // actualizar tokens en BD/cookie

Notas

Impura — reutiliza oauth2DoTokenRequest para el POST. Algunos proveedores (Google) no devuelven un nuevo RefreshToken al renovar — en ese caso se conserva el original. Otros (Microsoft) pueden rotar el refresh token en cada renovacion: el campo tokens.RefreshToken siempre trae el que hay que guardar para la proxima renovacion. Si el refresh token expiro (el usuario revoco acceso o paso demasiado tiempo) el proveedor retorna 400 con error: invalid_grant y se propaga como error.