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
This commit is contained in:
2026-04-18 17:41:42 +02:00
parent fc1ebb4967
commit 4601af88b5
9 changed files with 501 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
package infra
import (
"net/url"
"strings"
)
// Oauth2AuthURL construye la URL de autorizacion OAuth2 a partir de la config.
// Funcion pura — no hace I/O, solo concatenacion de strings.
// La URL resultante redirige al usuario al proveedor para que autorice el acceso.
func Oauth2AuthURL(config OAuthConfig, state string) string {
q := url.Values{}
q.Set("client_id", config.ClientID)
q.Set("redirect_uri", config.RedirectURL)
q.Set("response_type", "code")
if len(config.Scopes) > 0 {
q.Set("scope", strings.Join(config.Scopes, " "))
}
if state != "" {
q.Set("state", state)
}
sep := "?"
if strings.Contains(config.AuthURL, "?") {
sep = "&"
}
return config.AuthURL + sep + q.Encode()
}