@xube/kit-aws 0.0.59 → 0.0.61
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 +3 -0
- package/dist/index.js +3 -0
- package/dist/resources/cognito/authenticate.d.ts +6 -0
- package/dist/resources/cognito/authenticate.js +43 -0
- package/dist/resources/cognito/register.d.ts +4 -0
- package/dist/resources/cognito/register.js +41 -0
- package/dist/resources/parameter-store/get.d.ts +3 -0
- package/dist/resources/parameter-store/get.js +40 -0
- package/package.json +11 -9
- package/src/index.ts +3 -0
- package/src/resources/cognito/authenticate.ts +62 -0
- package/src/resources/cognito/register.ts +65 -0
- package/src/resources/parameter-store/get.ts +46 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
export * from "./resources/cognito/authenticate";
|
|
2
|
+
export * from "./resources/cognito/register";
|
|
1
3
|
export * from "./resources/dynamodb/get";
|
|
2
4
|
export * from "./resources/dynamodb/put";
|
|
3
5
|
export * from "./resources/dynamodb/query";
|
|
4
6
|
export * from "./resources/dynamodb/transform";
|
|
7
|
+
export * from "./resources/parameter-store/get";
|
package/dist/index.js
CHANGED
|
@@ -14,7 +14,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./resources/cognito/authenticate"), exports);
|
|
18
|
+
__exportStar(require("./resources/cognito/register"), exports);
|
|
17
19
|
__exportStar(require("./resources/dynamodb/get"), exports);
|
|
18
20
|
__exportStar(require("./resources/dynamodb/put"), exports);
|
|
19
21
|
__exportStar(require("./resources/dynamodb/query"), exports);
|
|
20
22
|
__exportStar(require("./resources/dynamodb/transform"), exports);
|
|
23
|
+
__exportStar(require("./resources/parameter-store/get"), exports);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { AuthFlowType } from "@aws-sdk/client-cognito-identity-provider";
|
|
2
|
+
import { AuthenticationRequest, AuthenticationResponse } from "@xube/kit-aws-schema";
|
|
3
|
+
import { XubeLog } from "@xube/kit-log";
|
|
4
|
+
import { XubeResponse } from "@xube/kit-request";
|
|
5
|
+
export { AuthFlowType };
|
|
6
|
+
export declare const authenticateUser: (request: AuthenticationRequest, authFlow: typeof AuthFlowType.USER_PASSWORD_AUTH, clientId: string, log?: XubeLog) => Promise<XubeResponse<AuthenticationResponse>>;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.authenticateUser = exports.AuthFlowType = void 0;
|
|
4
|
+
const client_cognito_identity_provider_1 = require("@aws-sdk/client-cognito-identity-provider");
|
|
5
|
+
Object.defineProperty(exports, "AuthFlowType", { enumerable: true, get: function () { return client_cognito_identity_provider_1.AuthFlowType; } });
|
|
6
|
+
const kit_constants_1 = require("@xube/kit-constants");
|
|
7
|
+
const kit_log_1 = require("@xube/kit-log");
|
|
8
|
+
const kit_request_1 = require("@xube/kit-request");
|
|
9
|
+
const client = new client_cognito_identity_provider_1.CognitoIdentityProviderClient({
|
|
10
|
+
region: process.env.AWS_REGION,
|
|
11
|
+
});
|
|
12
|
+
const authenticateUser = async (request, authFlow, clientId, log = kit_log_1.XubeLog.getInstance()) => {
|
|
13
|
+
log.info("Authenticating user");
|
|
14
|
+
try {
|
|
15
|
+
const initiateAuthCommand = new client_cognito_identity_provider_1.InitiateAuthCommand({
|
|
16
|
+
AuthFlow: authFlow,
|
|
17
|
+
AuthParameters: {
|
|
18
|
+
USERNAME: request.email,
|
|
19
|
+
PASSWORD: request.password,
|
|
20
|
+
},
|
|
21
|
+
ClientId: clientId,
|
|
22
|
+
});
|
|
23
|
+
log.info("Sending authentication request");
|
|
24
|
+
const authResponse = await client.send(initiateAuthCommand);
|
|
25
|
+
const authenticationResponse = {
|
|
26
|
+
accessToken: authResponse.AuthenticationResult?.AccessToken || "",
|
|
27
|
+
refreshToken: authResponse.AuthenticationResult?.RefreshToken || "",
|
|
28
|
+
expiry: authResponse.AuthenticationResult?.ExpiresIn || 0,
|
|
29
|
+
token: authResponse.AuthenticationResult?.IdToken || "",
|
|
30
|
+
deviceKey: authResponse.AuthenticationResult?.NewDeviceMetadata?.DeviceKey,
|
|
31
|
+
};
|
|
32
|
+
log.info("Authentication successful");
|
|
33
|
+
return new kit_request_1.XubeResponse({
|
|
34
|
+
statusCode: kit_constants_1.StatusCode.OK,
|
|
35
|
+
data: authenticationResponse,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
catch (e) {
|
|
39
|
+
log.info("Authentication failed" + JSON.stringify(e));
|
|
40
|
+
throw e;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
exports.authenticateUser = authenticateUser;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { AuthenticationRequest, AuthenticationResponse } from "@xube/kit-aws-schema";
|
|
2
|
+
import { XubeLog } from "@xube/kit-log";
|
|
3
|
+
import { XubeResponse } from "@xube/kit-request";
|
|
4
|
+
export declare const register: (credentials: AuthenticationRequest, clientId: string, log?: XubeLog) => Promise<XubeResponse<AuthenticationResponse>>;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.register = void 0;
|
|
4
|
+
const client_cognito_identity_provider_1 = require("@aws-sdk/client-cognito-identity-provider");
|
|
5
|
+
const kit_constants_1 = require("@xube/kit-constants");
|
|
6
|
+
const kit_log_1 = require("@xube/kit-log");
|
|
7
|
+
const kit_request_1 = require("@xube/kit-request");
|
|
8
|
+
const authenticate_1 = require("./authenticate");
|
|
9
|
+
const cognitoIDPClient = new client_cognito_identity_provider_1.CognitoIdentityProviderClient({
|
|
10
|
+
region: process.env.AWS_REGION,
|
|
11
|
+
});
|
|
12
|
+
const register = async (credentials, clientId, log = kit_log_1.XubeLog.getInstance()) => {
|
|
13
|
+
try {
|
|
14
|
+
const response = await cognitoIDPClient.send(new client_cognito_identity_provider_1.SignUpCommand({
|
|
15
|
+
ClientId: clientId,
|
|
16
|
+
Username: credentials.email,
|
|
17
|
+
Password: credentials.password,
|
|
18
|
+
}));
|
|
19
|
+
if (!response.UserSub) {
|
|
20
|
+
log.info(`User was not created. Response from Cognito: ${JSON.stringify(response)}`);
|
|
21
|
+
return new kit_request_1.XubeResponse({
|
|
22
|
+
statusCode: kit_constants_1.StatusCode.InternalError,
|
|
23
|
+
error: `User was not created`,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
return await (0, authenticate_1.authenticateUser)(credentials, client_cognito_identity_provider_1.AuthFlowType.USER_PASSWORD_AUTH, clientId, log);
|
|
27
|
+
}
|
|
28
|
+
catch (e) {
|
|
29
|
+
if (e instanceof client_cognito_identity_provider_1.UsernameExistsException) {
|
|
30
|
+
return new kit_request_1.XubeResponse({
|
|
31
|
+
statusCode: kit_constants_1.StatusCode.Conflict,
|
|
32
|
+
error: `Username already exists`,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
return new kit_request_1.XubeResponse({
|
|
36
|
+
statusCode: kit_constants_1.StatusCode.InternalError,
|
|
37
|
+
error: `Error signing up user: ${e}`,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
exports.register = register;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getParameter = void 0;
|
|
4
|
+
const client_ssm_1 = require("@aws-sdk/client-ssm");
|
|
5
|
+
const kit_constants_1 = require("@xube/kit-constants");
|
|
6
|
+
const kit_log_1 = require("@xube/kit-log");
|
|
7
|
+
const kit_request_1 = require("@xube/kit-request");
|
|
8
|
+
const ssmClient = new client_ssm_1.SSMClient({ region: process.env.AWS_REGION });
|
|
9
|
+
const getParameter = async (parameterName, log = kit_log_1.XubeLog.getInstance()) => {
|
|
10
|
+
const getParamCommand = new client_ssm_1.GetParameterCommand({
|
|
11
|
+
Name: parameterName,
|
|
12
|
+
});
|
|
13
|
+
let parameterValue;
|
|
14
|
+
try {
|
|
15
|
+
const paramResponse = await ssmClient.send(getParamCommand);
|
|
16
|
+
if (!paramResponse?.Parameter?.Value) {
|
|
17
|
+
throw "Parameter value does not exist";
|
|
18
|
+
}
|
|
19
|
+
parameterValue = paramResponse.Parameter.Value;
|
|
20
|
+
if (!parameterValue) {
|
|
21
|
+
log.info(`Parameter ${parameterName} does not exist`);
|
|
22
|
+
return new kit_request_1.XubeResponse({
|
|
23
|
+
statusCode: kit_constants_1.StatusCode.NotFound,
|
|
24
|
+
error: `Parameter ${parameterName} does not exist`,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
log.error(`Error getting parameter ${parameterName}: ${error}`);
|
|
30
|
+
return new kit_request_1.XubeResponse({
|
|
31
|
+
statusCode: kit_constants_1.StatusCode.InternalError,
|
|
32
|
+
error: `Error getting parameter ${parameterName}: ${error}`,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
return new kit_request_1.XubeResponse({
|
|
36
|
+
statusCode: kit_constants_1.StatusCode.OK,
|
|
37
|
+
data: parameterValue,
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
exports.getParameter = getParameter;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xube/kit-aws",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.61",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -18,16 +18,18 @@
|
|
|
18
18
|
"homepage": "https://github.com/XubeLtd/dev-kit#readme",
|
|
19
19
|
"devDependencies": {
|
|
20
20
|
"@types/aws-lambda": "^8.10.119",
|
|
21
|
-
"@xube/kit-build": "^0.0.
|
|
21
|
+
"@xube/kit-build": "^0.0.61"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@aws-sdk/client-
|
|
25
|
-
"@aws-sdk/
|
|
26
|
-
"@aws-sdk/
|
|
27
|
-
"@
|
|
28
|
-
"@
|
|
29
|
-
"@xube/kit-
|
|
30
|
-
"@xube/kit-
|
|
24
|
+
"@aws-sdk/client-cognito-identity-provider": "^3.433.0",
|
|
25
|
+
"@aws-sdk/client-dynamodb": "^3.433.0",
|
|
26
|
+
"@aws-sdk/client-ssm": "^3.433.0",
|
|
27
|
+
"@aws-sdk/lib-dynamodb": "^3.433.0",
|
|
28
|
+
"@aws-sdk/util-dynamodb": "^3.433.0",
|
|
29
|
+
"@xube/kit-aws-schema": "^0.0.61",
|
|
30
|
+
"@xube/kit-log": "^0.0.61",
|
|
31
|
+
"@xube/kit-request": "^0.0.61",
|
|
32
|
+
"@xube/kit-schema": "^0.0.61",
|
|
31
33
|
"zod": "^3.22.4"
|
|
32
34
|
}
|
|
33
35
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
export * from "./resources/cognito/authenticate";
|
|
2
|
+
export * from "./resources/cognito/register";
|
|
1
3
|
export * from "./resources/dynamodb/get";
|
|
2
4
|
export * from "./resources/dynamodb/put";
|
|
3
5
|
export * from "./resources/dynamodb/query";
|
|
4
6
|
export * from "./resources/dynamodb/transform";
|
|
7
|
+
export * from "./resources/parameter-store/get";
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AuthFlowType,
|
|
3
|
+
CognitoIdentityProviderClient,
|
|
4
|
+
InitiateAuthCommand,
|
|
5
|
+
InitiateAuthCommandOutput,
|
|
6
|
+
} from "@aws-sdk/client-cognito-identity-provider";
|
|
7
|
+
import {
|
|
8
|
+
AuthenticationRequest,
|
|
9
|
+
AuthenticationResponse,
|
|
10
|
+
} from "@xube/kit-aws-schema";
|
|
11
|
+
import { StatusCode } from "@xube/kit-constants";
|
|
12
|
+
import { XubeLog } from "@xube/kit-log";
|
|
13
|
+
import { XubeResponse } from "@xube/kit-request";
|
|
14
|
+
|
|
15
|
+
export { AuthFlowType };
|
|
16
|
+
|
|
17
|
+
const client = new CognitoIdentityProviderClient({
|
|
18
|
+
region: process.env.AWS_REGION,
|
|
19
|
+
});
|
|
20
|
+
export const authenticateUser = async (
|
|
21
|
+
request: AuthenticationRequest,
|
|
22
|
+
authFlow: typeof AuthFlowType.USER_PASSWORD_AUTH,
|
|
23
|
+
clientId: string,
|
|
24
|
+
log: XubeLog = XubeLog.getInstance()
|
|
25
|
+
): Promise<XubeResponse<AuthenticationResponse>> => {
|
|
26
|
+
log.info("Authenticating user");
|
|
27
|
+
try {
|
|
28
|
+
const initiateAuthCommand = new InitiateAuthCommand({
|
|
29
|
+
AuthFlow: authFlow,
|
|
30
|
+
AuthParameters: {
|
|
31
|
+
USERNAME: request.email,
|
|
32
|
+
PASSWORD: request.password,
|
|
33
|
+
},
|
|
34
|
+
ClientId: clientId,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
log.info("Sending authentication request");
|
|
38
|
+
|
|
39
|
+
const authResponse: InitiateAuthCommandOutput = await client.send(
|
|
40
|
+
initiateAuthCommand
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const authenticationResponse: AuthenticationResponse = {
|
|
44
|
+
accessToken: authResponse.AuthenticationResult?.AccessToken || "",
|
|
45
|
+
refreshToken: authResponse.AuthenticationResult?.RefreshToken || "",
|
|
46
|
+
expiry: authResponse.AuthenticationResult?.ExpiresIn || 0,
|
|
47
|
+
token: authResponse.AuthenticationResult?.IdToken || "",
|
|
48
|
+
deviceKey:
|
|
49
|
+
authResponse.AuthenticationResult?.NewDeviceMetadata?.DeviceKey,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
log.info("Authentication successful");
|
|
53
|
+
|
|
54
|
+
return new XubeResponse({
|
|
55
|
+
statusCode: StatusCode.OK,
|
|
56
|
+
data: authenticationResponse,
|
|
57
|
+
});
|
|
58
|
+
} catch (e) {
|
|
59
|
+
log.info("Authentication failed" + JSON.stringify(e));
|
|
60
|
+
throw e;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AuthFlowType,
|
|
3
|
+
CognitoIdentityProviderClient,
|
|
4
|
+
SignUpCommand,
|
|
5
|
+
UsernameExistsException,
|
|
6
|
+
} from "@aws-sdk/client-cognito-identity-provider";
|
|
7
|
+
import {
|
|
8
|
+
AuthenticationRequest,
|
|
9
|
+
AuthenticationResponse,
|
|
10
|
+
} from "@xube/kit-aws-schema";
|
|
11
|
+
import { StatusCode } from "@xube/kit-constants";
|
|
12
|
+
import { XubeLog } from "@xube/kit-log";
|
|
13
|
+
import { XubeResponse } from "@xube/kit-request";
|
|
14
|
+
import { authenticateUser } from "./authenticate";
|
|
15
|
+
|
|
16
|
+
const cognitoIDPClient: CognitoIdentityProviderClient =
|
|
17
|
+
new CognitoIdentityProviderClient({
|
|
18
|
+
region: process.env.AWS_REGION,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export const register = async (
|
|
22
|
+
credentials: AuthenticationRequest,
|
|
23
|
+
clientId: string,
|
|
24
|
+
log: XubeLog = XubeLog.getInstance()
|
|
25
|
+
): Promise<XubeResponse<AuthenticationResponse>> => {
|
|
26
|
+
try {
|
|
27
|
+
const response = await cognitoIDPClient.send(
|
|
28
|
+
new SignUpCommand({
|
|
29
|
+
ClientId: clientId,
|
|
30
|
+
Username: credentials.email,
|
|
31
|
+
Password: credentials.password,
|
|
32
|
+
})
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
if (!response.UserSub) {
|
|
36
|
+
log.info(
|
|
37
|
+
`User was not created. Response from Cognito: ${JSON.stringify(
|
|
38
|
+
response
|
|
39
|
+
)}`
|
|
40
|
+
);
|
|
41
|
+
return new XubeResponse({
|
|
42
|
+
statusCode: StatusCode.InternalError,
|
|
43
|
+
error: `User was not created`,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return await authenticateUser(
|
|
48
|
+
credentials,
|
|
49
|
+
AuthFlowType.USER_PASSWORD_AUTH,
|
|
50
|
+
clientId,
|
|
51
|
+
log
|
|
52
|
+
);
|
|
53
|
+
} catch (e) {
|
|
54
|
+
if (e instanceof UsernameExistsException) {
|
|
55
|
+
return new XubeResponse({
|
|
56
|
+
statusCode: StatusCode.Conflict,
|
|
57
|
+
error: `Username already exists`,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return new XubeResponse({
|
|
61
|
+
statusCode: StatusCode.InternalError,
|
|
62
|
+
error: `Error signing up user: ${e}`,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { GetParameterCommand, SSMClient } from "@aws-sdk/client-ssm";
|
|
2
|
+
import { StatusCode } from "@xube/kit-constants";
|
|
3
|
+
import { XubeLog } from "@xube/kit-log";
|
|
4
|
+
import { XubeResponse } from "@xube/kit-request";
|
|
5
|
+
|
|
6
|
+
const ssmClient: SSMClient = new SSMClient({ region: process.env.AWS_REGION });
|
|
7
|
+
|
|
8
|
+
export const getParameter = async (
|
|
9
|
+
parameterName: string,
|
|
10
|
+
log: XubeLog = XubeLog.getInstance()
|
|
11
|
+
): Promise<XubeResponse<string>> => {
|
|
12
|
+
const getParamCommand = new GetParameterCommand({
|
|
13
|
+
Name: parameterName,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
let parameterValue: string;
|
|
17
|
+
|
|
18
|
+
try {
|
|
19
|
+
const paramResponse = await ssmClient.send(getParamCommand);
|
|
20
|
+
|
|
21
|
+
if (!paramResponse?.Parameter?.Value) {
|
|
22
|
+
throw "Parameter value does not exist";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
parameterValue = paramResponse.Parameter.Value;
|
|
26
|
+
|
|
27
|
+
if (!parameterValue) {
|
|
28
|
+
log.info(`Parameter ${parameterName} does not exist`);
|
|
29
|
+
return new XubeResponse({
|
|
30
|
+
statusCode: StatusCode.NotFound,
|
|
31
|
+
error: `Parameter ${parameterName} does not exist`,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
} catch (error) {
|
|
35
|
+
log.error(`Error getting parameter ${parameterName}: ${error}`);
|
|
36
|
+
return new XubeResponse({
|
|
37
|
+
statusCode: StatusCode.InternalError,
|
|
38
|
+
error: `Error getting parameter ${parameterName}: ${error}`,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return new XubeResponse({
|
|
43
|
+
statusCode: StatusCode.OK,
|
|
44
|
+
data: parameterValue,
|
|
45
|
+
});
|
|
46
|
+
};
|