Skip to content

Commit

Permalink
Merge pull request #26 from FlavioAandres/secret-user
Browse files Browse the repository at this point in the history
Adding secrets to user
  • Loading branch information
AndresMorelos authored Dec 21, 2020
2 parents 53c5ac7 + 456739f commit 28afbb1
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 23 deletions.
40 changes: 39 additions & 1 deletion API/Functions/Users/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const UserRepo = require("./../../../shared/database/repos/user.repo");

const { encrypt, decrypt } = require('../../../shared/utils/crypto')
module.exports.getUserInformation = async () => {
try {
const result = await UserRepo.getUser(
Expand Down Expand Up @@ -66,3 +66,41 @@ module.exports.addNewCategory = async (event) => {
};
}
};

module.exports.checkSecretKey = async (event) => {
const body = event.body ? JSON.parse(event.body) : {};

if (!body.secretKey) return {
statusCode: 400,
headers: {
"Access-Control-Allow-Origin": "*",
}
}

const user = await UserRepo.getUser({
emails: process.env.EMAIL_USERNAME,
})

if (!user.secretKey) return {
statusCode: 409,
headers: {
"Access-Control-Allow-Origin": "*",
}
}

const userKey = decrypt(user.secretKey)

if (userKey !== body.secretKey) return {
statusCode: 401,
headers: {
"Access-Control-Allow-Origin": "*",
}
}

return {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
}
}
}
22 changes: 19 additions & 3 deletions client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class App extends React.Component {
constructor(props) {
super(props);
this.state = {
secret: "null",
secret: null,
user: {},
banks: [],
prepayments: [],
Expand All @@ -37,12 +37,27 @@ class App extends React.Component {
};

componentDidMount = () => {
if(!this.state.secretKey) return;
this.loadInitialData()
};

loadInitialData(){
this.getPrePayments();
this.getUserInformation()
};
}
componentDidUpdate(prevProps, prevState){
const { secret } = prevState
if(!secret && this.state.secret){
this.loadInitialData()
}
}

onLoginClick = (secret) => {
console.log(secret);
axios.post(constants.basepath + constants.routes.secret, {
secretKey: secret
}).then(result=>{
this.setState({secret: true})
}).catch(err=>console.error(err))
};

getUserInformation = () => {
Expand Down Expand Up @@ -89,6 +104,7 @@ class App extends React.Component {
}
})
}

render() {
const { prepayments, secret, user } = this.state;

Expand Down
38 changes: 20 additions & 18 deletions client/src/components/SecretCodeScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,31 @@ import React from "react";
import { PasswordStrengthMeter, TextField, Icon, Button } from "emerald-ui/lib";

const SecretCodeScreen = (props) => {
const {
onLoginClick = ()=>null
} = props
const [secretPassword, setSecret] = React.useState('')
const { onLoginClick = () => null } = props;
const [secretPassword, setSecret] = React.useState("");
return (
<div className="full-screen-secret-container">
<div className="login-container">
<img src="" alt="" />
<div className="secret-key-input-container">
<PasswordStrengthMeter id="passwordmeter">
<TextField
value={secretPassword}
onChange={(evt)=> console.log(evt.target)}
label="Ingrese su Llave de acceso: "
type="password"
style={{ width: "250px" }}
/>
</PasswordStrengthMeter>
<Button
onClick={() => onLoginClick(secretPassword)}
color="danger"
clearable="true"
className="sign-in-button">
<TextField
clearable
onClear={() => setSecret("")}
value={secretPassword}
onKeyPress={(evt) => {
if (evt.key === "Enter") onLoginClick(secretPassword);
}}
onChange={(evt) => setSecret(evt.target.value)}
label="Ingrese su Llave de acceso: "
type="password"
style={{ width: "250px" }}
/>
<Button
onClick={() => onLoginClick(secretPassword)}
color="danger"
clearable="true"
className="sign-in-button"
>
<Icon name="thumb_up" />
</Button>
</div>
Expand Down
1 change: 1 addition & 0 deletions client/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export default {
datacredit: '/datacredit',
user: '/user',
categories: '/user/categories',
secret: '/user/secret',
}
}
2 changes: 1 addition & 1 deletion devutils/generateDataCreditoCredentials.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { encrypt } = require('../shared/utils/crypto')

const username = "user";
const username = "XXXXXXX";
const password = "passwprd";
const secondpass = "secondpass";

Expand Down
14 changes: 14 additions & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ functions:
- origin: "*"
- headers: "Access-Control-Allow-Origin"

CheckUserSecret:
handler: API/Functions/Users.checkSecretKey
name: UserRepo-post-secretKey-${opt:stage}
environment:
EMAIL_USERNAME: ${file(./config/${opt:stage}.json):EMAIL_USERNAME}
events:
- http:
path: /user/secret
method: POST
cors:
- enabled: true
- origin: "*"
- headers: "Access-Control-Allow-Origin"

SaveCategory:
handler: API/Functions/Users.addNewCategory
name: UserRepo-Categories-POST-${opt:stage}
Expand Down
4 changes: 4 additions & 0 deletions shared/models/user.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const userSchema = mongoose.Schema(
index: true,
required: false,
},
secretKey: {
iv: String,
content: String,
},
email: {
type: String,
required: true,
Expand Down

0 comments on commit 28afbb1

Please sign in to comment.