@0xsequence/waas
Advanced tools
Comparing version 0.0.0-20231108135717 to 0.0.0-20231108174744
@@ -10,2 +10,3 @@ 'use strict'; | ||
var clientKms = require('@aws-sdk/client-kms'); | ||
var clientCognitoIdentityProvider = require('@aws-sdk/client-cognito-identity-provider'); | ||
@@ -577,2 +578,3 @@ function _extends() { | ||
idpRegion: 'us-east-2', | ||
emailRegion: 'us-east-2', | ||
keyId: 'arn:aws:kms:us-east-1:000000000000:key/aeb99e0f-9e89-44de-a084-e1817af47778', | ||
@@ -585,2 +587,3 @@ endpoint: 'http://localstack:4566' | ||
idpRegion: 'us-east-2', | ||
emailRegion: 'us-east-2', | ||
keyId: 'arn:aws:kms:us-east-2:170768627592:key/0fd8f803-9cb5-4de5-86e4-41963fb6043d', | ||
@@ -1100,2 +1103,89 @@ endpoint: undefined | ||
function getRandomString(len) { | ||
const randomValues = new Uint8Array(len); | ||
window.crypto.getRandomValues(randomValues); | ||
return Array.from(randomValues).map(nr => nr.toString(16).padStart(2, '0')).join(''); | ||
} | ||
class EmailAuth { | ||
constructor(region, clientId) { | ||
this.region = region; | ||
this.clientId = clientId; | ||
} | ||
cognito() { | ||
if (!this.cognitoMemo) { | ||
this.cognitoMemo = new clientCognitoIdentityProvider.CognitoIdentityProviderClient({ | ||
region: this.region | ||
}); | ||
} | ||
return this.cognitoMemo; | ||
} | ||
signUp(email) { | ||
return this.cognito().send(new clientCognitoIdentityProvider.SignUpCommand({ | ||
ClientId: this.clientId, | ||
Username: email, | ||
Password: 'aB1%' + getRandomString(14), | ||
UserAttributes: [{ | ||
Name: 'email', | ||
Value: email | ||
}] | ||
})); | ||
} | ||
signIn(email) { | ||
return this.cognito().send(new clientCognitoIdentityProvider.InitiateAuthCommand({ | ||
AuthFlow: 'CUSTOM_AUTH', | ||
ClientId: this.clientId, | ||
AuthParameters: { | ||
USERNAME: email | ||
} | ||
})); | ||
} | ||
async initiateAuth({ | ||
}) { | ||
let res; | ||
try { | ||
// Try sign in directly first | ||
res = await this.signIn(email); | ||
} catch (e) { | ||
if (e instanceof clientCognitoIdentityProvider.UserLambdaValidationException && e.message.includes("user not found")) { | ||
// Sign up and sign in | ||
await this.signUp(email); | ||
res = await this.signIn(email); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
if (!res.Session) { | ||
throw new Error("response session is empty"); | ||
} | ||
return { | ||
// Notice: rename session to instance to avoid | ||
// confusion with the native waas session | ||
instance: res.Session, | ||
email: email | ||
}; | ||
} | ||
async finalizeAuth({ | ||
instance, | ||
email, | ||
answer | ||
}) { | ||
const res = await this.cognito().send(new clientCognitoIdentityProvider.RespondToAuthChallengeCommand({ | ||
ClientId: this.clientId, | ||
Session: instance, | ||
ChallengeName: 'CUSTOM_CHALLENGE', | ||
ChallengeResponses: { | ||
USERNAME: email, | ||
ANSWER: answer | ||
} | ||
})); | ||
if (!res.AuthenticationResult || !res.AuthenticationResult.IdToken) { | ||
throw new Error('AuthenticationResult.IdToken is empty'); | ||
} | ||
return { | ||
idToken: res.AuthenticationResult.IdToken | ||
}; | ||
} | ||
} | ||
function encodeHex(data) { | ||
@@ -1140,2 +1230,15 @@ return "0x" + Array.from(typeof data === 'string' ? new TextEncoder().encode(data) : data, byte => byte.toString(16).padStart(2, '0')).join(""); | ||
} | ||
get email() { | ||
if (this.emailClient) { | ||
return this.emailClient; | ||
} | ||
if (!this.config.emailRegion) { | ||
throw new Error('Missing emailRegion'); | ||
} | ||
if (!this.config.emailClientId) { | ||
throw new Error('Missing emailClientId'); | ||
} | ||
this.emailClient = new EmailAuth(this.config.emailRegion, this.config.emailClientId); | ||
return this.emailClient; | ||
} | ||
async onValidationRequired(callback) { | ||
@@ -1142,0 +1245,0 @@ this.validationRequiredCallback.push(callback); |
@@ -10,2 +10,3 @@ 'use strict'; | ||
var clientKms = require('@aws-sdk/client-kms'); | ||
var clientCognitoIdentityProvider = require('@aws-sdk/client-cognito-identity-provider'); | ||
@@ -577,2 +578,3 @@ function _extends() { | ||
idpRegion: 'us-east-2', | ||
emailRegion: 'us-east-2', | ||
keyId: 'arn:aws:kms:us-east-1:000000000000:key/aeb99e0f-9e89-44de-a084-e1817af47778', | ||
@@ -585,2 +587,3 @@ endpoint: 'http://localstack:4566' | ||
idpRegion: 'us-east-2', | ||
emailRegion: 'us-east-2', | ||
keyId: 'arn:aws:kms:us-east-2:170768627592:key/0fd8f803-9cb5-4de5-86e4-41963fb6043d', | ||
@@ -1100,2 +1103,89 @@ endpoint: undefined | ||
function getRandomString(len) { | ||
const randomValues = new Uint8Array(len); | ||
window.crypto.getRandomValues(randomValues); | ||
return Array.from(randomValues).map(nr => nr.toString(16).padStart(2, '0')).join(''); | ||
} | ||
class EmailAuth { | ||
constructor(region, clientId) { | ||
this.region = region; | ||
this.clientId = clientId; | ||
} | ||
cognito() { | ||
if (!this.cognitoMemo) { | ||
this.cognitoMemo = new clientCognitoIdentityProvider.CognitoIdentityProviderClient({ | ||
region: this.region | ||
}); | ||
} | ||
return this.cognitoMemo; | ||
} | ||
signUp(email) { | ||
return this.cognito().send(new clientCognitoIdentityProvider.SignUpCommand({ | ||
ClientId: this.clientId, | ||
Username: email, | ||
Password: 'aB1%' + getRandomString(14), | ||
UserAttributes: [{ | ||
Name: 'email', | ||
Value: email | ||
}] | ||
})); | ||
} | ||
signIn(email) { | ||
return this.cognito().send(new clientCognitoIdentityProvider.InitiateAuthCommand({ | ||
AuthFlow: 'CUSTOM_AUTH', | ||
ClientId: this.clientId, | ||
AuthParameters: { | ||
USERNAME: email | ||
} | ||
})); | ||
} | ||
async initiateAuth({ | ||
}) { | ||
let res; | ||
try { | ||
// Try sign in directly first | ||
res = await this.signIn(email); | ||
} catch (e) { | ||
if (e instanceof clientCognitoIdentityProvider.UserLambdaValidationException && e.message.includes("user not found")) { | ||
// Sign up and sign in | ||
await this.signUp(email); | ||
res = await this.signIn(email); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
if (!res.Session) { | ||
throw new Error("response session is empty"); | ||
} | ||
return { | ||
// Notice: rename session to instance to avoid | ||
// confusion with the native waas session | ||
instance: res.Session, | ||
email: email | ||
}; | ||
} | ||
async finalizeAuth({ | ||
instance, | ||
email, | ||
answer | ||
}) { | ||
const res = await this.cognito().send(new clientCognitoIdentityProvider.RespondToAuthChallengeCommand({ | ||
ClientId: this.clientId, | ||
Session: instance, | ||
ChallengeName: 'CUSTOM_CHALLENGE', | ||
ChallengeResponses: { | ||
USERNAME: email, | ||
ANSWER: answer | ||
} | ||
})); | ||
if (!res.AuthenticationResult || !res.AuthenticationResult.IdToken) { | ||
throw new Error('AuthenticationResult.IdToken is empty'); | ||
} | ||
return { | ||
idToken: res.AuthenticationResult.IdToken | ||
}; | ||
} | ||
} | ||
function encodeHex(data) { | ||
@@ -1140,2 +1230,15 @@ return "0x" + Array.from(typeof data === 'string' ? new TextEncoder().encode(data) : data, byte => byte.toString(16).padStart(2, '0')).join(""); | ||
} | ||
get email() { | ||
if (this.emailClient) { | ||
return this.emailClient; | ||
} | ||
if (!this.config.emailRegion) { | ||
throw new Error('Missing emailRegion'); | ||
} | ||
if (!this.config.emailClientId) { | ||
throw new Error('Missing emailClientId'); | ||
} | ||
this.emailClient = new EmailAuth(this.config.emailRegion, this.config.emailClientId); | ||
return this.emailClient; | ||
} | ||
async onValidationRequired(callback) { | ||
@@ -1142,0 +1245,0 @@ this.validationRequiredCallback.push(callback); |
@@ -6,2 +6,3 @@ import { ethers } from 'ethers'; | ||
import { GenerateDataKeyCommand, KMSClient } from '@aws-sdk/client-kms'; | ||
import { CognitoIdentityProviderClient, SignUpCommand, InitiateAuthCommand, UserLambdaValidationException, RespondToAuthChallengeCommand } from '@aws-sdk/client-cognito-identity-provider'; | ||
@@ -573,2 +574,3 @@ function _extends() { | ||
idpRegion: 'us-east-2', | ||
emailRegion: 'us-east-2', | ||
keyId: 'arn:aws:kms:us-east-1:000000000000:key/aeb99e0f-9e89-44de-a084-e1817af47778', | ||
@@ -581,2 +583,3 @@ endpoint: 'http://localstack:4566' | ||
idpRegion: 'us-east-2', | ||
emailRegion: 'us-east-2', | ||
keyId: 'arn:aws:kms:us-east-2:170768627592:key/0fd8f803-9cb5-4de5-86e4-41963fb6043d', | ||
@@ -1096,2 +1099,89 @@ endpoint: undefined | ||
function getRandomString(len) { | ||
const randomValues = new Uint8Array(len); | ||
window.crypto.getRandomValues(randomValues); | ||
return Array.from(randomValues).map(nr => nr.toString(16).padStart(2, '0')).join(''); | ||
} | ||
class EmailAuth { | ||
constructor(region, clientId) { | ||
this.region = region; | ||
this.clientId = clientId; | ||
} | ||
cognito() { | ||
if (!this.cognitoMemo) { | ||
this.cognitoMemo = new CognitoIdentityProviderClient({ | ||
region: this.region | ||
}); | ||
} | ||
return this.cognitoMemo; | ||
} | ||
signUp(email) { | ||
return this.cognito().send(new SignUpCommand({ | ||
ClientId: this.clientId, | ||
Username: email, | ||
Password: 'aB1%' + getRandomString(14), | ||
UserAttributes: [{ | ||
Name: 'email', | ||
Value: email | ||
}] | ||
})); | ||
} | ||
signIn(email) { | ||
return this.cognito().send(new InitiateAuthCommand({ | ||
AuthFlow: 'CUSTOM_AUTH', | ||
ClientId: this.clientId, | ||
AuthParameters: { | ||
USERNAME: email | ||
} | ||
})); | ||
} | ||
async initiateAuth({ | ||
}) { | ||
let res; | ||
try { | ||
// Try sign in directly first | ||
res = await this.signIn(email); | ||
} catch (e) { | ||
if (e instanceof UserLambdaValidationException && e.message.includes("user not found")) { | ||
// Sign up and sign in | ||
await this.signUp(email); | ||
res = await this.signIn(email); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
if (!res.Session) { | ||
throw new Error("response session is empty"); | ||
} | ||
return { | ||
// Notice: rename session to instance to avoid | ||
// confusion with the native waas session | ||
instance: res.Session, | ||
email: email | ||
}; | ||
} | ||
async finalizeAuth({ | ||
instance, | ||
email, | ||
answer | ||
}) { | ||
const res = await this.cognito().send(new RespondToAuthChallengeCommand({ | ||
ClientId: this.clientId, | ||
Session: instance, | ||
ChallengeName: 'CUSTOM_CHALLENGE', | ||
ChallengeResponses: { | ||
USERNAME: email, | ||
ANSWER: answer | ||
} | ||
})); | ||
if (!res.AuthenticationResult || !res.AuthenticationResult.IdToken) { | ||
throw new Error('AuthenticationResult.IdToken is empty'); | ||
} | ||
return { | ||
idToken: res.AuthenticationResult.IdToken | ||
}; | ||
} | ||
} | ||
function encodeHex(data) { | ||
@@ -1136,2 +1226,15 @@ return "0x" + Array.from(typeof data === 'string' ? new TextEncoder().encode(data) : data, byte => byte.toString(16).padStart(2, '0')).join(""); | ||
} | ||
get email() { | ||
if (this.emailClient) { | ||
return this.emailClient; | ||
} | ||
if (!this.config.emailRegion) { | ||
throw new Error('Missing emailRegion'); | ||
} | ||
if (!this.config.emailClientId) { | ||
throw new Error('Missing emailClientId'); | ||
} | ||
this.emailClient = new EmailAuth(this.config.emailRegion, this.config.emailClientId); | ||
return this.emailClient; | ||
} | ||
async onValidationRequired(callback) { | ||
@@ -1138,0 +1241,0 @@ this.validationRequiredCallback.push(callback); |
@@ -7,2 +7,3 @@ import { Store } from "./store.js"; | ||
import { SimpleNetwork, WithSimpleNetwork } from "./networks.js"; | ||
import { EmailAuth } from "./email.js"; | ||
export type Sessions = (Session & { | ||
@@ -14,2 +15,3 @@ isThis: boolean; | ||
tenant: number; | ||
emailClientId?: string; | ||
identityPoolId: string; | ||
@@ -28,2 +30,3 @@ }; | ||
keyId: string; | ||
emailRegion?: string; | ||
endpoint?: string; | ||
@@ -52,3 +55,5 @@ }; | ||
private readonly deviceName; | ||
private emailClient; | ||
constructor(config: SequenceConfig & Partial<ExtendedSequenceConfig>, preset?: ExtendedSequenceConfig, store?: Store); | ||
get email(): EmailAuth; | ||
onValidationRequired(callback: () => void): Promise<() => void>; | ||
@@ -55,0 +60,0 @@ private handleValidationRequired; |
@@ -6,2 +6,3 @@ export declare const DEFAULT_GUARD = "https://guard2.sequence.app"; | ||
idpRegion: string; | ||
emailRegion: string; | ||
keyId: string; | ||
@@ -14,4 +15,5 @@ endpoint: string; | ||
idpRegion: string; | ||
emailRegion: string; | ||
keyId: string; | ||
endpoint: undefined; | ||
}; |
{ | ||
"name": "@0xsequence/waas", | ||
"version": "0.0.0-20231108135717", | ||
"version": "0.0.0-20231108174744", | ||
"description": "waas session client", | ||
@@ -12,2 +12,3 @@ "repository": "https://github.com/0xsequence/sequence.js/tree/master/packages/waas", | ||
"dependencies": { | ||
"@aws-sdk/client-cognito-identity-provider": "^3.445.0", | ||
"@aws-sdk/client-kms": "^3.429.0", | ||
@@ -14,0 +15,0 @@ "@aws-sdk/credential-providers": "^3.429.0", |
@@ -13,2 +13,3 @@ import { fromCognitoIdentityPool } from '@aws-sdk/credential-providers' | ||
import { TEMPLATE_LOCAL } from './defaults'; | ||
import { EmailAuth } from './email'; | ||
@@ -20,2 +21,4 @@ export type Sessions = (Session & { isThis: boolean })[] | ||
tenant: number, | ||
emailClientId?: string, | ||
identityPoolId: string, | ||
@@ -37,2 +40,3 @@ } | ||
keyId: string; | ||
emailRegion?: string; | ||
endpoint?: string; | ||
@@ -108,2 +112,4 @@ } | ||
private emailClient: EmailAuth | undefined | ||
constructor ( | ||
@@ -121,2 +127,19 @@ config: SequenceConfig & Partial<ExtendedSequenceConfig>, | ||
public get email() { | ||
if (this.emailClient) { | ||
return this.emailClient | ||
} | ||
if (!this.config.emailRegion) { | ||
throw new Error('Missing emailRegion') | ||
} | ||
if (!this.config.emailClientId) { | ||
throw new Error('Missing emailClientId') | ||
} | ||
this.emailClient = new EmailAuth(this.config.emailRegion, this.config.emailClientId) | ||
return this.emailClient | ||
} | ||
async onValidationRequired(callback: () => void) { | ||
@@ -123,0 +146,0 @@ this.validationRequiredCallback.push(callback) |
@@ -8,2 +8,3 @@ | ||
idpRegion: 'us-east-2', | ||
emailRegion: 'us-east-2', | ||
keyId: 'arn:aws:kms:us-east-1:000000000000:key/aeb99e0f-9e89-44de-a084-e1817af47778', | ||
@@ -17,4 +18,5 @@ endpoint: 'http://localstack:4566', | ||
idpRegion: 'us-east-2', | ||
emailRegion: 'us-east-2', | ||
keyId: 'arn:aws:kms:us-east-2:170768627592:key/0fd8f803-9cb5-4de5-86e4-41963fb6043d', | ||
endpoint: undefined, | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
255660
39
7600
9
6
+ Added@aws-sdk/client-cognito-identity-provider@3.738.0(transitive)