Files
fn_registry/functions/infra/oauth2_auth_url.md
T
egutierrez 4601af88b5 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_auth_url function go infra 1.0.0 pure func Oauth2AuthURL(config OAuthConfig, state string) string Construye la URL de autorizacion OAuth2 a partir de la config. Funcion pura que concatena el AuthURL del proveedor con los query params (client_id, redirect_uri, response_type=code, scope, state).
oauth
oauth2
auth
url
infra
OAuthConfig_go_infra
false
net/url
strings
name desc
config OAuthConfig del proveedor (Google, GitHub, etc.) con ClientID, AuthURL, RedirectURL y Scopes
name desc
state valor aleatorio anti-CSRF que debe validarse en el callback. Si es vacio no se añade
URL completa a la que redirigir al usuario para iniciar el flujo OAuth2 true
genera URL con todos los params basicos
concatena scopes con espacio
añade state si no es vacio
detecta si AuthURL ya trae query y usa & en vez de ?
functions/infra/oauth2_auth_url_test.go functions/infra/oauth2_auth_url.go

Ejemplo

google := OAuthConfig{
    ClientID:    os.Getenv("GOOGLE_CLIENT_ID"),
    AuthURL:     "https://accounts.google.com/o/oauth2/v2/auth",
    RedirectURL: "http://localhost:8080/callback",
    Scopes:      []string{"openid", "email", "profile"},
}
state := "random-anti-csrf-token" // guardar en cookie/session
url := Oauth2AuthURL(google, state)
http.Redirect(w, r, url, http.StatusTemporaryRedirect)

Notas

Pura — solo hace string building con net/url.Values.Encode() (ordena params alfabeticamente y hace URL-encoding). No lee env, ni toca I/O, ni time.Now(). El state es critico para prevenir CSRF: debe ser aleatorio por sesion, guardarse server-side (cookie firmada, session, etc.) y validarse en el callback antes de hacer Oauth2Exchange. Un state vacio significa sin proteccion CSRF y no se incluye en la URL — solo apto para pruebas locales.