Skip to content

Commit

Permalink
[BCAN-1] Fix main environment (#6)
Browse files Browse the repository at this point in the history
Co-authored-by: jaren adams
  • Loading branch information
jarenadams21 authored Oct 3, 2024
1 parent b5e6618 commit 857fa0c
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 26 deletions.
2 changes: 1 addition & 1 deletion backend/dist/app.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion backend/dist/auth/auth.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
}),
],
Expand Down
40 changes: 23 additions & 17 deletions backend/dist/auth/auth.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,21 @@ 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() {
this.logger = new common_1.Logger(AuthService_1.name);
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');
}
Expand All @@ -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,
Expand All @@ -71,7 +74,6 @@ let AuthService = AuthService_1 = class AuthService {
MessageAction: 'SUPPRESS',
})
.promise();
// Set the user's password
await this.cognito
.adminSetUserPassword({
UserPoolId: userPoolId,
Expand All @@ -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: {
Expand All @@ -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;
Expand All @@ -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 {
Expand All @@ -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.');
}
Expand All @@ -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({
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions backend/dist/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
4 changes: 2 additions & 2 deletions backend/dist/user/user.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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,
},
Expand Down
5 changes: 4 additions & 1 deletion backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export class AuthService {
throw new Error('Access token is undefined.');
}


const getUserResponse = await this.cognito
.getUser({ AccessToken: accessToken })
.promise();
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions backend/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const dynamodb = new AWS.DynamoDB.DocumentClient();
export class UserService {
async getAllUsers(): Promise<any> {
const params = {
TableName: process.env.DYNAMODB_USERS_TABLE || 'TABLE_FAILURE',
TableName: process.env.DYNAMODB_USER_TABLE_NAME || 'TABLE_FAILURE',
};

try {
Expand All @@ -20,7 +20,7 @@ export class UserService {

async getUserById(userId: string): Promise<any> {
const params = {
TableName: process.env.DYNAMODB_USERS_TABLE || 'TABLE_FAILURE',
TableName: process.env.DYNAMODB_USER_TABLE_NAME || 'TABLE_FAILURE',
Key: {
userId,
},
Expand Down

0 comments on commit 857fa0c

Please sign in to comment.