ama-oauth-client
v0.0.5
Published
Lightweight OAuth 2.0 Client utilities for Next.js / front apps
Downloads
9
Maintainers
Readme
ama-oauth-client 🚀
ama-oauth-client est une librairie légère TypeScript pour simplifier l'intégration OAuth 2.0 dans vos projets Next.js, Vite, ou Node.js.
Elle gère automatiquement PKCE, state, et l'échange de tokens de manière simple et moderne.
✨ Fonctionnalités
- Génération automatique du Code Verifier et Code Challenge (PKCE)
- Support natif de state (protection CSRF)
- Construction facile d'une Authorization URL
- Échange de code, refresh_token, client_credentials avec
exchangeToken() - Écrit en TypeScript, typé à 100%
- Ultra léger, sans dépendances lourdes
- Idéal pour Next.js, Vite, Node.js apps modernes
📦 Installation
npm install ama-oauth-client
# ou
yarn add ama-oauth-client🚀 Utilisation
1. Définir votre configuration OAuth2
import { OAuthConfig } from "ama-oauth-client";
const config: OAuthConfig = {
id: "my-app",
name: "My App",
issuer: "https://my-app.com",
type: "oauth",
wellKnown: "https://my-app.com/.well-known/openid-configuration",
authorization: {
url: "https://my-app.com/oauth/authorize",
params: {
scope: "openid profile email",
},
},
checks: ["pkce", "state"],
token: "https://my-app.com/oauth/token",
userinfo: "https://my-app.com/oauth/userinfo",
clientId: "your-client-id",
clientSecret: "your-client-secret",
redirectUri: "https://my-app.com/oauth/callback",
};2. Générer l'URL de connexion (PKCE + State)
import { buildAuthorizationUrl, generateServerCodeVerifier, generateServerCodeChallenge } from "ama-oauth-client";
const codeVerifier = await generateServerCodeVerifier();
const codeChallenge = await generateServerCodeChallenge(codeVerifier);
const authorizationUrl = buildAuthorizationUrl(config, codeChallenge);
// Redirigez l'utilisateur vers `authorizationUrl`3. Échanger le Code d'Autorisation pour un Token
import { exchangeToken } from "ama-oauth-client";
const tokenResponse = await exchangeToken(config, {
grantType: "authorization_code",
code: "CODE_REÇU",
codeVerifier,
state: "STATE_UTILISÉ_OPTIONNEL",
});4. Rafraîchir un Access Token
const refreshResponse = await exchangeToken(config, {
grantType: "refresh_token",
refreshToken: "VOTRE_REFRESH_TOKEN",
});5. Authentification Client Credentials (facultatif)
const clientCredentialsResponse = await exchangeToken(config, {
grantType: "client_credentials",
});💖 Bonus
- PKCE est automatiquement géré (
S256) si vous utilisez"pkce"danschecks - State est aussi supporté pour renforcer la sécurité
📚 Fonctions Exportées
| Fonction | Description |
|:---------|:------------|
| buildAuthorizationUrl(config, codeChallenge) | Génère l'URL d'authentification OAuth2 |
| generateServerCodeVerifier() | Génère un Code Verifier (PKCE) |
| generateServerCodeChallenge(codeVerifier) | Génère un Code Challenge (PKCE) |
| exchangeToken(config, options) | Permet d’échanger authorization_code, refresh_token ou client_credentials |
📝 License
MIT © Michael Amimba
🛠️ À venir ?
- Génération automatique de
state - Support des erreurs OAuth détaillées
