@tenxyte/core 0.1.5 → 0.9.2

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.
@@ -1,122 +0,0 @@
1
- import { TenxyteHttpClient } from '../http/client';
2
- import { TokenPair } from '../types';
3
-
4
- export interface OtpRequestParams {
5
- email?: string;
6
- phone_country_code?: string;
7
- phone_number?: string;
8
- type: 'email_verification' | 'phone_verification' | 'password_reset';
9
- }
10
-
11
- export interface VerifyOtpEmailParams {
12
- email: string;
13
- code: string;
14
- }
15
-
16
- export interface VerifyOtpPhoneParams {
17
- phone_country_code: string;
18
- phone_number: string;
19
- code: string;
20
- }
21
-
22
- export interface Setup2FAResponse {
23
- qr_code_url: string;
24
- secret: string;
25
- backup_codes: string[];
26
- }
27
-
28
- export interface WebAuthnRegisterBeginResponse {
29
- publicKey: any; // CredentialCreationOptions
30
- }
31
-
32
- export interface WebAuthnAuthenticateBeginResponse {
33
- publicKey: any; // CredentialRequestOptions
34
- }
35
-
36
- export class SecurityModule {
37
- constructor(private client: TenxyteHttpClient) { }
38
-
39
- // --- OTP Verification --- //
40
-
41
- async requestOtp(data: OtpRequestParams): Promise<void> {
42
- return this.client.post<void>('/api/v1/auth/otp/request/', data);
43
- }
44
-
45
- async verifyOtpEmail(data: VerifyOtpEmailParams): Promise<void> {
46
- return this.client.post<void>('/api/v1/auth/otp/verify/email/', data);
47
- }
48
-
49
- async verifyOtpPhone(data: VerifyOtpPhoneParams): Promise<void> {
50
- return this.client.post<void>('/api/v1/auth/otp/verify/phone/', data);
51
- }
52
-
53
- // --- TOTP / 2FA --- //
54
-
55
- async get2FAStatus(): Promise<{ is_enabled: boolean; backup_codes_remaining: number }> {
56
- return this.client.get('/api/v1/auth/2fa/status/');
57
- }
58
-
59
- async setup2FA(): Promise<Setup2FAResponse> {
60
- return this.client.post<Setup2FAResponse>('/api/v1/auth/2fa/setup/');
61
- }
62
-
63
- async confirm2FA(totp_code: string): Promise<void> {
64
- return this.client.post<void>('/api/v1/auth/2fa/confirm/', { totp_code });
65
- }
66
-
67
- async disable2FA(totp_code: string, password?: string): Promise<void> {
68
- return this.client.post<void>('/api/v1/auth/2fa/disable/', { totp_code, password });
69
- }
70
-
71
- async regenerateBackupCodes(totp_code: string): Promise<{ backup_codes: string[] }> {
72
- return this.client.post('/api/v1/auth/2fa/backup-codes/', { totp_code });
73
- }
74
-
75
- // --- Password Management --- //
76
-
77
- async resetPasswordRequest(data: { email?: string; phone_country_code?: string; phone_number?: string }): Promise<void> {
78
- return this.client.post<void>('/api/v1/auth/password/reset/request/', data);
79
- }
80
-
81
- async resetPasswordConfirm(data: { otp_code: string; new_password: string; email?: string; phone_country_code?: string; phone_number?: string }): Promise<void> {
82
- return this.client.post<void>('/api/v1/auth/password/reset/confirm/', data);
83
- }
84
-
85
- async changePassword(data: { current_password: string; new_password: string }): Promise<void> {
86
- return this.client.post<void>('/api/v1/auth/password/change/', data);
87
- }
88
-
89
- async checkPasswordStrength(data: { password: string; email?: string }): Promise<{ score: number; feedback: string[] }> {
90
- return this.client.post('/api/v1/auth/password/strength/', data);
91
- }
92
-
93
- async getPasswordRequirements(): Promise<any> {
94
- return this.client.get('/api/v1/auth/password/requirements/');
95
- }
96
-
97
- // --- WebAuthn / Passkeys --- //
98
-
99
- async registerWebAuthnBegin(): Promise<WebAuthnRegisterBeginResponse> {
100
- return this.client.post<WebAuthnRegisterBeginResponse>('/api/v1/auth/webauthn/register/begin/');
101
- }
102
-
103
- async registerWebAuthnComplete(data: any): Promise<void> {
104
- return this.client.post<void>('/api/v1/auth/webauthn/register/complete/', data);
105
- }
106
-
107
- async authenticateWebAuthnBegin(data?: { email?: string }): Promise<WebAuthnAuthenticateBeginResponse> {
108
- return this.client.post<WebAuthnAuthenticateBeginResponse>('/api/v1/auth/webauthn/authenticate/begin/', data || {});
109
- }
110
-
111
- async authenticateWebAuthnComplete(data: any): Promise<TokenPair> {
112
- return this.client.post<TokenPair>('/api/v1/auth/webauthn/authenticate/complete/', data);
113
- }
114
-
115
- async listWebAuthnCredentials(): Promise<any[]> {
116
- return this.client.get<any[]>('/api/v1/auth/webauthn/credentials/');
117
- }
118
-
119
- async deleteWebAuthnCredential(credentialId: string): Promise<void> {
120
- return this.client.delete<void>(`/api/v1/auth/webauthn/credentials/${credentialId}/`);
121
- }
122
- }
@@ -1,80 +0,0 @@
1
- import { TenxyteHttpClient } from '../http/client';
2
-
3
- export interface UpdateProfileParams {
4
- first_name?: string;
5
- last_name?: string;
6
- [key: string]: any; // Allow custom metadata updates
7
- }
8
-
9
- export interface AdminUpdateUserParams {
10
- first_name?: string;
11
- last_name?: string;
12
- is_active?: boolean;
13
- is_locked?: boolean;
14
- max_sessions?: number;
15
- max_devices?: number;
16
- }
17
-
18
- export class UserModule {
19
- constructor(private client: TenxyteHttpClient) { }
20
-
21
- // --- Standard Profile Actions --- //
22
-
23
- async getProfile(): Promise<any> {
24
- return this.client.get('/api/v1/auth/me/');
25
- }
26
-
27
- async updateProfile(data: UpdateProfileParams): Promise<any> {
28
- return this.client.patch('/api/v1/auth/me/', data);
29
- }
30
-
31
- /**
32
- * Upload an avatar using FormData.
33
- * Ensure the environment supports FormData (browser or Node.js v18+).
34
- * @param formData The FormData object containing the 'avatar' field.
35
- */
36
- async uploadAvatar(formData: FormData): Promise<any> {
37
- return this.client.patch('/api/v1/auth/me/', formData);
38
- }
39
-
40
- async deleteAccount(password: string, otpCode?: string): Promise<void> {
41
- return this.client.post<void>('/api/v1/auth/request-account-deletion/', {
42
- password,
43
- otp_code: otpCode
44
- });
45
- }
46
-
47
- // --- Admin Actions Mapping --- //
48
-
49
- async listUsers(params?: Record<string, any>): Promise<any[]> {
50
- return this.client.get<any[]>('/api/v1/auth/admin/users/', { params });
51
- }
52
-
53
- async getUser(userId: string): Promise<any> {
54
- return this.client.get(`/api/v1/auth/admin/users/${userId}/`);
55
- }
56
-
57
- async adminUpdateUser(userId: string, data: AdminUpdateUserParams): Promise<any> {
58
- return this.client.patch(`/api/v1/auth/admin/users/${userId}/`, data);
59
- }
60
-
61
- async adminDeleteUser(userId: string): Promise<void> {
62
- return this.client.delete<void>(`/api/v1/auth/admin/users/${userId}/`);
63
- }
64
-
65
- async banUser(userId: string, reason: string = ''): Promise<void> {
66
- return this.client.post<void>(`/api/v1/auth/admin/users/${userId}/ban/`, { reason });
67
- }
68
-
69
- async unbanUser(userId: string): Promise<void> {
70
- return this.client.post<void>(`/api/v1/auth/admin/users/${userId}/unban/`);
71
- }
72
-
73
- async lockUser(userId: string, durationMinutes: number = 30, reason: string = ''): Promise<void> {
74
- return this.client.post<void>(`/api/v1/auth/admin/users/${userId}/lock/`, { duration_minutes: durationMinutes, reason });
75
- }
76
-
77
- async unlockUser(userId: string): Promise<void> {
78
- return this.client.post<void>(`/api/v1/auth/admin/users/${userId}/unlock/`);
79
- }
80
- }
@@ -1,39 +0,0 @@
1
- import type { TenxyteStorage } from './index';
2
-
3
- /**
4
- * CookieStorage implementation
5
- * Note: To be secure, tokens should be HttpOnly where possible.
6
- * This class handles client-side cookies if necessary.
7
- */
8
- export class CookieStorage implements TenxyteStorage {
9
- private defaultOptions: string;
10
-
11
- constructor(options: { secure?: boolean; sameSite?: 'Strict' | 'Lax' | 'None' } = {}) {
12
- const secure = options.secure ?? true;
13
- const sameSite = options.sameSite ?? 'Lax';
14
- this.defaultOptions = `path=/; SameSite=${sameSite}${secure ? '; Secure' : ''}`;
15
- }
16
-
17
- getItem(key: string): string | null {
18
- if (typeof document === 'undefined') return null;
19
- const match = document.cookie.match(new RegExp(`(^| )${key}=([^;]+)`));
20
- return match ? decodeURIComponent(match[2]) : null;
21
- }
22
-
23
- setItem(key: string, value: string): void {
24
- if (typeof document === 'undefined') return;
25
- document.cookie = `${key}=${encodeURIComponent(value)}; ${this.defaultOptions}`;
26
- }
27
-
28
- removeItem(key: string): void {
29
- if (typeof document === 'undefined') return;
30
- document.cookie = `${key}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;
31
- }
32
-
33
- clear(): void {
34
- // Cannot easily clear all cookies securely because we don't know them all
35
- // Usually auth keys are known, e.g., tx_access, tx_refresh
36
- this.removeItem('tx_access');
37
- this.removeItem('tx_refresh');
38
- }
39
- }
@@ -1,29 +0,0 @@
1
- export interface TenxyteStorage {
2
- /**
3
- * Retrieves a value from storage.
4
- * @param key The key to retrieve
5
- */
6
- getItem(key: string): string | null | Promise<string | null>;
7
-
8
- /**
9
- * Saves a value to storage.
10
- * @param key The key to store
11
- * @param value The string value
12
- */
13
- setItem(key: string, value: string): void | Promise<void>;
14
-
15
- /**
16
- * Removes a specific key from storage.
17
- * @param key The key to remove
18
- */
19
- removeItem(key: string): void | Promise<void>;
20
-
21
- /**
22
- * Clears all storage keys managed by the SDK.
23
- */
24
- clear(): void | Promise<void>;
25
- }
26
-
27
- export * from './memory';
28
- export * from './localStorage';
29
- export * from './cookie';
@@ -1,75 +0,0 @@
1
- import type { TenxyteStorage } from './index';
2
- import { MemoryStorage } from './memory';
3
-
4
- /**
5
- * LocalStorage wrapper for the browser.
6
- * Degrades gracefully to MemoryStorage if localStorage is unavailable
7
- * (e.g., SSR, Private Browsing mode strictness).
8
- */
9
- export class LocalStorage implements TenxyteStorage {
10
- private fallbackMemoryStore: MemoryStorage | null = null;
11
- private isAvailable: boolean;
12
-
13
- constructor() {
14
- this.isAvailable = this.checkAvailability();
15
- if (!this.isAvailable) {
16
- this.fallbackMemoryStore = new MemoryStorage();
17
- }
18
- }
19
-
20
- private checkAvailability(): boolean {
21
- try {
22
- if (typeof window === 'undefined' || !window.localStorage) {
23
- return false;
24
- }
25
- const testKey = '__tenxyte_test__';
26
- window.localStorage.setItem(testKey, '1');
27
- window.localStorage.removeItem(testKey);
28
- return true;
29
- } catch (e) {
30
- return false;
31
- }
32
- }
33
-
34
- getItem(key: string): string | null {
35
- if (!this.isAvailable && this.fallbackMemoryStore) {
36
- return this.fallbackMemoryStore.getItem(key);
37
- }
38
- return window.localStorage.getItem(key);
39
- }
40
-
41
- setItem(key: string, value: string): void {
42
- if (!this.isAvailable && this.fallbackMemoryStore) {
43
- this.fallbackMemoryStore.setItem(key, value);
44
- return;
45
- }
46
- try {
47
- window.localStorage.setItem(key, value);
48
- } catch (e) {
49
- // Storage quota exceeded or similar error
50
- console.warn(`[Tenxyte SDK] Warning: failed to write to localStorage for key ${key}`);
51
- }
52
- }
53
-
54
- removeItem(key: string): void {
55
- if (!this.isAvailable && this.fallbackMemoryStore) {
56
- this.fallbackMemoryStore.removeItem(key);
57
- return;
58
- }
59
- window.localStorage.removeItem(key);
60
- }
61
-
62
- clear(): void {
63
- if (!this.isAvailable && this.fallbackMemoryStore) {
64
- this.fallbackMemoryStore.clear();
65
- return;
66
- }
67
- // We ideally only clear tenxyte specific keys if needed,
68
- // but standard clear() removes everything.
69
- // If the library only ever writes specific keys,
70
- // we could keep track of them and iterate, but for now clear() is standard.
71
- // For safer implementation we could just let the caller do removeItems() individually
72
- // but let's conform to the clear API.
73
- window.localStorage.clear();
74
- }
75
- }
@@ -1,30 +0,0 @@
1
- import type { TenxyteStorage } from './index';
2
-
3
- /**
4
- * MemoryStorage implementation primarily used in Node.js (SSR)
5
- * environments or as a fallback when browser storage is unavailable.
6
- */
7
- export class MemoryStorage implements TenxyteStorage {
8
- private store: Map<string, string>;
9
-
10
- constructor() {
11
- this.store = new Map<string, string>();
12
- }
13
-
14
- getItem(key: string): string | null {
15
- const value = this.store.get(key);
16
- return value !== undefined ? value : null;
17
- }
18
-
19
- setItem(key: string, value: string): void {
20
- this.store.set(key, value);
21
- }
22
-
23
- removeItem(key: string): void {
24
- this.store.delete(key);
25
- }
26
-
27
- clear(): void {
28
- this.store.clear();
29
- }
30
- }