WEBVTT 0 00:01.310 --> 00:01.690 All right. 1 00:01.720 --> 00:09.940 So we've taken a look at level 1 encryption which is basically just storing the password as plain text 2 00:10.300 --> 00:11.390 in our database. 3 00:11.410 --> 00:18.840 So maybe it'll be a little bit difficult for people to get access to our server and access our database. 4 00:18.880 --> 00:24.970 At least you can't just simply right click on a website to view page source and be able to see it in 5 00:24.970 --> 00:25.510 the HTML. 6 00:25.510 --> 00:30.160 Well at least it's stored at server level. But that's not really good enough. 7 00:30.160 --> 00:37.150 So let's go ahead and see what we can do to improve the security for our users on our website. 8 00:37.540 --> 00:47.740 So let's increase to level 2 authentication. And level 2 authentication involves the use of encryption. 9 00:48.550 --> 00:50.530 So what exactly is encryption? 10 00:50.590 --> 00:58.180 Well basically all it is is just scrambling something so that people can't tell what the original was 11 00:58.450 --> 01:03.250 unless they were in on the secret and they knew how to unscramble it. 12 01:03.250 --> 01:08.590 This is exactly the same as if you and your friend were sending each other secret messages and you had 13 01:08.590 --> 01:12.220 a key to encode the message that you both knew 14 01:12.370 --> 01:15.040 so that you could decode the message. 15 01:15.040 --> 01:21.730 Now on a bigger scale if you've ever watched The Imitation Game or read about the Enigma machine, 16 01:21.730 --> 01:25.020 well that is basically a form of encryption. 17 01:25.330 --> 01:32.440 And the Enigma machine if you don't know is just simply a machine that was used during World War 2 when 18 01:32.440 --> 01:38.890 the Germans would send each other messages they would use the machine to encrypt those messages so that 19 01:38.890 --> 01:46.360 when the messages are intercepted say over the radio, unless you had the same Enigma machine and you 20 01:46.360 --> 01:53.200 knew what the decoding Key was or what the settings were for the machine, then you wouldn't be able to 21 01:53.200 --> 01:56.200 tell what it is that they were trying to communicate with each other. 22 01:56.350 --> 02:03.640 If you were interested, I really recommend watching two videos that were done by Numberphile on YouTube and 23 02:03.670 --> 02:09.150 I've linked to it in the course resources list but it explains the Enigma machine 24 02:09.250 --> 02:15.550 and it talks about the flaw in the Enigma machine that led Alan Turing and other people at Bletchley 25 02:15.550 --> 02:23.080 Park to be able to crack the code and create what was very much a specialised computer to be able to decode 26 02:23.080 --> 02:26.350 those messages and helped the Allies win the war. 27 02:26.410 --> 02:27.990 And if you ever visit London 28 02:28.000 --> 02:33.100 be sure to go and check out Bletchley Park and they have a computer museum next to it as well which 29 02:33.100 --> 02:34.840 is super fascinating. 30 02:34.870 --> 02:36.070 Anyways I digress. 31 02:36.070 --> 02:42.280 So back to ciphers and encryption. One of the earliest ways of encrypting messages that we know about 32 02:42.550 --> 02:44.710 is the Caesar cipher. 33 02:44.710 --> 02:52.450 And this comes from Julius Caesar who was one of the generals in the Roman Empire and what he did is 34 02:52.540 --> 02:58.060 he would send messages to his generals and he would encrypt it 35 02:58.060 --> 03:04.090 so if his messenger got murdered along the way then his messages would be kept secret. 36 03:04.090 --> 03:09.260 And this is one of the simplest forms of encryption we know about. 37 03:09.430 --> 03:10.540 And it's very simple. 38 03:10.540 --> 03:12.560 Let's say we have the alphabet right? 39 03:12.580 --> 03:18.520 ABCDEFG. All that the Caesar Cipher does it's a letter substitution cipher. 40 03:18.730 --> 03:24.790 And the key for the cipher is the number of letters that you would shift by. 41 03:24.910 --> 03:32.370 So if you knew what the shift pattern was then you could really quickly decipher the message. 42 03:32.380 --> 03:38.200 So if we were to encrypt the word Hello there's a really neat tool online that can help us do that. 43 03:38.230 --> 03:44.800 It's called cryptii.com and it's got two 'i's at the end. And you can basically choose the kind of 44 03:44.800 --> 03:52.000 cipher or encryption that you want to-- that you want to use and then you can specify a shift and we're 45 03:52.000 --> 03:54.130 going to see a shift of three let's say. 46 03:54.130 --> 04:02.140 So if my text was Hello, then it becomes shifted into "khoor" and to an unknowing person and a non cryptographer 47 04:02.500 --> 04:07.350 it can be quite difficult to see at a glance what exactly this is trying to say. 48 04:07.510 --> 04:14.740 Now in modern days and with modern cryptography this is overly simplistic and it's very very easy to 49 04:14.740 --> 04:15.580 crack. 50 04:15.730 --> 04:21.790 But there are other forms of encryption which are a little bit more complicated and it involves a lot 51 04:21.790 --> 04:26.900 more maths to make it more time consuming for somebody to crack. 52 04:27.040 --> 04:31.330 But essentially all encryption works exactly the same way. 53 04:31.450 --> 04:40.610 You have a way of scrambling your message and it requires a key to be able to unscramble that message. 54 04:40.630 --> 04:40.930 All right. 55 04:40.960 --> 04:46.360 So now that we've learned all about encryption, it's time to go ahead and implement it so that we can 56 04:46.360 --> 04:51.040 encrypt our users passwords on our database and keep it secure. 57 04:51.580 --> 04:57.130 So the npm package that we're going to use is something called mongoose-encryption. 58 04:57.550 --> 05:04.900 And if you take a look at the documentation it's basically a very simple encryption package that works 59 05:05.110 --> 05:06.220 with a mongoose. 60 05:06.250 --> 05:07.870 So it's perfect for us. 61 05:08.200 --> 05:14.710 So it uses an encryption algorithm called AES which is a relatively modern encryption algorithm 62 05:14.740 --> 05:20.610 and it is far more secure than something like the Caesar cipher which is very very easy to break. 63 05:20.620 --> 05:26.280 Now before we get started I recommend you just have a quick read of the documentation here. 64 05:26.290 --> 05:29.020 This package does two things: 65 05:29.020 --> 05:32.340 it can encrypt and it can authenticate. 66 05:32.350 --> 05:37.900 We're only going to use its most basic functionality which is encryption and we're going to leave the 67 05:37.900 --> 05:40.260 authentication for a later lesson 68 05:40.360 --> 05:43.420 when we discuss things such as hashing algorithms. 69 05:43.420 --> 05:50.260 So as always to begin we're going to go ahead and install that package into our project. 70 05:50.260 --> 05:50.530 All right. 71 05:50.560 --> 06:00.050 So first things first, let's stop our server from running and let's go ahead and install mongoose-encryption. 72 06:00.100 --> 06:03.810 Let's just make sure that we don't have any typos in that name and 73 06:03.910 --> 06:05.830 let's hit enter. 74 06:05.830 --> 06:13.990 So while NPM is installing that package we can go ahead and require it in our app.js. 75 06:14.020 --> 06:25.170 So I'm going to call this encrypt and we're going to set it equal require mongoose-encryption. All 76 06:25.170 --> 06:25.370 right. 77 06:25.730 --> 06:32.170 So now that we have the package required and installed, we're gonna go ahead and use it. 78 06:32.300 --> 06:34.260 And of course to figure out how we do that 79 06:34.280 --> 06:42.760 we go to the documentation. So it briefly describes how the encryption actually works 80 06:42.830 --> 06:46.540 and then it tells you how to get started. 81 06:46.610 --> 06:50.180 So we've got installation step which we've already done, 82 06:50.210 --> 06:52.850 so now we're onto the usage step. 83 06:53.210 --> 06:58.640 So firstly we have to require mongoose and require mongoose-encryption and then we have to change our 84 06:58.640 --> 07:05.840 Mongoose schema a little bit because notice that in their Mongoose schema they're actually creating a new 85 07:06.020 --> 07:12.350 Mongoose schema whereas ours is just a very simple Javascript object. 86 07:12.350 --> 07:18.770 Now this works as long as you're not doing anything fancy with the schema. But in this case, as you'll 87 07:18.770 --> 07:25.400 see later on, we're actually adding this encrypt package as a plugin to that Mongoose schema. 88 07:25.400 --> 07:31.250 So we're going to have to level up a little bit and turn our schema into a proper Mongoose schema object 89 07:31.850 --> 07:37.580 and in fact if you take a look at the schema section in the Mongoose documentation where they talk about 90 07:37.580 --> 07:43.880 defining your schema, you can see that they're also creating a new mongoose.Schema and then they're creating 91 07:43.970 --> 07:48.260 a new object from that class in order to define the schema. 92 07:48.260 --> 07:54.640 So let's go ahead and change our very simple version of a schema into a full Mongoose schema. 93 07:54.800 --> 08:02.060 So we're going to type new mongoose.Schema with a capital "S" and then we can open a set of parentheses 94 08:02.120 --> 08:05.450 to enclose this entire Javascript object. 95 08:05.450 --> 08:08.180 So one here and one here. 96 08:08.210 --> 08:15.740 So now this user schema is no longer just a simple Javascript object but it's actually an object that's 97 08:15.740 --> 08:19.580 created from the Mongoose schema class. 98 08:19.700 --> 08:24.950 So, let's go ahead and hit save and then let's see what else we need to do. 99 08:25.310 --> 08:32.390 So there's two ways of going about encrypting your database using this Mongoose encryption package. 100 08:32.450 --> 08:39.880 One way is to create an encryption key and assigning key. Alternatively and the one that we're going 101 08:39.880 --> 08:48.130 to be using is a little bit later down in the documentation which is why it's helpful to read the entire 102 08:48.490 --> 08:49.840 documentation. 103 08:49.930 --> 08:56.500 We're going to be using the convenient method of defining a secret which is simply a long string and 104 08:56.500 --> 09:00.750 then we're going to use that secret to encrypt our database. 105 09:00.790 --> 09:04.000 So let's go ahead and define that secret down here. 106 09:04.330 --> 09:11.620 So I'm gonna create a new constant that's called secret and this is gonna be set to a long string that 107 09:11.620 --> 09:12.850 you're going to keep secret. 108 09:12.850 --> 09:23.670 So let's say ThisisourLittlesecret. And now we're ready to use that secret to encrypt our database 109 09:24.110 --> 09:31.550 and we do that by taking that schema that we defined earlier on over here and we're going to add our 110 09:31.590 --> 09:39.190 Mongoose encrypt as a plug in to our schema and we're gonna pass over our secret as a Javascript object. 111 09:39.360 --> 09:46.170 So let's go ahead and implement this line of code in our app.js and I'm gonna put it right below 112 09:46.170 --> 09:46.880 here. 113 09:46.890 --> 09:53.580 Now it's important that you add this plugin to the schema before you create your Mongoose model because 114 09:53.580 --> 09:59.940 you can see that we're passing in the userSchema as a parameter to create our new Mongoose model, 115 09:59.940 --> 10:01.560 that's the user model. 116 10:01.650 --> 10:08.430 But before then, we're going to add our encrypt package as a plugin. And if you have a moment to spare, do 117 10:08.430 --> 10:13.690 read the part of the documentation on plugins on the Mongoose website 118 10:13.710 --> 10:20.850 and it talks about how essentially what plugins are is that just extra bits of packaged code that you 119 10:20.850 --> 10:28.240 can add to the Mongoose schemas to extend their functionality or give them more powers essentially. 120 10:29.040 --> 10:36.810 So now that our schema has this encryption power enabled, but what this will do is it will encrypt our 121 10:36.810 --> 10:38.160 entire database. 122 10:38.160 --> 10:44.520 Now you may or may not want that kind of behavior for your database because later on when the user logs 123 10:44.520 --> 10:50.010 in we're going to have to search through our database to find their email address. 124 10:50.040 --> 10:56.850 It's best if we only encrypt the password field and leave the email field unencrypted. 125 10:56.850 --> 11:03.360 So to do that, we have to change an option for our Mongoose encryption package and the option that we're 126 11:03.360 --> 11:11.640 going to change is to only encrypt certain fields and they cover that in this section of the documentation. 127 11:12.060 --> 11:20.040 And to do that all you have to do is to add this option "encryptedFields" into that JavaScript object 128 11:20.130 --> 11:22.980 at the end of that plugin code. 129 11:23.250 --> 11:29.910 So we're going to copy this little part here and we're going to add it just before the closing curly 130 11:29.910 --> 11:30.480 brace, 131 11:30.510 --> 11:32.760 so right after our secret 132 11:33.000 --> 11:39.940 right here. Now in our case the encrypted field is not age but it's going to be the password field. 133 11:40.050 --> 11:47.520 So we're going to swap out this particular input with password and that's just a simple string that 134 11:47.520 --> 11:52.650 you put in there that you have to make sure matches with one of the names of your fields. 135 11:52.650 --> 11:59.100 Now if you wanted to encrypt multiple fields you can also do that just by adding other entries into 136 11:59.190 --> 12:00.900 this array. 137 12:01.380 --> 12:08.940 So now that we've added our encryption package to our userSchema, we've defined the secret that we're 138 12:08.940 --> 12:15.730 going to use to encrypt our password and also the field that we actually want to encrypt, 139 12:15.750 --> 12:17.540 we're pretty much done. 140 12:17.820 --> 12:24.870 So we don't actually have to do anything else special in the register or the login sections because 141 12:25.260 --> 12:33.300 the way that Mongoose encrypt works is that it will encrypt when you call save, and then it will decrypt 142 12:33.540 --> 12:35.180 when you call find. 143 12:35.190 --> 12:41.640 So that means that when we create a new user with an email a password and we call save, automatically 144 12:41.640 --> 12:49.260 behind the scenes Mongoose encrypt will encrypt our password field. And when we later on try to find 145 12:49.380 --> 12:57.180 our document based off the email that the user has entered then at this stage Mongoose encrypt will decrypt 146 12:57.300 --> 13:02.400 our password field to be able to check it in this step and see if the user can login. 147 13:03.000 --> 13:11.720 So let's save our file and let's go ahead and run it by using nodemon 148 13:12.030 --> 13:20.190 app.js and make sure there's no errors on running it. Then we can go ahead and hit up localhost:3000 149 13:20.640 --> 13:22.830 and try to register. 150 13:22.830 --> 13:26.310 So we've already created a user called 1@2.com, 151 13:26.310 --> 13:34.160 let's create a2b.com now and let's define their password as qwerty, 152 13:34.260 --> 13:42.360 so the first six letters of the keyboard and believe it or not this is one of the top five passwords 153 13:42.390 --> 13:48.330 that's used by people all across the world along with the actual word password 154 13:48.480 --> 13:50.930 and 123456. 155 13:50.940 --> 13:56.940 Now if I have read out your password just now, please change it while you learning security. It's a good 156 13:56.940 --> 13:58.620 idea to update your own as well. 157 13:59.400 --> 14:04.790 So let's go ahead and hit register and we've been taken to the secret page. 158 14:04.830 --> 14:09.780 So that means we've successfully saved our new user into our database. 159 14:09.780 --> 14:16.770 So let's take a look at that user inside Robo 3T. So we can see that previously when we didn't encrypt 160 14:16.830 --> 14:20.330 our password it's just out there in plain sight 161 14:20.330 --> 14:23.870 basically. Anyone can read this user's password. 162 14:24.650 --> 14:33.230 But now that we have added our encryption, you can see that it's now a very long binary string that is 163 14:33.230 --> 14:37.520 very hard for anybody to guess what it might be. 164 14:37.520 --> 14:44.570 So that means if somebody hacks into our database they won't be able to skim everybody's password immediately 165 14:44.840 --> 14:46.190 like they can do here. 166 14:46.520 --> 14:50.680 So we've updated the security for our users just a little bit. 167 14:50.720 --> 14:54.390 So let's see how Mongoose encryption handles decryption. 168 14:54.380 --> 14:58.120 So let's go back to the home page and try to log in our user 169 14:58.130 --> 15:04.740 that was a@b.com and let's put in their password which was qwerty. 170 15:04.920 --> 15:05.790 Let's go ahead and click 171 15:05.790 --> 15:06.480 Login. 172 15:06.480 --> 15:15.000 So it's taken me to the secret website and we know that on our database that password is stored encrypted. 173 15:15.000 --> 15:22.290 So that means that when we perform that findOne step, Mongoose encrypt was successful in decrypting 174 15:22.290 --> 15:26.010 the password to be able to compare it at this stage. 175 15:26.010 --> 15:31.800 And if you want to, you can actually log the value of foundUser.password inside the 176 15:31.830 --> 15:36.150 findOne completion block and you'll see it in plain text. 177 15:36.750 --> 15:45.780 So this does mean that if somebody was to hack into your website, then they probably will get access 178 15:45.840 --> 15:46.770 to your app. 179 15:46.910 --> 15:49.830 js. It's not that hard to access it. 180 15:49.830 --> 15:53.280 And once they do, they'll find your secret. 181 15:53.410 --> 16:02.010 And once they've found your secret then they can use the same package to decrypt all of your users passwords. 182 16:02.010 --> 16:09.570 So as long as we're able to recover the plain text version of our users passwords we're still kind of 183 16:09.570 --> 16:10.740 leaving them out to dry. 184 16:11.340 --> 16:18.060 So in the next lesson I want to cover something called environment variables and we're going to learn 185 16:18.060 --> 16:26.880 that to enable us to store secrets such as our encryption keys or things like API keys that are tied to credit 186 16:26.880 --> 16:27.670 cards, 187 16:27.720 --> 16:31.320 anything that you want to keep secure essentially. 188 16:31.320 --> 16:34.440 So for all of that and more, I'll see you on the next lesson.