<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Efe on Medium]]></title>
        <description><![CDATA[Stories by Efe on Medium]]></description>
        <link>https://medium.com/@efedortluoglu02?source=rss-8a175c9c50c5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*SRt7pdC-ASJsmEgN</url>
            <title>Stories by Efe on Medium</title>
            <link>https://medium.com/@efedortluoglu02?source=rss-8a175c9c50c5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 02:27:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@efedortluoglu02/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Security on Web Development]]></title>
            <link>https://medium.com/@efedortluoglu02/security-on-web-development-53aad012ccaf?source=rss-8a175c9c50c5------2</link>
            <guid isPermaLink="false">https://medium.com/p/53aad012ccaf</guid>
            <category><![CDATA[web-security]]></category>
            <dc:creator><![CDATA[Efe]]></dc:creator>
            <pubDate>Mon, 13 Mar 2023 10:27:49 GMT</pubDate>
            <atom:updated>2023-03-13T10:38:36.414Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Authentication </em>and <em>authorization</em>, these are the two fundamental parts of security.</p><p>We can think of authentication like an identity check, what I mean by that, it checks whether you are a registered user i.e., whether your credentials are stored on the database or not. As for authorization, after you are authenticated, in other words you are logged in, the services you want to use, will have certain conditions, like a <em>role </em>or a <em>policy</em>, that are required to be satisfied in order to be used. To make it more concrete, we can think of a service that requires only users who are assigned as an admin as for a role or users who are over a certain age or from a certain country.</p><p>Before we deep dive into our main concern, it is necessary to explain and understand some related topics like hashing, cookies and web tokens. So let’s begin.</p><p><strong>Encoding, Encryption, Hashing</strong></p><p>Another point is how passwords are stored on the database and how data is transferred between client and server. Some methods are encoding, encryption, hashing. So let’s explain them.</p><p><em>Encoding </em>is simply converting a data from one form to another and helps securing data by no means i.e. totally reversable. What I mean by totally reversable is that we can think of encoding like a literal mapping i.e. “A” corresponds to 65 and vice versa in ASCII. With that, as long as which encoding algorithm is used for the data is known, we just can simply convert it to the plain-text form. Some examples are: ASCII, BASE64, UNICODE.</p><p><em>Encryption</em> is converting data to another form with the help of a <em>key. </em>As long as the key is considered secure, so is the encryption. Encryption is reversible only if the key is known, yet this is not safe enough for storing passwords. So we use hash functions.</p><p><em>Hashing </em>is converting data to a hash value using some <em>hashing functions</em>. An interesting point here is that even if the original data is the same, every attempt to generate a hash value will result in a different sequence, yet still comparable. This is done by a random value called <em>salt, </em>added to the original data before hashing. So as long as we also store the salt, we can join it to the credentials we want to verify and get the same hash value. That helps us combatting storing ‘common passwords’, like ‘12345’, on the database with the same values. Now we can use hash values instead, so that even if a hacker got access to our database s/he has to ‘find out’ each password individually even if they are identical.</p><p>Here are two hash values of ‘12345’ generated by using <em>Bcrypt </em>function. Although they appear different of the surface, they have the same internal value which is ‘12345’ that will be used for comparison. So matching ‘12345’ with them will require two different salts.</p><p>$2a$12$PvkgYxAZF2xrBKCB.U6bEegPZSLVJXl7yamSJEzSsuemD4EbqavbW</p><p>$2a$12$gDX3IebqI2miEFwsjv.4ZOSDaBOiIZiFzHBajP/uPKLXPrhGanw/m</p><p><strong>Web Cookies and Sessions</strong></p><p><em>Cookies </em>are small text files stored on the user’s device by the web browser. They contain information like login-credentials, website preferences like language etc. They enable us to use <em>stateless http request</em> in a ‘<em>stateful</em>’ manner. With the help of them we can personalize websites such as advertising and also since they hold the login-credentials, we do not have to log in every time when we visit the website. These <em>long-lasting</em> or <em>persistent cookies</em> are utilized by social network sites or to track the user’s browsing behaviors.</p><p>On the other hand, another type of cookies are <em>ephemeral cookies</em>, also known as <em>session cookies</em> are used by websites having high-level security concerns like banking sites. They are deleted after the browser is closed or after a predetermined short amount of time.</p><p>When we log in a website, the server creates a session id which is a unique identifier of the cookie. Moreover, unlike cookies session ids are stored on the server. Then the server will create and send a cookie with that id to the browser. Later on, whenever we go to the page again, we will also send the cookie within the<em> http header</em> and the server will try to match the sent session id with the ones stored on the database.</p><p>It is worth noting that cookies cannot be used with another domain. This causes some problems especially in horizontal scaling and storage. So, consider a banking service which has two banking servers and another server dedicated to retirement issues. When you log in to<em> banking-server-1</em>, you will get a cookie from that server which can only be used within it. If you need to use a retirement service, you will be surprised and sort of angry. Because you are required to log in again, despite the fact that you already logged in the main service. That is just because, the session id in the cookie that is given to you by <em>banking-server-1,</em> is not stored on the <em>retirement-server</em> and due to that it cannot verify your credentials. Another issue is that when you are logged in to <em>banking-server-1</em>, it may get too many requests, so the requests are split between <em>server-1</em> and <em>server-2 </em>and you may redirected to <em>server-2</em>. However, caused by the same reasons stated above, you cannot use our <em>on-server-1-created-cookie</em> on <em>server-2</em> and required to log in again. Of course you may share sessions ids across multiple servers but this will just make it harder to maintain and just take up more space. The solution to these problems are provided by JWTs which we will talk now.</p><p><strong>JSON Web Tokens</strong></p><p>The structure of JWTs are like the following:</p><p><strong>[Header].[Payload].[Signature]</strong></p><blockquote>eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c</blockquote><p>Header is the part where information like signing algorithm and type of the token are contained. It is encoded using BASE64URL encoding.</p><p>Payload is the part where the actual claims are contained. Some examples are issuer, expiration date as well as application-specific claims. It is also encoded using BASE64URL encoding.</p><p>Signature serves as a way to ensure the token’s integrity and it has not tampered with. It is obtained by combining encoded header and encoded payload and hashing them with a hashing function like HMAC or RSA, using a key only known to the issuer and then encoding hash value with BASE64URL. When the server receives a JWT, it can recompute the signature with the received-payload using the same secret key stored on the server. Then server compares it against the signature within the JWT. If they do not match, we can infer that the received-JWT(payload) has been <em>modified </em>in transit.</p><p>As I said before, problems with cookies are resolved by JWTs. Let‘s flesh it out a little. Taking into account that, to verify a JWT we only need to know the secret key which most of the time will be unique in a single app, and because of that, nothing has to be stored on the server. That means all the user information are stored on the client-side. That enable us to use JWTs across multiple servers, provided that the secret key is shared among them. Along with that, we never have to deal with storage issues, since nothing is stored on the database.</p><p>And a little side note is that tokens are used for authorization, i.e., after we are signed in.</p><p><strong>Authentication</strong></p><p>And now let’s turn back to our main topic by explaining security context.</p><p><em>Security context</em> helps us to store all information that the user has for security purposes like e-mail, username etc. All these information is encapsulated within an object called <em>ClaimsPrincipal.</em><strong><em> </em></strong><em>Principal </em>can be thought as a user which can have one or more identities, but only one at a time. Those identities will have, so-called <em>claims</em>, key-value pairs like “password: 12345”.</p><p>What carries out our actual authentication process is a method(inherits from an auth. interface) called <em>authentication provider</em>. Within the app, we may have different <em>authentication schemes</em>(the ways in which a user can authenticate) like JWTs, OAuth(e.g. Google, Facebook) or OTP. Therefore, we cannot bound ourselves to just one authentication provider. Since we have multiple authentication providers, something has to to <em>delegate </em>the call to the correct authentication provider. This is done by <em>authentication manager</em>, which checks each authentication provider one by one to decide whether it supports the current call or not. When a client contact the server, after all processes, authentication provider will return a token if it verifies client’s credentials.</p><p>So, when we submit our credentials for username-password authentication, the server attempts to verify the credentials. If it is valid, then server creates a cookie or a token, depends on our configurations, and send it back to client. Client stores the token and send it to the server along with every subsequent request. If server validates that the token is coming from an authenticated user, it will send the response to client.</p><p>Let’s give some more details about this process. After the credentials are received, it is authentication manager’s duty to decide to which authentication provider delegate those credentials. It checks all available providers until it finds a supported one, in this case username-password provider. Then, it loads the user by sent-username along with the hashed password and the key. Lastly, it hashes our sent-password with the key and if they match, it creates <em>a principal</em> and stores it in the <em>HttpContext.User</em>. Also, creates a token or a cookie and send it with the response.</p><p><strong>Authorization</strong></p><p>Now, as for <em>authorization</em>, as I said before, is the process of determining to which resources a user is granted to access. We can have <em>a role based</em> and <em>a policy based</em> authorization.</p><p><em>Roles</em> are simple <em>labels </em>to categorize a set of users that have common access to certain services or rights. We can think of an admin, student, teacher roles.</p><p>On the other hand policies are more broad than roles. We can think of them like a set of rules that must be met in order to perform a specific action or to access a specific resource. For example, think of a contest that can only be participated if you are a university student from a Mediterrenian country. So the required policies are: student-role, being a university student, and coming from a Mediterrenian country. We can fetch these <em>‘claims’</em> once the user is authenticated.</p><p>An advantage of using policies rather than roles is that, policies are more flexible and can be tuned in much more details. Also, as we have seen, policies spans roles. So, we do not lose any functionality of roles, as we use policies.</p><p>To implement our authorization requirement, we configure the <em>authorization middleware</em> and add the authorization policies to the <em>controller</em> of the specific resource.</p><p>Each request will be <em>intercepted </em>by our filters before it reaches the service-layer and checked for authentication and authorization. If both is suitable for the requested resource, then the response will be sent.</p><p><strong>Conclusion</strong></p><p>So, that was a brief introduction to web security. Thanks for your time, see you next time =)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=53aad012ccaf" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>