From 7d1d401813949fa98f4792f968180ac8a69f080b Mon Sep 17 00:00:00 2001 From: jaren adams Date: Thu, 3 Oct 2024 11:22:05 -0400 Subject: [PATCH] [BCAN-1] Fix main environment --- backend/dist/app.module.js | 2 +- backend/dist/auth/auth.module.js | 2 +- backend/dist/auth/auth.service.js | 40 ++++++++++++++++++------------- backend/dist/main.js | 4 ++-- backend/dist/user/user.service.js | 4 ++-- backend/src/auth/auth.service.ts | 5 +++- backend/src/user/user.service.ts | 4 ++-- 7 files changed, 35 insertions(+), 26 deletions(-) diff --git a/backend/dist/app.module.js b/backend/dist/app.module.js index c073eea..1d0686a 100644 --- a/backend/dist/app.module.js +++ b/backend/dist/app.module.js @@ -14,7 +14,7 @@ let AppModule = class AppModule { }; AppModule = __decorate([ (0, common_1.Module)({ - imports: [auth_module_1.AuthModule, user_module_1.UserModule], // Add any other modules here + imports: [auth_module_1.AuthModule, user_module_1.UserModule], }) ], AppModule); exports.AppModule = AppModule; diff --git a/backend/dist/auth/auth.module.js b/backend/dist/auth/auth.module.js index 3fbfeb6..5bfcc16 100644 --- a/backend/dist/auth/auth.module.js +++ b/backend/dist/auth/auth.module.js @@ -17,7 +17,7 @@ AuthModule = __decorate([ (0, common_1.Module)({ imports: [ jwt_1.JwtModule.register({ - secret: process.env.JWT_SECRET || 'yourSecretKey', + secret: process.env.JWT_SECRET, signOptions: { expiresIn: '1h' }, }), ], diff --git a/backend/dist/auth/auth.service.js b/backend/dist/auth/auth.service.js index 6b68b79..ffcf024 100644 --- a/backend/dist/auth/auth.service.js +++ b/backend/dist/auth/auth.service.js @@ -38,7 +38,7 @@ const common_1 = require("@nestjs/common"); const aws_sdk_1 = __importDefault(require("aws-sdk")); const crypto = __importStar(require("crypto")); aws_sdk_1.default.config.update({ - region: process.env.AWS_REGION || 'us-east-1', + region: process.env.AWS_REGION, }); let AuthService = AuthService_1 = class AuthService { constructor() { @@ -46,9 +46,13 @@ let AuthService = AuthService_1 = class AuthService { this.cognito = new aws_sdk_1.default.CognitoIdentityServiceProvider(); this.dynamoDb = new aws_sdk_1.default.DynamoDB.DocumentClient(); } - computeSecretHash(username, clientId, clientSecret) { + computeHatch(username, clientId, clientSecret) { + const hatch = process.env.FISH_EYE_LENS; + if (!hatch) { + throw new EvalError("Corrupted"); + } return crypto - .createHmac('SHA256', clientSecret) + .createHmac(hatch, clientSecret) .update(username + clientId) .digest('base64'); } @@ -59,7 +63,6 @@ let AuthService = AuthService_1 = class AuthService { throw new Error('Cognito User Pool ID is not defined.'); } try { - // Create the user in Cognito await this.cognito .adminCreateUser({ UserPoolId: userPoolId, @@ -71,7 +74,6 @@ let AuthService = AuthService_1 = class AuthService { MessageAction: 'SUPPRESS', }) .promise(); - // Set the user's password await this.cognito .adminSetUserPassword({ UserPoolId: userPoolId, @@ -80,8 +82,8 @@ let AuthService = AuthService_1 = class AuthService { Permanent: true, }) .promise(); - // Create a new user record in DynamoDB - const tableName = process.env.DYNAMODB_TABLE_NAME || 'BCANBeings'; + // Todo + const tableName = process.env.DYNAMODB_TABLE_NAME || 'TABLE_FAILURE'; const params = { TableName: tableName, Item: { @@ -101,6 +103,7 @@ let AuthService = AuthService_1 = class AuthService { throw new Error('An unknown error occurred during registration'); } } + // Overall, needs better undefined handling and optional adding async login(username, password) { var _a; const clientId = process.env.COGNITO_CLIENT_ID; @@ -109,14 +112,15 @@ let AuthService = AuthService_1 = class AuthService { this.logger.error('Cognito Client ID or Secret is not defined.'); throw new Error('Cognito Client ID or Secret is not defined.'); } - const secretHash = this.computeSecretHash(username, clientId, clientSecret); + const hatch = this.computeHatch(username, clientId, clientSecret); + // Todo, change constants of AUTH_FLOW types & other constants in repo const authParams = { AuthFlow: 'USER_PASSWORD_AUTH', ClientId: clientId, AuthParameters: { USERNAME: username, PASSWORD: password, - SECRET_HASH: secretHash, + SECRET_HASH: hatch, }, }; try { @@ -138,9 +142,10 @@ let AuthService = AuthService_1 = class AuthService { this.logger.error('Authentication failed: Missing IdToken or AccessToken'); throw new Error('Authentication failed: Missing IdToken or AccessToken'); } + // User Identity Information const idToken = response.AuthenticationResult.IdToken; + // Grants access to resources const accessToken = response.AuthenticationResult.AccessToken; - // Retrieve user's email using getUser if AccessToken is valid if (!accessToken) { throw new Error('Access token is undefined.'); } @@ -154,25 +159,26 @@ let AuthService = AuthService_1 = class AuthService { break; } } + // Fundamental attribute check (email must exist between Cognito and Dynamo) if (!email) { throw new Error('Failed to retrieve user email from Cognito.'); } - // Fetch user data from DynamoDB - const tableName = process.env.DYNAMODB_TABLE_NAME || 'BCANBeings'; + const tableName = process.env.DYNAMODB_USER_TABLE_NAME || 'TABLE_FAILURE'; + this.logger.debug('user response..?' + tableName); const params = { TableName: tableName, Key: { - userId: username, // Ensure this matches the DynamoDB table's partition key (adjust if necessary) + userId: username, }, }; + // Grab table reference for in-app use const userResult = await this.dynamoDb.get(params).promise(); let user = userResult.Item; if (!user) { - // User not found, create a new user record const newUser = { userId: username, email: email, - biography: '', // Initialize biography as empty + biography: '', }; await this.dynamoDb .put({ @@ -199,11 +205,11 @@ let AuthService = AuthService_1 = class AuthService { this.logger.error('Cognito Client ID or Secret is not defined.'); throw new Error('Cognito Client ID or Secret is not defined.'); } - const secretHash = this.computeSecretHash(username, clientId, clientSecret); + const hatch = this.computeHatch(username, clientId, clientSecret); const challengeResponses = { USERNAME: username, NEW_PASSWORD: newPassword, - SECRET_HASH: secretHash, + SECRET_HASH: hatch, }; if (email) { challengeResponses.email = email; diff --git a/backend/dist/main.js b/backend/dist/main.js index 0181c27..f100103 100644 --- a/backend/dist/main.js +++ b/backend/dist/main.js @@ -36,8 +36,8 @@ async function bootstrap() { region: process.env.AWS_REGION }); const app = await core_1.NestFactory.create(app_module_1.AppModule); - app.enableCors(); // Enable CORS if needed - await app.listen(3001); // Port where the server listens + app.enableCors(); + await app.listen(3001); } dotenv.config(); bootstrap(); diff --git a/backend/dist/user/user.service.js b/backend/dist/user/user.service.js index 8815b98..439f033 100644 --- a/backend/dist/user/user.service.js +++ b/backend/dist/user/user.service.js @@ -16,7 +16,7 @@ const dynamodb = new aws_sdk_1.default.DynamoDB.DocumentClient(); let UserService = class UserService { async getAllUsers() { const params = { - TableName: process.env.DYNAMODB_USERS_TABLE || 'UsersTable', + TableName: process.env.DYNAMODB_USER_TABLE_NAME || 'TABLE_FAILURE', }; try { const data = await dynamodb.scan(params).promise(); @@ -28,7 +28,7 @@ let UserService = class UserService { } async getUserById(userId) { const params = { - TableName: process.env.DYNAMODB_USERS_TABLE || 'UsersTable', + TableName: process.env.DYNAMODB_USER_TABLE_NAME || 'TABLE_FAILURE', Key: { userId, }, diff --git a/backend/src/auth/auth.service.ts b/backend/src/auth/auth.service.ts index 66b10b5..da81e08 100644 --- a/backend/src/auth/auth.service.ts +++ b/backend/src/auth/auth.service.ts @@ -155,6 +155,7 @@ export class AuthService { throw new Error('Access token is undefined.'); } + const getUserResponse = await this.cognito .getUser({ AccessToken: accessToken }) .promise(); @@ -173,7 +174,9 @@ export class AuthService { throw new Error('Failed to retrieve user email from Cognito.'); } - const tableName = process.env.DYNAMODB_TABLE_NAME || 'TABLE_FAILURE'; + const tableName = process.env.DYNAMODB_USER_TABLE_NAME || 'TABLE_FAILURE'; + + this.logger.debug('user response..?' + tableName) const params = { TableName: tableName, diff --git a/backend/src/user/user.service.ts b/backend/src/user/user.service.ts index 216ec36..ed9ce7c 100644 --- a/backend/src/user/user.service.ts +++ b/backend/src/user/user.service.ts @@ -7,7 +7,7 @@ const dynamodb = new AWS.DynamoDB.DocumentClient(); export class UserService { async getAllUsers(): Promise { const params = { - TableName: process.env.DYNAMODB_USERS_TABLE || 'TABLE_FAILURE', + TableName: process.env.DYNAMODB_USER_TABLE_NAME || 'TABLE_FAILURE', }; try { @@ -20,7 +20,7 @@ export class UserService { async getUserById(userId: string): Promise { const params = { - TableName: process.env.DYNAMODB_USERS_TABLE || 'TABLE_FAILURE', + TableName: process.env.DYNAMODB_USER_TABLE_NAME || 'TABLE_FAILURE', Key: { userId, },