@originals/auth 1.5.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/.turbo/turbo-build.log +1 -0
- package/dist/client/index.d.ts +22 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +22 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/turnkey-client.d.ts +53 -0
- package/dist/client/turnkey-client.d.ts.map +1 -0
- package/dist/client/turnkey-client.js +268 -0
- package/dist/client/turnkey-client.js.map +1 -0
- package/dist/client/turnkey-did-signer.d.ts +54 -0
- package/dist/client/turnkey-did-signer.d.ts.map +1 -0
- package/dist/client/turnkey-did-signer.js +125 -0
- package/dist/client/turnkey-did-signer.js.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/server/email-auth.d.ts +42 -0
- package/dist/server/email-auth.d.ts.map +1 -0
- package/dist/server/email-auth.js +187 -0
- package/dist/server/email-auth.js.map +1 -0
- package/dist/server/index.d.ts +22 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +22 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/jwt.d.ts +49 -0
- package/dist/server/jwt.d.ts.map +1 -0
- package/dist/server/jwt.js +113 -0
- package/dist/server/jwt.js.map +1 -0
- package/dist/server/middleware.d.ts +39 -0
- package/dist/server/middleware.d.ts.map +1 -0
- package/dist/server/middleware.js +110 -0
- package/dist/server/middleware.js.map +1 -0
- package/dist/server/turnkey-client.d.ts +24 -0
- package/dist/server/turnkey-client.d.ts.map +1 -0
- package/dist/server/turnkey-client.js +118 -0
- package/dist/server/turnkey-client.js.map +1 -0
- package/dist/server/turnkey-signer.d.ts +40 -0
- package/dist/server/turnkey-signer.d.ts.map +1 -0
- package/dist/server/turnkey-signer.js +121 -0
- package/dist/server/turnkey-signer.js.map +1 -0
- package/dist/types.d.ts +155 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/package.json +79 -0
- package/src/client/index.ts +37 -0
- package/src/client/turnkey-client.ts +340 -0
- package/src/client/turnkey-did-signer.ts +189 -0
- package/src/index.ts +32 -0
- package/src/server/email-auth.ts +258 -0
- package/src/server/index.ts +38 -0
- package/src/server/jwt.ts +154 -0
- package/src/server/middleware.ts +136 -0
- package/src/server/turnkey-client.ts +152 -0
- package/src/server/turnkey-signer.ts +170 -0
- package/src/types.ts +168 -0
- package/tests/index.test.ts +25 -0
- package/tsconfig.json +28 -0
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-side Turnkey client utilities
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { Turnkey } from '@turnkey/sdk-server';
|
|
6
|
+
|
|
7
|
+
export interface TurnkeyClientConfig {
|
|
8
|
+
/** Turnkey API base URL (default: https://api.turnkey.com) */
|
|
9
|
+
apiBaseUrl?: string;
|
|
10
|
+
/** Turnkey API public key */
|
|
11
|
+
apiPublicKey: string;
|
|
12
|
+
/** Turnkey API private key */
|
|
13
|
+
apiPrivateKey: string;
|
|
14
|
+
/** Default organization ID */
|
|
15
|
+
organizationId: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Create a Turnkey server client
|
|
20
|
+
*/
|
|
21
|
+
export function createTurnkeyClient(config?: Partial<TurnkeyClientConfig>): Turnkey {
|
|
22
|
+
const apiPublicKey = config?.apiPublicKey ?? process.env.TURNKEY_API_PUBLIC_KEY;
|
|
23
|
+
const apiPrivateKey = config?.apiPrivateKey ?? process.env.TURNKEY_API_PRIVATE_KEY;
|
|
24
|
+
const organizationId = config?.organizationId ?? process.env.TURNKEY_ORGANIZATION_ID;
|
|
25
|
+
|
|
26
|
+
if (!apiPublicKey) {
|
|
27
|
+
throw new Error('TURNKEY_API_PUBLIC_KEY is required');
|
|
28
|
+
}
|
|
29
|
+
if (!apiPrivateKey) {
|
|
30
|
+
throw new Error('TURNKEY_API_PRIVATE_KEY is required');
|
|
31
|
+
}
|
|
32
|
+
if (!organizationId) {
|
|
33
|
+
throw new Error('TURNKEY_ORGANIZATION_ID is required');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return new Turnkey({
|
|
37
|
+
apiBaseUrl: config?.apiBaseUrl ?? 'https://api.turnkey.com',
|
|
38
|
+
apiPublicKey,
|
|
39
|
+
apiPrivateKey,
|
|
40
|
+
defaultOrganizationId: organizationId,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Get or create a Turnkey sub-organization for a user
|
|
46
|
+
* Creates sub-org with email-only root user and required wallet accounts
|
|
47
|
+
*/
|
|
48
|
+
export async function getOrCreateTurnkeySubOrg(
|
|
49
|
+
email: string,
|
|
50
|
+
turnkeyClient: Turnkey
|
|
51
|
+
): Promise<string> {
|
|
52
|
+
const organizationId = process.env.TURNKEY_ORGANIZATION_ID;
|
|
53
|
+
if (!organizationId) {
|
|
54
|
+
throw new Error('TURNKEY_ORGANIZATION_ID is required');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Generate a consistent base name for lookup
|
|
58
|
+
const baseSubOrgName = `user-${email.replace(/[^a-z0-9]/gi, '-').toLowerCase()}`;
|
|
59
|
+
|
|
60
|
+
console.log(`🔍 Checking for existing sub-organization for ${email}...`);
|
|
61
|
+
|
|
62
|
+
try {
|
|
63
|
+
// Try to get existing sub-organizations by email filter
|
|
64
|
+
const subOrgs = await turnkeyClient.apiClient().getSubOrgIds({
|
|
65
|
+
organizationId,
|
|
66
|
+
filterType: 'EMAIL',
|
|
67
|
+
filterValue: email,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const subOrgIds = subOrgs.organizationIds || [];
|
|
71
|
+
const existingSubOrgId = subOrgIds.length > 0 ? subOrgIds[0] : null;
|
|
72
|
+
|
|
73
|
+
if (existingSubOrgId) {
|
|
74
|
+
console.log(`✅ Found existing sub-organization: ${existingSubOrgId}`);
|
|
75
|
+
|
|
76
|
+
// Check if this sub-org has a wallet
|
|
77
|
+
try {
|
|
78
|
+
const walletsCheck = await turnkeyClient.apiClient().getWallets({
|
|
79
|
+
organizationId: existingSubOrgId,
|
|
80
|
+
});
|
|
81
|
+
const walletCount = walletsCheck.wallets?.length || 0;
|
|
82
|
+
|
|
83
|
+
if (walletCount > 0) {
|
|
84
|
+
return existingSubOrgId;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
console.log(`⚠️ Sub-org has no wallet, creating new sub-org with wallet...`);
|
|
88
|
+
} catch (walletCheckErr) {
|
|
89
|
+
console.error('Could not check wallet in sub-org:', walletCheckErr);
|
|
90
|
+
return existingSubOrgId;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
} catch (lookupError) {
|
|
94
|
+
console.log(`📝 No existing sub-org found, will create new one`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Generate a unique name for the new sub-org
|
|
98
|
+
const subOrgName = `${baseSubOrgName}-${Date.now()}`;
|
|
99
|
+
|
|
100
|
+
console.log(`📧 Creating new Turnkey sub-organization for ${email}...`);
|
|
101
|
+
|
|
102
|
+
// Create sub-organization with wallet containing required keys
|
|
103
|
+
const result = await turnkeyClient.apiClient().createSubOrganization({
|
|
104
|
+
subOrganizationName: subOrgName,
|
|
105
|
+
rootUsers: [
|
|
106
|
+
{
|
|
107
|
+
userName: email,
|
|
108
|
+
userEmail: email,
|
|
109
|
+
apiKeys: [],
|
|
110
|
+
authenticators: [],
|
|
111
|
+
oauthProviders: [],
|
|
112
|
+
},
|
|
113
|
+
],
|
|
114
|
+
rootQuorumThreshold: 1,
|
|
115
|
+
wallet: {
|
|
116
|
+
walletName: 'default-wallet',
|
|
117
|
+
accounts: [
|
|
118
|
+
{
|
|
119
|
+
curve: 'CURVE_SECP256K1',
|
|
120
|
+
pathFormat: 'PATH_FORMAT_BIP32',
|
|
121
|
+
path: "m/44'/0'/0'/0/0", // Bitcoin path for auth-key
|
|
122
|
+
addressFormat: 'ADDRESS_FORMAT_ETHEREUM',
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
curve: 'CURVE_ED25519',
|
|
126
|
+
pathFormat: 'PATH_FORMAT_BIP32',
|
|
127
|
+
path: "m/44'/501'/0'/0'", // Ed25519 for assertion-key
|
|
128
|
+
addressFormat: 'ADDRESS_FORMAT_SOLANA',
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
curve: 'CURVE_ED25519',
|
|
132
|
+
pathFormat: 'PATH_FORMAT_BIP32',
|
|
133
|
+
path: "m/44'/501'/1'/0'", // Ed25519 for update-key
|
|
134
|
+
addressFormat: 'ADDRESS_FORMAT_SOLANA',
|
|
135
|
+
},
|
|
136
|
+
],
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
const subOrgId = result.activity?.result?.createSubOrganizationResultV7?.subOrganizationId;
|
|
141
|
+
|
|
142
|
+
if (!subOrgId) {
|
|
143
|
+
throw new Error('No sub-organization ID returned from Turnkey');
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
console.log(`✅ Created sub-organization: ${subOrgId}`);
|
|
147
|
+
|
|
148
|
+
return subOrgId;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turnkey Signer - Integration between Turnkey key management and Originals SDK
|
|
3
|
+
*
|
|
4
|
+
* Provides an ExternalSigner implementation that works with Turnkey-managed
|
|
5
|
+
* keys for use with the Originals SDK's DID creation and signing operations.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { Turnkey } from '@turnkey/sdk-server';
|
|
9
|
+
import { ExternalSigner, ExternalVerifier, multikey, OriginalsSDK } from '@originals/sdk';
|
|
10
|
+
import { sha512 } from '@noble/hashes/sha2.js';
|
|
11
|
+
import { concatBytes, bytesToHex } from '@noble/hashes/utils.js';
|
|
12
|
+
import * as ed25519 from '@noble/ed25519';
|
|
13
|
+
|
|
14
|
+
// Configure @noble/ed25519 with required SHA-512 function
|
|
15
|
+
const sha512Fn = (...msgs: Uint8Array[]): Uint8Array => sha512(concatBytes(...msgs));
|
|
16
|
+
|
|
17
|
+
// Initialize Ed25519 configuration
|
|
18
|
+
try {
|
|
19
|
+
const ed25519Module = ed25519 as unknown as {
|
|
20
|
+
utils?: { sha512Sync?: typeof sha512Fn };
|
|
21
|
+
etc?: { sha512Sync?: typeof sha512Fn };
|
|
22
|
+
};
|
|
23
|
+
if (ed25519Module.utils) {
|
|
24
|
+
ed25519Module.utils.sha512Sync = sha512Fn;
|
|
25
|
+
}
|
|
26
|
+
if (ed25519Module.etc) {
|
|
27
|
+
ed25519Module.etc.sha512Sync = sha512Fn;
|
|
28
|
+
}
|
|
29
|
+
} catch (error) {
|
|
30
|
+
console.warn('Failed to configure ed25519 utils:', error);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Turnkey-based signer for use with Originals SDK
|
|
35
|
+
* Implements the ExternalSigner and ExternalVerifier interfaces
|
|
36
|
+
*/
|
|
37
|
+
export class TurnkeyWebVHSigner implements ExternalSigner, ExternalVerifier {
|
|
38
|
+
private subOrgId: string;
|
|
39
|
+
private keyId: string;
|
|
40
|
+
private publicKeyMultibase: string;
|
|
41
|
+
private turnkeyClient: Turnkey;
|
|
42
|
+
private verificationMethodId: string;
|
|
43
|
+
|
|
44
|
+
constructor(
|
|
45
|
+
subOrgId: string,
|
|
46
|
+
keyId: string,
|
|
47
|
+
publicKeyMultibase: string,
|
|
48
|
+
turnkeyClient: Turnkey,
|
|
49
|
+
verificationMethodId: string
|
|
50
|
+
) {
|
|
51
|
+
this.subOrgId = subOrgId;
|
|
52
|
+
this.keyId = keyId;
|
|
53
|
+
this.publicKeyMultibase = publicKeyMultibase;
|
|
54
|
+
this.turnkeyClient = turnkeyClient;
|
|
55
|
+
this.verificationMethodId = verificationMethodId;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Sign data using Turnkey's API
|
|
60
|
+
*/
|
|
61
|
+
async sign(input: {
|
|
62
|
+
document: Record<string, unknown>;
|
|
63
|
+
proof: Record<string, unknown>;
|
|
64
|
+
}): Promise<{ proofValue: string }> {
|
|
65
|
+
try {
|
|
66
|
+
// Prepare the data for signing using the SDK's canonical approach
|
|
67
|
+
const dataToSign = await OriginalsSDK.prepareDIDDataForSigning(input.document, input.proof);
|
|
68
|
+
|
|
69
|
+
// Convert canonical data to hex format for Turnkey's sign API
|
|
70
|
+
const dataHex = `0x${bytesToHex(dataToSign)}`;
|
|
71
|
+
|
|
72
|
+
// Sign using Turnkey's API
|
|
73
|
+
const result = await this.turnkeyClient.apiClient().signRawPayload({
|
|
74
|
+
organizationId: this.subOrgId,
|
|
75
|
+
signWith: this.keyId,
|
|
76
|
+
payload: dataHex,
|
|
77
|
+
encoding: 'PAYLOAD_ENCODING_HEXADECIMAL',
|
|
78
|
+
hashFunction: 'HASH_FUNCTION_NO_OP',
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const signRawResult = result.activity?.result?.signRawPayloadResult;
|
|
82
|
+
if (!signRawResult?.r || !signRawResult?.s) {
|
|
83
|
+
throw new Error('No signature returned from Turnkey');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const signature = signRawResult.r + signRawResult.s;
|
|
87
|
+
|
|
88
|
+
// Convert signature to bytes
|
|
89
|
+
const cleanSig = signature.startsWith('0x') ? signature.slice(2) : signature;
|
|
90
|
+
let signatureBytes = Buffer.from(cleanSig, 'hex');
|
|
91
|
+
|
|
92
|
+
// Ed25519 signatures should be exactly 64 bytes
|
|
93
|
+
if (signatureBytes.length === 65) {
|
|
94
|
+
signatureBytes = signatureBytes.slice(0, 64);
|
|
95
|
+
} else if (signatureBytes.length !== 64) {
|
|
96
|
+
throw new Error(
|
|
97
|
+
`Invalid Ed25519 signature length: ${signatureBytes.length} (expected 64 bytes)`
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Encode signature as multibase
|
|
102
|
+
const proofValue = multikey.encodeMultibase(signatureBytes);
|
|
103
|
+
return { proofValue };
|
|
104
|
+
} catch (error) {
|
|
105
|
+
console.error('Error signing with Turnkey:', error);
|
|
106
|
+
throw new Error(
|
|
107
|
+
`Failed to sign with Turnkey: ${error instanceof Error ? error.message : String(error)}`
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Verify a signature
|
|
114
|
+
*/
|
|
115
|
+
async verify(
|
|
116
|
+
signature: Uint8Array,
|
|
117
|
+
message: Uint8Array,
|
|
118
|
+
publicKey: Uint8Array
|
|
119
|
+
): Promise<boolean> {
|
|
120
|
+
try {
|
|
121
|
+
// Ed25519 public keys must be exactly 32 bytes
|
|
122
|
+
let ed25519PublicKey = publicKey;
|
|
123
|
+
if (publicKey.length === 33) {
|
|
124
|
+
ed25519PublicKey = publicKey.slice(1);
|
|
125
|
+
} else if (publicKey.length !== 32) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const ed25519Module = ed25519 as unknown as {
|
|
130
|
+
utils?: { sha512Sync?: typeof sha512Fn };
|
|
131
|
+
};
|
|
132
|
+
if (typeof ed25519Module.utils?.sha512Sync !== 'function') {
|
|
133
|
+
ed25519Module.utils!.sha512Sync = sha512Fn;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return await ed25519.verify(signature, message, ed25519PublicKey);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
console.error('Error verifying signature:', error);
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
getVerificationMethodId(): string {
|
|
144
|
+
return this.verificationMethodId;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
getPublicKeyMultibase(): string {
|
|
148
|
+
return this.publicKeyMultibase;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Create a Turnkey signer for use with the Originals SDK
|
|
154
|
+
*/
|
|
155
|
+
export async function createTurnkeySigner(
|
|
156
|
+
subOrgId: string,
|
|
157
|
+
keyId: string,
|
|
158
|
+
turnkeyClient: Turnkey,
|
|
159
|
+
verificationMethodId: string,
|
|
160
|
+
publicKeyMultibase: string
|
|
161
|
+
): Promise<TurnkeyWebVHSigner> {
|
|
162
|
+
return new TurnkeyWebVHSigner(
|
|
163
|
+
subOrgId,
|
|
164
|
+
keyId,
|
|
165
|
+
publicKeyMultibase,
|
|
166
|
+
turnkeyClient,
|
|
167
|
+
verificationMethodId
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared types for @originals/auth
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Authenticated user information
|
|
7
|
+
*/
|
|
8
|
+
export interface AuthUser {
|
|
9
|
+
/** Database user ID */
|
|
10
|
+
id: string;
|
|
11
|
+
/** User's email address */
|
|
12
|
+
email: string;
|
|
13
|
+
/** User's DID identifier */
|
|
14
|
+
did: string;
|
|
15
|
+
/** Turnkey sub-organization ID */
|
|
16
|
+
turnkeySubOrgId: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* JWT token payload structure
|
|
21
|
+
*/
|
|
22
|
+
export interface TokenPayload {
|
|
23
|
+
/** Subject - Turnkey sub-organization ID (stable identifier) */
|
|
24
|
+
sub: string;
|
|
25
|
+
/** User email (metadata) */
|
|
26
|
+
email: string;
|
|
27
|
+
/** Optional Turnkey session token for user authentication */
|
|
28
|
+
sessionToken?: string;
|
|
29
|
+
/** Issued at timestamp */
|
|
30
|
+
iat: number;
|
|
31
|
+
/** Expiration timestamp */
|
|
32
|
+
exp: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Options for creating auth middleware
|
|
37
|
+
*/
|
|
38
|
+
export interface AuthMiddlewareOptions {
|
|
39
|
+
/** Function to look up user by Turnkey sub-org ID */
|
|
40
|
+
getUserByTurnkeyId: (turnkeyId: string) => Promise<AuthUser | null>;
|
|
41
|
+
/** Optional function to create user on first auth */
|
|
42
|
+
createUser?: (turnkeyId: string, email: string, temporaryDid: string) => Promise<AuthUser>;
|
|
43
|
+
/** Cookie name for JWT token (default: 'auth_token') */
|
|
44
|
+
cookieName?: string;
|
|
45
|
+
/** JWT secret (default: process.env.JWT_SECRET) */
|
|
46
|
+
jwtSecret?: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Email authentication session
|
|
51
|
+
*/
|
|
52
|
+
export interface EmailAuthSession {
|
|
53
|
+
/** User's email address */
|
|
54
|
+
email: string;
|
|
55
|
+
/** Turnkey sub-organization ID */
|
|
56
|
+
subOrgId?: string;
|
|
57
|
+
/** Turnkey OTP ID */
|
|
58
|
+
otpId?: string;
|
|
59
|
+
/** Session creation timestamp */
|
|
60
|
+
timestamp: number;
|
|
61
|
+
/** Whether the session has been verified */
|
|
62
|
+
verified: boolean;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Result of initiating email authentication
|
|
67
|
+
*/
|
|
68
|
+
export interface InitiateAuthResult {
|
|
69
|
+
/** Session ID for verification step */
|
|
70
|
+
sessionId: string;
|
|
71
|
+
/** User-friendly message */
|
|
72
|
+
message: string;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Result of verifying email authentication
|
|
77
|
+
*/
|
|
78
|
+
export interface VerifyAuthResult {
|
|
79
|
+
/** Whether verification was successful */
|
|
80
|
+
verified: boolean;
|
|
81
|
+
/** User's email address */
|
|
82
|
+
email: string;
|
|
83
|
+
/** Turnkey sub-organization ID */
|
|
84
|
+
subOrgId: string;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Cookie configuration for auth tokens
|
|
89
|
+
*/
|
|
90
|
+
export interface AuthCookieConfig {
|
|
91
|
+
/** Cookie name */
|
|
92
|
+
name: string;
|
|
93
|
+
/** Cookie value (JWT token) */
|
|
94
|
+
value: string;
|
|
95
|
+
/** Cookie options */
|
|
96
|
+
options: {
|
|
97
|
+
httpOnly: boolean;
|
|
98
|
+
secure: boolean;
|
|
99
|
+
sameSite: 'strict' | 'lax' | 'none';
|
|
100
|
+
maxAge: number;
|
|
101
|
+
path: string;
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Turnkey wallet information
|
|
107
|
+
*/
|
|
108
|
+
export interface TurnkeyWallet {
|
|
109
|
+
/** Wallet ID */
|
|
110
|
+
walletId: string;
|
|
111
|
+
/** Wallet name */
|
|
112
|
+
walletName: string;
|
|
113
|
+
/** Wallet accounts */
|
|
114
|
+
accounts: TurnkeyWalletAccount[];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Turnkey wallet account
|
|
119
|
+
*/
|
|
120
|
+
export interface TurnkeyWalletAccount {
|
|
121
|
+
/** Account address */
|
|
122
|
+
address: string;
|
|
123
|
+
/** Cryptographic curve */
|
|
124
|
+
curve: 'CURVE_SECP256K1' | 'CURVE_ED25519';
|
|
125
|
+
/** Derivation path */
|
|
126
|
+
path: string;
|
|
127
|
+
/** Address format */
|
|
128
|
+
addressFormat: string;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Client-side Turnkey authentication state
|
|
133
|
+
*/
|
|
134
|
+
export interface TurnkeyAuthState {
|
|
135
|
+
/** Whether the user is authenticated */
|
|
136
|
+
isAuthenticated: boolean;
|
|
137
|
+
/** Whether an auth operation is in progress */
|
|
138
|
+
isLoading: boolean;
|
|
139
|
+
/** Error message if any */
|
|
140
|
+
error: string | null;
|
|
141
|
+
/** User's email address */
|
|
142
|
+
email: string | null;
|
|
143
|
+
/** User's wallets */
|
|
144
|
+
wallets: TurnkeyWallet[];
|
|
145
|
+
/** OTP ID for verification step */
|
|
146
|
+
otpId: string | null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Request context with authenticated user
|
|
151
|
+
*/
|
|
152
|
+
export interface AuthenticatedRequest {
|
|
153
|
+
user: {
|
|
154
|
+
/** Database user ID */
|
|
155
|
+
id: string;
|
|
156
|
+
/** Turnkey sub-organization ID */
|
|
157
|
+
turnkeySubOrgId: string;
|
|
158
|
+
/** User's email */
|
|
159
|
+
email: string;
|
|
160
|
+
/** User's DID */
|
|
161
|
+
did: string;
|
|
162
|
+
/** Turnkey session token (if available) */
|
|
163
|
+
sessionToken?: string;
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { describe, test, expect } from 'bun:test';
|
|
2
|
+
|
|
3
|
+
describe('@originals/auth', () => {
|
|
4
|
+
test('package exports are defined', async () => {
|
|
5
|
+
// Test that the main exports are accessible
|
|
6
|
+
const auth = await import('../src/index');
|
|
7
|
+
expect(auth).toBeDefined();
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
test('server exports are defined', async () => {
|
|
11
|
+
const server = await import('../src/server/index');
|
|
12
|
+
expect(server).toBeDefined();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test('client exports are defined', async () => {
|
|
16
|
+
const client = await import('../src/client/index');
|
|
17
|
+
expect(client).toBeDefined();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test('types are defined', async () => {
|
|
21
|
+
const types = await import('../src/types');
|
|
22
|
+
expect(types).toBeDefined();
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"declarationMap": true,
|
|
8
|
+
"sourceMap": true,
|
|
9
|
+
"outDir": "./dist",
|
|
10
|
+
"rootDir": "./src",
|
|
11
|
+
"strict": true,
|
|
12
|
+
"esModuleInterop": true,
|
|
13
|
+
"skipLibCheck": true,
|
|
14
|
+
"forceConsistentCasingInFileNames": true,
|
|
15
|
+
"resolveJsonModule": true,
|
|
16
|
+
"isolatedModules": true,
|
|
17
|
+
"lib": ["ES2022", "DOM"],
|
|
18
|
+
"types": ["bun-types"],
|
|
19
|
+
"paths": {
|
|
20
|
+
"@/*": ["./src/*"]
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"include": ["src/**/*"],
|
|
24
|
+
"exclude": ["node_modules", "dist", "tests"]
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|