 Next talk here. So our next speaker is a gentleman named Adam Hopkins. He is, let's see if, okay, so he is a builder of web applications for 20 plus years. You have probably used his code because he's the maintainer of the async.io web server and framework known as sanic, which learning about that led me to look up a while back, look up where that term came from. I love it when they name a library after a meme. That just makes me really happy. He's a self-styled authentication nut and he's going to be talking about, as you can see from his slide there, he's going to be talking about overcoming access control in web APIs and addressing security concerns using sanic. So all of you API lovers are going to really enjoy this one. So everyone, please welcome Adam Hopkins. Hello everyone. Okay, as I said, my name is Adam Hopkins. I am a senior software engineer with packet fabric. Packet fabric is a network as a service platform that provides cloud connectivity solutions and has recently been named one of the top 10 hottest networking startups. As I also said, I'm one of the maintainers of the sanic project. And I'm going to be using that to kind of showcase my talk here, but it's not really a sanic talk itself. So with that said, we're going to be talking about security mainly, but we're not going to be talking everything. We're not going to talk about TLS certificates, how you should maintain passwords and information, sensitive information, how you should store that, or how you should keep your server secures, SQL injection, all that kind of stuff. I'd be very happy to talk with you about, but we'll take that offline. And what we really want to talk about is these two things. We want to talk about authentication and authorization. So what is authentication? We're talking about, do I know who this person is that's trying to access my API? That's the first step. That's this question here, that is the person logged in. If not, we're going to send them an unauthorized message. If they are, we're going to ask them a second question. We're going to ask whether or not should I let this person in and the same thing. If not, we're going to throw them an error message. So real quick, that's basically what we're talking about here. So here is my endpoint. As you can see, we're going to serve up some really super top secret information. We don't want anybody to know this, that foo is bar. So we're going to try to protect this. And this is how we're going to do it inside of Sanic. Right now, if I were to hit that endpoint, it doesn't really do me any good, because anybody that hits that endpoint is going to find out foo is bar. So how are we going to protect that? Sanic is very similar to Flask in that it uses decorators pretty heavily. So we're going to create ourselves this protected decorator. What is protected need to do? It needs to do protection. If we can get through this do protection and we're happy with what happens, then we'll go ahead and we'll execute our handler. If not, we're going to bail out and we'll serve up our error. So we've got this function up here and we're going to try to figure out what that should do. Now, I just said that we're going to do this using decorators, but there's also a very other valid way to handle things. One of the things that Sanic sort of prides itself on is not being very opinionated and try to leave as many tools to the developer as possible because after all, this is your application, this is your API. So a very other valid way to handle authentication would be to create this middleware and on every single request before it even gets to the handlers, we'll execute this middle error and do the exact same do protection method. So this is certainly a valid option. So this is probably the most important thing. If you walk away with nothing else from this conversation, remember, authentication, failure is a four one and that's unauthorized. It's weird. I get it. It's sort of this legacy thing that's hang over from the early days when the World Wide Web is still like the Wild Wild West. So authentication leads to unauthorized and authorization failure is forbidden. So remember that. So Sanic is going to provide us with a very easy way to do both of these things. So we've got these two exceptions. We're going to run due to protection. If we fail authentication, you raise unauthorized. If we pass and we fail authorization, then we go on to forbidden. Okay. So we know currently as we're set up, it works. It does what we want. So the question is, how are we going to determine authentication? And this is sort of the meat of the talk here. So inside of HTTP, there's a few different common ways of handling authentication, but we're going to eliminate right off the bat three of these basic and digest. Again, they're sort of legacy. They have some security issues and they're not really meant for handling the type of APIs that we're dealing with. OAuth, great tool, probably a conversation for its own 30 minute talk at another time. So again, this is something we could talk about offline. A lot of the concepts that we're going to talk about here are going to be applicable, but it's sort of a framework in its own right. What we're really talking about here are bearer tokens and these are things that go inside of your headers and session tokens. But I want you to forget that. Everything that you already know, you already have a preconceived notion of sessions, cookies, headers, take that and just put it aside for a minute because we're going to sort of build up our information knowledge again. Okay. So first we're going to talk about this sort of hypothetical that I have. We've got a train and our train offers two different types of tickets. You can have a session ticket or a non-session ticket. And something else that's going to be peculiar about our train is every single time that our train stops at a station, our conductor is going to check every single person's ticket to make sure that they're still valid. Okay. So how's this going to work? Inter-session based. It's a single ride. You buy your ticket. You're going to go from one place to another place. You're going to get off and that's it. You can't use that ticket again. You can't. You have to buy a new ticket if you want. And every single time that you get to a station, the conductor is going to take his tablet. He's going to look up your ticket number. He's going to say, yep, it's in our database. Yes, this is still valid. We haven't gotten to the endpoint. You can stay on the train. We're going to contrast that with what I'm calling non-session tickets. You're probably not going to see this language elsewhere. So don't go start Googling this because you're not going to really see this. This is sort of the all day pass. This is, I can get on and off as long as I want, as long as my ticket hasn't expired. And the really nice thing about this for the conductor is he's going to look at your ticket and just by looking at the face of it, he knows whether or not he issued this. He knows that this is valid. It hasn't expired. He doesn't have to go looking up inside of a database or anything like that. So how do those session-based requests work? Our client is going to initiate a login with some credentials to our server. Our server is going to check them to make sure that they're okay. And it's going to create this thing called a session. And it's going to store it somewhere. And it's going to give a session ID back to our client. And every single time that that client wants to make a new request, they're going to use that session ID. We're going to go look that up in the database. And then we're going to deliver the information. So this is a very tried and true method. You find this all over the web currently. So the next thing we've got is these non-session based. And this is where we supply credentials. We're going to generate some kind of a token. We don't need a store this somewhere. There's no data store. And every single time that token comes back, we can just look at it to know whether or not it's valid and whether or not we want to provide the information. Okay. So let's take that information and hold on to it. We have sessions and non-sessions. The next thing we're going to decide is what's our strategy going to be. And we've got three questions here. Who is going to consume our API? Is it going to be a script? Is this going to be some sort of application? Is there going to be somebody inside of a curl command? Do we have control over that client? And will there be a web browser that's going to be trying to get information from our API? And really what we're trying to get at is sort of that last question. Is this a direct access API? Is this a browser API? Or is it going to be both? Why do we want to know that? Well, first of all, direct APIs are a lot easier to handle. Number one, we were dealing with usually a more technically sophisticated user. It's usually going to be a programmer that's going to be going to be doing this. And we kind of assume that they're doing what they can to protect their tokens and their keys. So this is sort of the typical scenario. Maybe we've used one of these web-based email providers. And you want to be able to send out an email. They're going to provide you a key. These login credentials, you're going to send a request. You're going to send them that key. And you've got access to the API. Then on the other side, we've got the browser-based. Now, this is really sort of our problem. The browsers are sort of built not really to handle authentication the way that we need to do it these days. And because we have got lesser technically sophisticated users who might have installed a bad plugin or maybe the websites itself got security flaws, we've got these two things called CSRF and XSS. Going into details about exactly what they are. Let's do that in the conversation afterwards. But these are the two things that we're really trying to protect against. And these are the things that we're concerned with. So we want to figure out how do we solve for that? Okay. So the problems with browsers is these two questions here. How is the browser going to store my token? Is it going to put it into a cookie? It's going to store it somewhere. These are different tools that are built into the JavaScript libraries. And because they're built into JavaScript libraries, JavaScript can access them. If JavaScript can access them, then sort of the bad guys can get at them too. And that's really what the XSS attacks are that we're trying to figure out. Now, the other thing we have to talk about is how is our browser going to send those cookies back to us? We've got two options. We've have cookies and we have authentication headers. And so how is this sort of typically handled? How is if you go into Google and try to figure this out, what are they probably going to recommend? What are they going to say if you've got a session token sticking in a cookie? You know, yeah, we've got to deal with this thing called CSRF. But we're going to solve that with a header. Now, inside, I've put in a link in the discussion form to repo and inside there, there's an example that goes into how you can do this. So let's, again, let's talk about that kind of offline and look at that example. And we can talk about CSRF. But just know that we're going to solve for the session-based cookies by using an XS RF token header. Now, what you might find out there is people are going to say, well, you've got a token, like a JWT token. Well, how are you going to do that? You're going to send it over an authorization bearer token. The problem with this is that we're still open to XSS attacks. So let's do a little recap. To answer the question of how to be authenticated, we need to answer, you know, know about session versus non-session. We need to know about direct APIs versus browser APIs. And we needed to know about different types of tokens we have here. Okay, so we know if we're going to have a direct API, we can use an API key in the authorization header, and we feel okay that that's going to be safe. Or we can put a session ID inside of cookies in the browser, and that's going to be safe. But what about when our API has to do both? What if we need to serve both a direct API and a browser, and we don't want to have two different methodologies to sort of authenticate? How are we going to do this without over complicating our application? Second question is, what if we want to use JWTs inside of our front-end framework? How can we do that safely? Well, I'm going to tell you that we can. All you need to do is you need to take the right pill here. So let's talk a little bit about what a JWT is. This is a mess of characters, but we'll notice that there are periods inside of a JWT. And these periods break up the JWT into three different parts. I've color coded them here into blue and purple. And those represent three bits of information. The orange is our meta information. This tells us how we can read the rest of the token. The blue is sort of the money. This is really where the value of JWTs come in, because it allows us to take some actual usable information about who has logged in and actually serve that information back. And then lastly, we have the signature here. So it's really that sort of bit that allows us to take a look at this and know whether it's valid just on the face of whether or not we can look at it. Because basically what's going to happen is we're going to take this whole thing and we're going to create this signature. And it's going to be based off of a security secret that is going to only be known to our server. And so our server is the only one that's going to be able to generate this based off of the rest of this information. So how are we going to handle this? Well, what I'm going to suggest is let's create two cookies. We're going to have one cookie that's going to be called our access token. And that's just going to be the first two parts of our JWT. Then we're going to have the second part that is going to be what I call the access token signature. And that's just that third part. So we've broken up our JWT into different parts. And really the important thing to note here is this is HTTP only. And this is a browser feature. All major browsers support this now. So we can rely upon it. And what that's going to do is it is going to say, yes, I have a cookie, but this cookie can only go over HTTP requests. JavaScript cannot get at it. So that's great. Problem is, if our entire cookie were HTTP only, then we can't get at this section here in blue that we do want our front end application to be able to get at. So that's why we don't have HTTP only here. And we do have it down here. So cookies, cookies seem like they're going to do the job for us. So how can we handle this inside of our Python code? And this is, and so what we need to do is we're going to take our token, and we're going to split it based on that on that period. And to two parts, we have the header and the payload, we have the signature. And then all we're going to do is we're going to create cookies, we're going to create an access token with our header and payload. And this is the important thing that I just mentioned, HTTP only is false. And then our signature, and we're going to put HTTP only on for that one. Now, I've also put in the CSFR token here, because this is typically, you're going to want to do it at the same time. And again, this needs to be false here. And the reason why is the way that we get that CSFR security is we're going to, we want to be able to read that from our JavaScript. And we want to be able to insert that into a header when we make the request. And the reason why we're going to do that is because now we know that it came from our JavaScript. Okay, so this is sort of how you would set cookies inside of Sanic. It sort of acts and looks like a dictionary, but not really because as you can see here, you know, this value is just a, it's just a string. So it kind of looks like it, but just know it's, it's something a little bit different. So we can, we can say our cookie key equals value and then set all this sort of meta information on it afterwards. So it looks like we have a winner, we solve the problem, we figured out how we can use a JWT. And the solution is with two cookies, we're going to put, we're going to store two cookies, we're going to send two cookies. And then we've got this header for CSFR protection here. So we've, we've sort of solved both of these problems. And we've got our nice little green check mark there. So what is this going to look like inside of those handlers that we set up at the very beginning? Well, when we do execute is authenticated, we need to get that token. So how are we going to do that? We're going to go back into our cookie again. It's actually at this point when we're inside of a request that actually is a dictionary. So, so we've got to, we've got, we've got our tokens that we can, we can take here and we can rebuild them back into one single thing. So once we do that, we can try and decode it. Now this decode method is going to throw up all sorts of exceptions if that JWT is not valid. So all we need to do is just listen for the exceptions and return either true or false. So that's pretty simple there. Okay. So this is back, this is that, that, that thing that we're going to do inside of our protection method. So we, we just figured out how we can do is authenticated. The next question is how are we going to do is authorized. So we've got our, we've got a method here. And this is where I'm going to introduce something. It's a little bit different. This is something that I call structured scopes. Now a couple years ago, when I was trying to build an API, I figured, okay, that's great. Let's go. And let's add some scoping. I want to have certain users be able to take this X endpoint, but not this one. And how is this done? And I, I spent a lot of time trying to figure out exactly what's the standard out there. And I was really actually kind of disappointed to find out that there was no standard. So I went, you know, and try to figure out what's the best way that, that to do it. And I came up with this idea. Maybe one of these days, if I ever, you know, find the time to do it, maybe I'll submit an RFC or something about it. But basically what we've got is a string of characters, and we're going to separate it with these colons. The first one is our namespace and everything after that is going to be an action. So we're going to have two of these. And we have, we have, this is what we're going to validate against. And this is what we've got. And we've got a check mark. So this is sort of what it looks like. Our requirement says we have user read. And incoming, we've got user read right. And this is valid. So we're just going to stick this inside of our, our handler. So now all we need to do is put this on our endpoint. And we can just proceed on with JavaScript, we can, we can feel, you know, comfortable that our cookies are going to be sent back and forth. That's great. But is there a better way? And that's this package here that I, that I created. It's going to set up a lot of this stuff for you. So the important things that I want to point out to you here is we're going to be sending our tokens with cookies. We're going to splitting them up into two parts. But we're also going to say cookie strict equals false. And this allows us to fall back to those authorization headers. So now our API is able to do both the direct API and the browser based. SanHJWT added these three, these three endpoints for us. So this is what our, what our whole application looks like currently with fairly minimal code. And that is sort of, that's, that's it. That's what I've got for now. Any questions, I'd be happy to answer them here or in the, in the channel later. Adam, thank you so much for that excellent talk. Okay, come on computer. It's, it's doing it to me again. Oh, this is always embarrassing when I think of things that work correctly. Here we go. Yeah. All right. And I do in fact have a couple of questions here for you. So Goose asks, why do we not need to store a token for a non session based ticket? Why do it's not a question of do we need to the, the, the, it's, it's we're trying to figure out if there's a way that we can avoid having to have session tokens. So in one side of the one world, we need to check sessions and another world we don't one of the, one of the sort of advantages of not having it is we just took away a round trip call to make to our database. So with, with session tokens, you've got this extra request that you have to make on every single request to the database and doing that, you know, adds more network time. So that's going to inherently slow down your application. So one thing that you do gain by this is, is by reducing, reducing that and you can, you can make your application a little bit faster. Okay. Cesar asks, he's a sorry, is it working with HTTP2 or HTTP3 or is it not dependent on that? It's a little bit, it's a little bit HTTP1, 1, 1, 2, 3, it's sort of a little bit of a different conversation. That's going to be more at the server level. So you can, you can run, say like over HTTP2, but it would, it would handle this stuff pretty much the exact same way. The one thing to know about HTTP2 is you're sort of forced into using TL certificate, which is a good thing because that's going to have obviously add some additional security layers for you with encryption. All right. Once again, thank you very much.