@auditauth/core 0.2.0-beta.1

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 ADDED
@@ -0,0 +1,42 @@
1
+ # @auditauth/core
2
+
3
+ `@auditauth/core` is the shared foundation used by the AuditAuth SDK packages.
4
+ It provides token and portal helpers, request metrics helpers, shared types,
5
+ and runtime settings used across the SDK ecosystem.
6
+
7
+ ## Install
8
+
9
+ Install the package in your project.
10
+
11
+ ```bash
12
+ npm install @auditauth/core
13
+ ```
14
+
15
+ ## Minimal example
16
+
17
+ Import and use the core helpers directly.
18
+
19
+ ```ts
20
+ import { buildAuthUrl, CORE_SETTINGS } from '@auditauth/core'
21
+
22
+ const url = await buildAuthUrl({
23
+ apiKey: process.env.AUDITAUTH_API_KEY!,
24
+ redirectUrl: 'https://app.example.com/auth/callback',
25
+ })
26
+
27
+ console.log(url)
28
+ console.log(CORE_SETTINGS.jwt_issuer)
29
+ ```
30
+
31
+ ## Compatibility
32
+
33
+ This package requires Node.js `>=18.18.0`.
34
+
35
+ ## Resources
36
+
37
+ - Repository: https://github.com/nimibyte/auditauth-sdk
38
+ - Documentation: https://docs.auditauth.com
39
+
40
+ ## License
41
+
42
+ MIT
@@ -0,0 +1,18 @@
1
+ import { SessionUser } from "../types";
2
+ type AuthorizePayload = {
3
+ code: string | null;
4
+ client_type: 'browser' | 'server';
5
+ };
6
+ type AuthorizeData = {
7
+ user: SessionUser;
8
+ access_token: string;
9
+ access_expires_seconds: number;
10
+ refresh_token?: string;
11
+ refresh_expires_seconds: number;
12
+ };
13
+ type AuthorizeReturns = {
14
+ response: Response;
15
+ data: AuthorizeData;
16
+ };
17
+ declare const authorizeCode: ({ code, client_type }: AuthorizePayload) => Promise<AuthorizeReturns>;
18
+ export default authorizeCode;
@@ -0,0 +1,20 @@
1
+ import { CORE_SETTINGS } from "../settings";
2
+ const authorizeCode = async ({ code, client_type }) => {
3
+ if (!code)
4
+ throw new Error('Invalid code');
5
+ const response = await fetch(`${CORE_SETTINGS.domains.api}/auth/authorize`, {
6
+ method: 'POST',
7
+ headers: { 'Content-Type': 'application/json' },
8
+ body: JSON.stringify({ code, client_type }),
9
+ credentials: 'include',
10
+ });
11
+ if (!response.ok) {
12
+ throw new Error('Unauthorized');
13
+ }
14
+ const data = await response.json();
15
+ return {
16
+ response,
17
+ data: data,
18
+ };
19
+ };
20
+ export default authorizeCode;
@@ -0,0 +1,6 @@
1
+ type BuildAuthUrlPayload = {
2
+ apiKey: string;
3
+ redirectUrl: string;
4
+ };
5
+ declare const buildAuthUrl: ({ apiKey, redirectUrl }: BuildAuthUrlPayload) => Promise<import("url").URL>;
6
+ export default buildAuthUrl;
@@ -0,0 +1,21 @@
1
+ import { CORE_SETTINGS } from "../settings";
2
+ const buildAuthUrl = async ({ apiKey, redirectUrl }) => {
3
+ const response = await fetch(`${CORE_SETTINGS.domains.api}/applications/login`, {
4
+ method: 'POST',
5
+ headers: {
6
+ 'Content-Type': 'application/json',
7
+ 'x-api-key': apiKey,
8
+ },
9
+ body: JSON.stringify({ redirect_url: redirectUrl }),
10
+ });
11
+ if (!response.ok) {
12
+ const error = await response.text();
13
+ throw new Error(error);
14
+ }
15
+ const body = await response.json();
16
+ const { redirectUrl: authRedirectUrl, code } = body;
17
+ const url = new URL(authRedirectUrl);
18
+ url.searchParams.set('code', code);
19
+ return url;
20
+ };
21
+ export default buildAuthUrl;
@@ -0,0 +1,4 @@
1
+ export { default as buildAuthUrl } from './buildAuthUrl';
2
+ export { default as authorizeCode } from './authorizeCode';
3
+ export { default as revokeSession } from './revokeSession';
4
+ export { default as refreshTokens } from './refreshTokens';
@@ -0,0 +1,4 @@
1
+ export { default as buildAuthUrl } from './buildAuthUrl';
2
+ export { default as authorizeCode } from './authorizeCode';
3
+ export { default as revokeSession } from './revokeSession';
4
+ export { default as refreshTokens } from './refreshTokens';
@@ -0,0 +1,7 @@
1
+ import { CredentialsResponse } from "../types";
2
+ type RefreshTokensPayload = {
3
+ client_type: 'browser' | 'server';
4
+ refresh_token?: string;
5
+ };
6
+ declare const refreshTokens: ({ client_type, refresh_token }: RefreshTokensPayload) => Promise<CredentialsResponse | null>;
7
+ export default refreshTokens;
@@ -0,0 +1,14 @@
1
+ import { CORE_SETTINGS } from "../settings";
2
+ const refreshTokens = async ({ client_type, refresh_token }) => {
3
+ const response = await fetch(`${CORE_SETTINGS.domains.api}/auth/refresh`, {
4
+ method: 'POST',
5
+ credentials: 'include',
6
+ headers: { 'Content-Type': 'application/json' },
7
+ body: JSON.stringify({ client_type, refresh_token }),
8
+ });
9
+ if (!response.ok)
10
+ return null;
11
+ const data = await response.json();
12
+ return data;
13
+ };
14
+ export default refreshTokens;
@@ -0,0 +1,5 @@
1
+ type RevokeSessionPayload = {
2
+ access_token?: string;
3
+ };
4
+ declare const revokeSession: ({ access_token }: RevokeSessionPayload) => Promise<void>;
5
+ export default revokeSession;
@@ -0,0 +1,11 @@
1
+ import { CORE_SETTINGS } from "../settings";
2
+ const revokeSession = async ({ access_token }) => {
3
+ const response = await fetch(`${CORE_SETTINGS.domains.api}/auth/revoke`, {
4
+ method: 'PATCH',
5
+ headers: { Authorization: `Bearer ${access_token}` },
6
+ });
7
+ if (!response.ok) {
8
+ throw new Error('Error revoking session');
9
+ }
10
+ };
11
+ export default revokeSession;
@@ -0,0 +1,5 @@
1
+ export * from './settings';
2
+ export type * from './types';
3
+ export * from './auth';
4
+ export * from './portal';
5
+ export * from './metrics';
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from './settings';
2
+ export * from './auth';
3
+ export * from './portal';
4
+ export * from './metrics';
@@ -0,0 +1 @@
1
+ export { default as sendMetrics } from './sendMetrics';
@@ -0,0 +1 @@
1
+ export { default as sendMetrics } from './sendMetrics';
@@ -0,0 +1,8 @@
1
+ import { Metric } from "../types";
2
+ type SendMetricsPayload = {
3
+ payload: Metric;
4
+ appId: string;
5
+ apiKey: string;
6
+ };
7
+ declare const sendMetrics: ({ apiKey, appId, payload }: SendMetricsPayload) => Promise<void>;
8
+ export default sendMetrics;
@@ -0,0 +1,11 @@
1
+ import { CORE_SETTINGS } from "../settings";
2
+ const sendMetrics = async ({ apiKey, appId, payload }) => {
3
+ await fetch(`${CORE_SETTINGS.domains.api}/metrics`, {
4
+ method: 'POST',
5
+ headers: {
6
+ 'Content-Type': 'application/json',
7
+ },
8
+ body: JSON.stringify({ ...payload, appId, apiKey }),
9
+ });
10
+ };
11
+ export default sendMetrics;
@@ -0,0 +1,6 @@
1
+ type BuildPortalUrlPayload = {
2
+ access_token: string;
3
+ redirectUrl: string;
4
+ };
5
+ declare const buildPortalUrl: ({ access_token, redirectUrl }: BuildPortalUrlPayload) => Promise<import("url").URL>;
6
+ export default buildPortalUrl;
@@ -0,0 +1,20 @@
1
+ import { CORE_SETTINGS } from "../settings";
2
+ const buildPortalUrl = async ({ access_token, redirectUrl }) => {
3
+ const response = await fetch(`${CORE_SETTINGS.domains.api}/portal/exchange`, {
4
+ method: 'GET',
5
+ headers: {
6
+ Authorization: `Bearer ${access_token}`,
7
+ },
8
+ });
9
+ if (!response.ok) {
10
+ const error = await response.text();
11
+ throw new Error(error);
12
+ }
13
+ const body = await response.json();
14
+ const { code, redirectUrl: portalUrl } = body;
15
+ const url = new URL(portalUrl);
16
+ url.searchParams.set('code', code);
17
+ url.searchParams.set('redirectUrl', redirectUrl);
18
+ return url;
19
+ };
20
+ export default buildPortalUrl;
@@ -0,0 +1 @@
1
+ export { default as buildPortalUrl } from './buildPortalUrl';
@@ -0,0 +1 @@
1
+ export { default as buildPortalUrl } from './buildPortalUrl';
@@ -0,0 +1,19 @@
1
+ declare const CORE_SETTINGS: {
2
+ readonly jwt_public_key: "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs2EYs4Q9OyjNuAEPqb4j\nIzc52JdfVcNvEbG43Xp8B2kI9QxwRyX7rtFSwKowj3W1BlCLaTIMK3TafWOf9QwH\nfemuL9Ni37PFcGptzpyuoCYYA650EuD82PENcO49lsObvty2cuXxQszbPPvAecm4\nJ/XG70td/W1UwbjAJcdmp8ktZGYR0JXM37hYA9Xq/aKwu7d0FTL6WdKTvt3L5VxL\nF6WNyLs65ZSbu+j8UEkwmoJ9h9Y0mLQmFtmkoh/HWOFyFDnBNiJX0vRb++RhJw6w\ncrSbqpbTu7z4vIep5lgSOut39P273SVTQZ3cGQIS+605Ur5wjkkSzzaJV1QLBBR9\nAQIDAQAB\n-----END PUBLIC KEY-----\n";
3
+ readonly jwt_issuer: "https://api.auditauth.com";
4
+ readonly domains: {
5
+ readonly api: "https://api.auditauth.com/v1";
6
+ readonly client: "https://auditauth.com";
7
+ } | {
8
+ readonly api: "http://localhost:4000/v1";
9
+ readonly client: "http://localhost:3000";
10
+ };
11
+ readonly storage_keys: {
12
+ readonly access: "auditauth_access";
13
+ readonly session: "auditauth_session";
14
+ readonly refresh: "auditauth_refresh";
15
+ readonly session_id: "auditauth_sid";
16
+ readonly sync: "__auditauth_sync__";
17
+ };
18
+ };
19
+ export { CORE_SETTINGS };
@@ -0,0 +1,39 @@
1
+ const PROD_DOMAINS = {
2
+ api: 'https://api.auditauth.com/v1',
3
+ client: 'https://auditauth.com',
4
+ };
5
+ const LOCAL_DOMAINS = {
6
+ api: 'http://localhost:4000/v1',
7
+ client: 'http://localhost:3000',
8
+ };
9
+ const resolveSdkEnv = () => {
10
+ const importMetaEnv = typeof import.meta !== 'undefined'
11
+ ? (import.meta.env
12
+ ?.AUDITAUTH_SDK_ENV ??
13
+ import.meta.env
14
+ ?.VITE_AUDITAUTH_SDK_ENV)
15
+ : undefined;
16
+ const globalEnv = typeof globalThis !== 'undefined'
17
+ ? globalThis.__AUDITAUTH_SDK_ENV__
18
+ : undefined;
19
+ const processEnv = typeof process !== 'undefined'
20
+ ? process.env?.AUDITAUTH_SDK_ENV ??
21
+ process.env?.NEXT_PUBLIC_AUDITAUTH_SDK_ENV ??
22
+ process.env?.NODE_ENV
23
+ : undefined;
24
+ return (importMetaEnv ?? globalEnv ?? processEnv ?? 'production').toLowerCase();
25
+ };
26
+ const isDevelopmentEnv = ['development', 'dev', 'local'].includes(resolveSdkEnv());
27
+ const CORE_SETTINGS = {
28
+ jwt_public_key: "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs2EYs4Q9OyjNuAEPqb4j\nIzc52JdfVcNvEbG43Xp8B2kI9QxwRyX7rtFSwKowj3W1BlCLaTIMK3TafWOf9QwH\nfemuL9Ni37PFcGptzpyuoCYYA650EuD82PENcO49lsObvty2cuXxQszbPPvAecm4\nJ/XG70td/W1UwbjAJcdmp8ktZGYR0JXM37hYA9Xq/aKwu7d0FTL6WdKTvt3L5VxL\nF6WNyLs65ZSbu+j8UEkwmoJ9h9Y0mLQmFtmkoh/HWOFyFDnBNiJX0vRb++RhJw6w\ncrSbqpbTu7z4vIep5lgSOut39P273SVTQZ3cGQIS+605Ur5wjkkSzzaJV1QLBBR9\nAQIDAQAB\n-----END PUBLIC KEY-----\n",
29
+ jwt_issuer: "https://api.auditauth.com",
30
+ domains: isDevelopmentEnv ? LOCAL_DOMAINS : PROD_DOMAINS,
31
+ storage_keys: {
32
+ access: 'auditauth_access',
33
+ session: 'auditauth_session',
34
+ refresh: 'auditauth_refresh',
35
+ session_id: 'auditauth_sid',
36
+ sync: '__auditauth_sync__',
37
+ }
38
+ };
39
+ export { CORE_SETTINGS };
@@ -0,0 +1,43 @@
1
+ type AuditAuthConfig = {
2
+ apiKey: string;
3
+ redirectUrl: string;
4
+ baseUrl: string;
5
+ appId: string;
6
+ };
7
+ type SessionUser = {
8
+ _id: string;
9
+ email: string;
10
+ email_verify: boolean;
11
+ name: string;
12
+ avatar: {
13
+ url: string | null;
14
+ format: string;
15
+ };
16
+ providers: string[];
17
+ };
18
+ type RequestMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
19
+ type Metric = {
20
+ event_type: 'request';
21
+ runtime: 'browser' | 'server';
22
+ target: {
23
+ type: 'api';
24
+ method: RequestMethod;
25
+ path: string;
26
+ status: number;
27
+ duration_ms: number;
28
+ };
29
+ } | {
30
+ event_type: 'navigation';
31
+ runtime: 'browser' | 'server';
32
+ target: {
33
+ type: 'page';
34
+ path: string;
35
+ };
36
+ };
37
+ type CredentialsResponse = {
38
+ access_token: string;
39
+ access_expires_seconds: number;
40
+ refresh_token: string;
41
+ refresh_expires_seconds: number;
42
+ };
43
+ export type { CredentialsResponse, AuditAuthConfig, SessionUser, Metric, RequestMethod };
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@auditauth/core",
3
+ "version": "0.2.0-beta.1",
4
+ "type": "module",
5
+ "license": "MIT",
6
+ "author": "Nimibyte",
7
+ "engines": {
8
+ "node": ">=18.18.0"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/nimibyte/auditauth-sdk.git"
13
+ },
14
+ "homepage": "https://docs.auditauth.com",
15
+ "bugs": {
16
+ "url": "https://github.com/nimibyte/auditauth-sdk/issues"
17
+ },
18
+ "keywords": [
19
+ "authentication",
20
+ "auth",
21
+ "oauth",
22
+ "identity",
23
+ "jwt",
24
+ "security",
25
+ "auditauth"
26
+ ],
27
+ "main": "dist/index.js",
28
+ "types": "dist/index.d.ts",
29
+ "files": [
30
+ "dist"
31
+ ],
32
+ "sideEffects": false,
33
+ "exports": {
34
+ ".": {
35
+ "types": "./dist/index.d.ts",
36
+ "default": "./dist/index.js"
37
+ },
38
+ "./package.json": "./package.json"
39
+ },
40
+ "scripts": {
41
+ "build": "tsc -p tsconfig.build.json",
42
+ "dev": "tsc -p tsconfig.build.json --watch"
43
+ }
44
+ }