@ooneex/auth 0.0.19 → 0.1.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/dist/index.d.ts +41 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +6 -3
- package/package.json +8 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,47 @@
|
|
|
1
|
+
import { Exception } from "@ooneex/exception";
|
|
2
|
+
declare class AuthException extends Exception {
|
|
3
|
+
constructor(message: string, data?: Record<string, unknown>);
|
|
4
|
+
}
|
|
5
|
+
import { Session, User } from "@clerk/backend";
|
|
6
|
+
declare class ClerkAuth {
|
|
7
|
+
private readonly client;
|
|
8
|
+
private readonly secretKey;
|
|
9
|
+
constructor();
|
|
10
|
+
getCurrentUser(token: string): Promise<User | null>;
|
|
11
|
+
banUser(userId: string): Promise<User>;
|
|
12
|
+
unbanUser(userId: string): Promise<User>;
|
|
13
|
+
getUser(userId: string): Promise<User>;
|
|
14
|
+
lockUser(userId: string): Promise<User>;
|
|
15
|
+
unlockUser(userId: string): Promise<User>;
|
|
16
|
+
updateUser(userId: string, params: Parameters<typeof this.client.users.updateUser>[1]): Promise<User>;
|
|
17
|
+
updateUserProfileImage(userId: string, params: {
|
|
18
|
+
file: Blob | File;
|
|
19
|
+
}): Promise<User>;
|
|
20
|
+
updateUserMetadata(userId: string, params: Parameters<typeof this.client.users.updateUserMetadata>[1]): Promise<User>;
|
|
21
|
+
getUserMetadata(userId: string): Promise<{
|
|
22
|
+
publicMetadata: User["publicMetadata"];
|
|
23
|
+
privateMetadata: User["privateMetadata"];
|
|
24
|
+
unsafeMetadata: User["unsafeMetadata"];
|
|
25
|
+
}>;
|
|
26
|
+
deleteUser(userId: string): Promise<User>;
|
|
27
|
+
deleteUserProfileImage(userId: string): Promise<User>;
|
|
28
|
+
getSession(sessionId: string): Promise<Session>;
|
|
29
|
+
getCurrentUserSession(token: string): Promise<Session | null>;
|
|
30
|
+
signOut(sessionId: string): Promise<Session>;
|
|
31
|
+
}
|
|
32
|
+
import { ContextConfigType as ContextConfigType2, ContextType as ContextType2 } from "@ooneex/controller";
|
|
33
|
+
import { UserRepository } from "@ooneex/typeorm/repositories/user";
|
|
34
|
+
import { IUser as IUser2 } from "@ooneex/user";
|
|
1
35
|
import { ContextConfigType, ContextType } from "@ooneex/controller";
|
|
2
36
|
import { IUser } from "@ooneex/user";
|
|
3
37
|
type AuthMiddlewareClassType = new (...args: any[]) => IAuthMiddleware;
|
|
4
38
|
interface IAuthMiddleware<T extends ContextConfigType = ContextConfigType> {
|
|
5
39
|
handler: (context: ContextType<T>) => Promise<IUser> | IUser;
|
|
6
40
|
}
|
|
7
|
-
|
|
41
|
+
declare class ClerkAuthMiddleware implements IAuthMiddleware {
|
|
42
|
+
private readonly clerkAuth;
|
|
43
|
+
private readonly userRepository;
|
|
44
|
+
constructor(clerkAuth: ClerkAuth, userRepository: UserRepository);
|
|
45
|
+
handler<T extends ContextConfigType2>(context: ContextType2<T>): Promise<IUser2>;
|
|
46
|
+
}
|
|
47
|
+
export { IAuthMiddleware, ClerkAuthMiddleware, ClerkAuth, AuthMiddlewareClassType, AuthException };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
// @bun
|
|
2
|
+
var o=function(e,s,t,r){var c=arguments.length,i=c<3?s:r===null?r=Object.getOwnPropertyDescriptor(s,t):r,u;if(typeof Reflect==="object"&&typeof Reflect.decorate==="function")i=Reflect.decorate(e,s,t,r);else for(var U=e.length-1;U>=0;U--)if(u=e[U])i=(c<3?u(i):c>3?u(s,t,i):u(s,t))||i;return c>3&&i&&Object.defineProperty(s,t,i),i},m=(e,s)=>(t,r)=>s(t,r,e),l=(e,s)=>{if(typeof Reflect==="object"&&typeof Reflect.metadata==="function")return Reflect.metadata(e,s)};import{Exception as f}from"@ooneex/exception";import{HttpStatus as P}from"@ooneex/http-status";class a extends f{constructor(e,s={}){super(e,{status:P.Code.InternalServerError,data:s});this.name="AuthException"}}import{createClerkClient as h,verifyToken as d}from"@clerk/backend";import{injectable as w}from"@ooneex/container";class n{client;secretKey;constructor(){let e=Bun.env.CLERK_SECRET_KEY,s=Bun.env.PUBLIC_CLERK_PUBLISHABLE_KEY;if(!e)throw new a("CLERK_SECRET_KEY environment variable is required");this.secretKey=e,this.client=h({secretKey:e,...s?{publishableKey:s}:{}})}async getCurrentUser(e){let{sub:s}=await d(e,{secretKey:this.secretKey});if(!s)return null;return await this.getUser(s)}async banUser(e){return await this.client.users.banUser(e)}async unbanUser(e){return await this.client.users.unbanUser(e)}async getUser(e){return await this.client.users.getUser(e)}async lockUser(e){return await this.client.users.lockUser(e)}async unlockUser(e){return await this.client.users.unlockUser(e)}async updateUser(e,s){return await this.client.users.updateUser(e,s)}async updateUserProfileImage(e,s){return await this.client.users.updateUserProfileImage(e,s)}async updateUserMetadata(e,s){return await this.client.users.updateUserMetadata(e,s)}async getUserMetadata(e){let s=await this.getUser(e);return{publicMetadata:s.publicMetadata,privateMetadata:s.privateMetadata,unsafeMetadata:s.unsafeMetadata}}async deleteUser(e){return await this.client.users.deleteUser(e)}async deleteUserProfileImage(e){return await this.client.users.deleteUserProfileImage(e)}async getSession(e){return await this.client.sessions.getSession(e)}async getCurrentUserSession(e){let{sid:s}=await d(e,{secretKey:this.secretKey});if(!s)return null;return await this.getSession(s)}async signOut(e){return await this.client.sessions.revokeSession(e)}}n=o([w(),l("design:paramtypes",[])],n);import{inject as y}from"@ooneex/container";import{HttpStatus as g}from"@ooneex/http-status";import{UserRepository as b}from"@ooneex/typeorm/repositories/user";class p{clerkAuth;userRepository;constructor(e,s){this.clerkAuth=e;this.userRepository=s}async handler(e){let s=e.header.getBearerToken();if(!s)throw new a("Authentication required: Missing bearer token",{status:g.Code.Unauthorized});let t=await this.clerkAuth.getCurrentUser(s);if(!t)throw new a("Authentication failed: Invalid or expired token",{status:g.Code.Unauthorized});let r=await this.userRepository.findOneBy({key:t.id});if(!r)throw new a("User not found",{status:g.Code.Unauthorized});return r}}p=o([m(0,y(n)),m(1,y(b)),l("design:paramtypes",[typeof n==="undefined"?Object:n,typeof b==="undefined"?Object:b])],p);export{p as ClerkAuthMiddleware,n as ClerkAuth,a as AuthException};
|
|
2
3
|
|
|
3
|
-
//# debugId=
|
|
4
|
+
//# debugId=A41AB52842A4B22064756E2164756E21
|
package/dist/index.js.map
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": [],
|
|
3
|
+
"sources": ["src/AuthException.ts", "src/ClerkAuth.ts", "src/ClerkAuthMiddleware.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
+
"import { Exception } from \"@ooneex/exception\";\nimport { HttpStatus } from \"@ooneex/http-status\";\n\nexport class AuthException extends Exception {\n constructor(message: string, data: Record<string, unknown> = {}) {\n super(message, {\n status: HttpStatus.Code.InternalServerError,\n data,\n });\n this.name = \"AuthException\";\n }\n}\n",
|
|
6
|
+
"import { createClerkClient, type Session, type User, verifyToken } from \"@clerk/backend\";\nimport { injectable } from \"@ooneex/container\";\nimport { AuthException } from \"./AuthException\";\n\n@injectable()\nexport class ClerkAuth {\n private readonly client: ReturnType<typeof createClerkClient>;\n private readonly secretKey: string;\n\n constructor() {\n const secretKey = Bun.env.CLERK_SECRET_KEY;\n const publishableKey = Bun.env.PUBLIC_CLERK_PUBLISHABLE_KEY;\n\n if (!secretKey) {\n throw new AuthException(\"CLERK_SECRET_KEY environment variable is required\");\n }\n\n this.secretKey = secretKey;\n this.client = createClerkClient({\n secretKey,\n ...(publishableKey ? { publishableKey } : {}),\n });\n }\n\n public async getCurrentUser(token: string): Promise<User | null> {\n const { sub: userId } = await verifyToken(token, {\n secretKey: this.secretKey,\n });\n\n if (!userId) {\n return null;\n }\n\n return await this.getUser(userId);\n }\n\n public async banUser(userId: string): Promise<User> {\n return await this.client.users.banUser(userId);\n }\n\n public async unbanUser(userId: string): Promise<User> {\n return await this.client.users.unbanUser(userId);\n }\n\n public async getUser(userId: string): Promise<User> {\n return await this.client.users.getUser(userId);\n }\n\n public async lockUser(userId: string): Promise<User> {\n return await this.client.users.lockUser(userId);\n }\n\n public async unlockUser(userId: string): Promise<User> {\n return await this.client.users.unlockUser(userId);\n }\n\n public async updateUser(userId: string, params: Parameters<typeof this.client.users.updateUser>[1]): Promise<User> {\n return await this.client.users.updateUser(userId, params);\n }\n\n public async updateUserProfileImage(userId: string, params: { file: Blob | File }): Promise<User> {\n return await this.client.users.updateUserProfileImage(userId, params);\n }\n\n public async updateUserMetadata(\n userId: string,\n params: Parameters<typeof this.client.users.updateUserMetadata>[1],\n ): Promise<User> {\n return await this.client.users.updateUserMetadata(userId, params);\n }\n\n public async getUserMetadata(userId: string): Promise<{\n publicMetadata: User[\"publicMetadata\"];\n privateMetadata: User[\"privateMetadata\"];\n unsafeMetadata: User[\"unsafeMetadata\"];\n }> {\n const user = await this.getUser(userId);\n return {\n publicMetadata: user.publicMetadata,\n privateMetadata: user.privateMetadata,\n unsafeMetadata: user.unsafeMetadata,\n };\n }\n\n public async deleteUser(userId: string): Promise<User> {\n return await this.client.users.deleteUser(userId);\n }\n\n public async deleteUserProfileImage(userId: string): Promise<User> {\n return await this.client.users.deleteUserProfileImage(userId);\n }\n\n public async getSession(sessionId: string): Promise<Session> {\n return await this.client.sessions.getSession(sessionId);\n }\n\n public async getCurrentUserSession(token: string): Promise<Session | null> {\n const { sid: sessionId } = await verifyToken(token, {\n secretKey: this.secretKey,\n });\n\n if (!sessionId) {\n return null;\n }\n\n return await this.getSession(sessionId);\n }\n\n public async signOut(sessionId: string): Promise<Session> {\n return await this.client.sessions.revokeSession(sessionId);\n }\n}\n",
|
|
7
|
+
"import { inject } from \"@ooneex/container\";\nimport type { ContextConfigType, ContextType } from \"@ooneex/controller\";\nimport { HttpStatus } from \"@ooneex/http-status\";\nimport { UserRepository } from \"@ooneex/typeorm/repositories/user\";\nimport type { IUser } from \"@ooneex/user\";\nimport { AuthException } from \"./AuthException\";\nimport { ClerkAuth } from \"./ClerkAuth\";\nimport type { IAuthMiddleware } from \"./types\";\n\nexport class ClerkAuthMiddleware implements IAuthMiddleware {\n constructor(\n @inject(ClerkAuth) private readonly clerkAuth: ClerkAuth,\n @inject(UserRepository) private readonly userRepository: UserRepository,\n ) {}\n\n public async handler<T extends ContextConfigType>(context: ContextType<T>): Promise<IUser> {\n const token = context.header.getBearerToken();\n\n if (!token) {\n throw new AuthException(\"Authentication required: Missing bearer token\", {\n status: HttpStatus.Code.Unauthorized,\n });\n }\n\n const clerkUser = await this.clerkAuth.getCurrentUser(token);\n\n if (!clerkUser) {\n throw new AuthException(\"Authentication failed: Invalid or expired token\", {\n status: HttpStatus.Code.Unauthorized,\n });\n }\n\n const user = await this.userRepository.findOneBy({ key: clerkUser.id });\n\n if (!user) {\n throw new AuthException(\"User not found\", {\n status: HttpStatus.Code.Unauthorized,\n });\n }\n\n return user;\n }\n}\n"
|
|
5
8
|
],
|
|
6
|
-
"mappings": "",
|
|
7
|
-
"debugId": "
|
|
9
|
+
"mappings": ";8cAAA,oBAAS,0BACT,qBAAS,4BAEF,MAAM,UAAsB,CAAU,CAC3C,WAAW,CAAC,EAAiB,EAAgC,CAAC,EAAG,CAC/D,MAAM,EAAS,CACb,OAAQ,EAAW,KAAK,oBACxB,MACF,CAAC,EACD,KAAK,KAAO,gBAEhB,CCXA,4BAAS,iBAA4C,uBACrD,qBAAS,0BAIF,MAAM,CAAU,CACJ,OACA,UAEjB,WAAW,EAAG,CACZ,IAAM,EAAY,IAAI,IAAI,iBACpB,EAAiB,IAAI,IAAI,6BAE/B,GAAI,CAAC,EACH,MAAM,IAAI,EAAc,mDAAmD,EAG7E,KAAK,UAAY,EACjB,KAAK,OAAS,EAAkB,CAC9B,eACI,EAAiB,CAAE,gBAAe,EAAI,CAAC,CAC7C,CAAC,OAGU,eAAc,CAAC,EAAqC,CAC/D,IAAQ,IAAK,GAAW,MAAM,EAAY,EAAO,CAC/C,UAAW,KAAK,SAClB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OAAO,MAAM,KAAK,WAAW,CAAS,OAG3B,QAAO,CAAC,EAAqC,CACxD,OAAO,MAAM,KAAK,OAAO,SAAS,cAAc,CAAS,EAE7D,CA1Ga,EAAN,GADN,EAAW,EACL,2BAAM,GCLb,iBAAS,0BAET,qBAAS,4BACT,yBAAS,0CAMF,MAAM,CAA+C,CAEpB,UACK,eAF3C,WAAW,CAC2B,EACK,EACzC,CAFoC,iBACK,2BAG9B,QAAoC,CAAC,EAAyC,CACzF,IAAM,EAAQ,EAAQ,OAAO,eAAe,EAE5C,GAAI,CAAC,EACH,MAAM,IAAI,EAAc,gDAAiD,CACvE,OAAQ,EAAW,KAAK,YAC1B,CAAC,EAGH,IAAM,EAAY,MAAM,KAAK,UAAU,eAAe,CAAK,EAE3D,GAAI,CAAC,EACH,MAAM,IAAI,EAAc,kDAAmD,CACzE,OAAQ,EAAW,KAAK,YAC1B,CAAC,EAGH,IAAM,EAAO,MAAM,KAAK,eAAe,UAAU,CAAE,IAAK,EAAU,EAAG,CAAC,EAEtE,GAAI,CAAC,EACH,MAAM,IAAI,EAAc,iBAAkB,CACxC,OAAQ,EAAW,KAAK,YAC1B,CAAC,EAGH,OAAO,EAEX,CAjCa,EAAN,GAEF,MAAO,CAAS,GAChB,MAAO,CAAc,GAHnB,0FAAM",
|
|
10
|
+
"debugId": "A41AB52842A4B22064756E2164756E21",
|
|
8
11
|
"names": []
|
|
9
12
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ooneex/auth",
|
|
3
3
|
"description": "Authentication utilities and strategies for securing Ooneex applications",
|
|
4
|
-
"version": "0.0
|
|
4
|
+
"version": "0.1.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist",
|
|
@@ -27,8 +27,13 @@
|
|
|
27
27
|
"npm:publish": "bun publish --tolerate-republish --access public"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@
|
|
31
|
-
"@ooneex/
|
|
30
|
+
"@clerk/backend": "^1.24.1",
|
|
31
|
+
"@ooneex/container": "0.0.18",
|
|
32
|
+
"@ooneex/controller": "0.16.1",
|
|
33
|
+
"@ooneex/exception": "0.0.17",
|
|
34
|
+
"@ooneex/http-status": "0.0.17",
|
|
35
|
+
"@ooneex/typeorm": "0.16.0",
|
|
36
|
+
"@ooneex/user": "0.0.17"
|
|
32
37
|
},
|
|
33
38
|
"devDependencies": {},
|
|
34
39
|
"keywords": [
|