@leanmcp/auth 0.1.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +606 -18
- package/dist/auth0-54GZT2EI.mjs +102 -0
- package/dist/{chunk-NALGJYQB.mjs → chunk-EVD2TRPR.mjs} +63 -15
- package/dist/clerk-FR7ITM33.mjs +115 -0
- package/dist/{cognito-GBSAAMZI.mjs → cognito-I6V5YNYM.mjs} +1 -1
- package/dist/index.d.mts +55 -6
- package/dist/index.d.ts +55 -6
- package/dist/index.js +289 -15
- package/dist/index.mjs +3 -1
- package/package.json +1 -1
- package/dist/chunk-YC7GFXAO.mjs +0 -193
- package/dist/cognito-VCVS77OX.mjs +0 -145
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AuthProviderBase,
|
|
3
|
-
__name
|
|
4
|
-
} from "./chunk-NALGJYQB.mjs";
|
|
5
|
-
|
|
6
|
-
// src/providers/cognito.ts
|
|
7
|
-
import { CognitoIdentityProviderClient, InitiateAuthCommand } from "@aws-sdk/client-cognito-identity-provider";
|
|
8
|
-
import { createHmac } from "crypto";
|
|
9
|
-
import axios from "axios";
|
|
10
|
-
import jwt from "jsonwebtoken";
|
|
11
|
-
import jwkToPem from "jwk-to-pem";
|
|
12
|
-
var AuthCognito = class extends AuthProviderBase {
|
|
13
|
-
static {
|
|
14
|
-
__name(this, "AuthCognito");
|
|
15
|
-
}
|
|
16
|
-
cognito = null;
|
|
17
|
-
region = "";
|
|
18
|
-
userPoolId = "";
|
|
19
|
-
clientId = "";
|
|
20
|
-
clientSecret = "";
|
|
21
|
-
jwksCache = null;
|
|
22
|
-
/**
|
|
23
|
-
* Initialize the Cognito client with configuration
|
|
24
|
-
*/
|
|
25
|
-
async init(config) {
|
|
26
|
-
this.region = config?.region || process.env.AWS_REGION || "";
|
|
27
|
-
this.userPoolId = config?.userPoolId || process.env.COGNITO_USER_POOL_ID || "";
|
|
28
|
-
this.clientId = config?.clientId || process.env.COGNITO_CLIENT_ID || "";
|
|
29
|
-
this.clientSecret = config?.clientSecret || process.env.COGNITO_CLIENT_SECRET || "";
|
|
30
|
-
if (!this.region || !this.userPoolId || !this.clientId) {
|
|
31
|
-
throw new Error("Missing required Cognito configuration: region, userPoolId, and clientId are required");
|
|
32
|
-
}
|
|
33
|
-
this.cognito = new CognitoIdentityProviderClient({
|
|
34
|
-
region: this.region
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Refresh access tokens using a refresh token
|
|
39
|
-
*/
|
|
40
|
-
async refreshToken(refreshToken, username) {
|
|
41
|
-
if (!this.cognito) {
|
|
42
|
-
throw new Error("CognitoAuth not initialized. Call init() first.");
|
|
43
|
-
}
|
|
44
|
-
const authParameters = {
|
|
45
|
-
REFRESH_TOKEN: refreshToken
|
|
46
|
-
};
|
|
47
|
-
if (this.clientSecret) {
|
|
48
|
-
const usernameForHash = username;
|
|
49
|
-
const secretHash = this.calculateSecretHash(usernameForHash);
|
|
50
|
-
authParameters.SECRET_HASH = secretHash;
|
|
51
|
-
}
|
|
52
|
-
const command = new InitiateAuthCommand({
|
|
53
|
-
AuthFlow: "REFRESH_TOKEN_AUTH",
|
|
54
|
-
ClientId: this.clientId,
|
|
55
|
-
AuthParameters: authParameters
|
|
56
|
-
});
|
|
57
|
-
return await this.cognito.send(command);
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Verify a Cognito JWT token using JWKS
|
|
61
|
-
*/
|
|
62
|
-
async verifyToken(token) {
|
|
63
|
-
try {
|
|
64
|
-
await this.verifyJwt(token);
|
|
65
|
-
return true;
|
|
66
|
-
} catch (error) {
|
|
67
|
-
if (error instanceof Error) {
|
|
68
|
-
if (error.message.includes("jwt expired")) {
|
|
69
|
-
throw new Error("Token has expired");
|
|
70
|
-
} else if (error.message.includes("invalid signature")) {
|
|
71
|
-
throw new Error("Invalid token signature");
|
|
72
|
-
} else if (error.message.includes("jwt malformed")) {
|
|
73
|
-
throw new Error("Malformed token");
|
|
74
|
-
} else if (error.message.includes("invalid issuer")) {
|
|
75
|
-
throw new Error("Invalid token issuer");
|
|
76
|
-
}
|
|
77
|
-
throw error;
|
|
78
|
-
}
|
|
79
|
-
return false;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Get user information from an ID token
|
|
84
|
-
*/
|
|
85
|
-
async getUser(idToken) {
|
|
86
|
-
const decoded = jwt.decode(idToken);
|
|
87
|
-
if (!decoded) {
|
|
88
|
-
throw new Error("Invalid ID token");
|
|
89
|
-
}
|
|
90
|
-
return {
|
|
91
|
-
username: decoded["cognito:username"],
|
|
92
|
-
email: decoded.email,
|
|
93
|
-
email_verified: decoded.email_verified,
|
|
94
|
-
sub: decoded.sub,
|
|
95
|
-
attributes: decoded
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Fetch JWKS from Cognito (cached)
|
|
100
|
-
*/
|
|
101
|
-
async fetchJWKS() {
|
|
102
|
-
if (!this.jwksCache) {
|
|
103
|
-
const jwksUri = `https://cognito-idp.${this.region}.amazonaws.com/${this.userPoolId}/.well-known/jwks.json`;
|
|
104
|
-
const { data } = await axios.get(jwksUri);
|
|
105
|
-
this.jwksCache = data.keys;
|
|
106
|
-
}
|
|
107
|
-
return this.jwksCache;
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Verify JWT token using JWKS
|
|
111
|
-
*/
|
|
112
|
-
async verifyJwt(token) {
|
|
113
|
-
const decoded = jwt.decode(token, {
|
|
114
|
-
complete: true
|
|
115
|
-
});
|
|
116
|
-
if (!decoded) {
|
|
117
|
-
throw new Error("Invalid token");
|
|
118
|
-
}
|
|
119
|
-
const jwks = await this.fetchJWKS();
|
|
120
|
-
const key = jwks.find((k) => k.kid === decoded.header.kid);
|
|
121
|
-
if (!key) {
|
|
122
|
-
throw new Error("Signing key not found in JWKS");
|
|
123
|
-
}
|
|
124
|
-
const pem = jwkToPem(key);
|
|
125
|
-
return jwt.verify(token, pem, {
|
|
126
|
-
algorithms: [
|
|
127
|
-
"RS256"
|
|
128
|
-
],
|
|
129
|
-
issuer: `https://cognito-idp.${this.region}.amazonaws.com/${this.userPoolId}`
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Calculate SECRET_HASH for Cognito authentication
|
|
134
|
-
* SECRET_HASH = Base64(HMAC_SHA256(username + clientId, clientSecret))
|
|
135
|
-
*/
|
|
136
|
-
calculateSecretHash(username) {
|
|
137
|
-
const message = username + this.clientId;
|
|
138
|
-
const hmac = createHmac("sha256", this.clientSecret);
|
|
139
|
-
hmac.update(message);
|
|
140
|
-
return hmac.digest("base64");
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
export {
|
|
144
|
-
AuthCognito
|
|
145
|
-
};
|