Skip to content

Commit 8ba0cc2

Browse files
authored
Merge branch 'main' into cx-fix-atom-exhaustion
2 parents 044a0ac + b09ad7e commit 8ba0cc2

9 files changed

+356
-16
lines changed

modules/1-introduction.livemd

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ This Training material is also ideally used in an educational environment for or
1818

1919
If you've never used an Elixir [Livebook](https://livebook.dev/) before, you're in for a treat! They are a very exciting new tool that is actively under development - very similar in application to [Jupyter Notebooks](https://jupyter.org/), but for the Elixir ecosystem!
2020

21-
It would not do the Livebook any justice to try and summize here how to fully take advantage of all its capabilities, for a better introduction there is a great tutorial offered in local installations of Livebook.
21+
It would not do the Livebook any justice to try and summarize here how to fully take advantage of all its capabilities, for a better introduction there is a great tutorial offered in local installations of Livebook.
2222

2323
**For the purposes of this Training material, just know that you need to run the "Setup" step for the "Notebook dependencies and setup" section at the very top of EVERY module before running any code samples found within the module you're working on.**
2424

modules/11-authentication.livemd

+194
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
# ESCT: Part 11 - Authentication
2+
3+
## Introduction
4+
5+
Authentication is the process of establishing that an entity, whether person or machine, is who they say they are. In this process the entity starts by "knocking on the door", and presenting their id card and credentials when prompted.
6+
7+
One of two things follow:
8+
- The authentication attempt is successful and further communication including the granting of access can proceed.
9+
- Authentication fails and other that notifying the entity of the failure, no further communication proceeds and no access is granted.
10+
11+
Imagine you get a knock on your door. You ask who it is, and the person on the other side says you have a package delivery. In fact, you're not expecting a package and you can see the person is not wearing a uniform and doesn't have a package in their hands. Something doesn't match. They don't seem to be who they say they are and so, you don't open the door.
12+
13+
Authentication is the mechanism that helps guard the front door of an application. It's the mechanism that helps control who gets into your system and if they are there legitimately.
14+
15+
## Table of Contents
16+
17+
* [Confusion with Authorization and Access](#confusion-with-authorization-and-access)
18+
* [Multi-factor Authentication](#multi-factor-authentication)
19+
* [Token-Based Implementations](#token-based-implementation)
20+
* [Sessions](#sessions)
21+
* [Authentication and Security Concerns](#authentication-and-security-concerns)
22+
* [Prevention and Countermeasures](#prevention-and-countermeasures)
23+
* [Quiz](#quiz)
24+
25+
## Confusion with Authorization and Access
26+
27+
### Description
28+
29+
Thinking back to the example above, authentication is establishing an entity is who they say they are. For applications, this means, the user who is attempting to login, is the user who created and has control over the account. But most applications have multiple levels of users, those with maximum access/privileges to move around and modify the application freely, and those with more restricted access.
30+
31+
Once an entity has been authenticated, then they are granted access but when implemented in an application/system, this often appears to happen in a single step. Users login and if you get a successful response you also get access to the application. Access immediately follows Authentication, but how much access an entity is allowed and the actions they are permitted to, is authorized, to perform are governed by a set of permissions or access controls referred to as Authorization, which is often managed by a token or similar credentials.
32+
33+
During the authentication and authorization process, validity of credentials and level of access are checked. Then, depending on the architecture of the system or application, once an entity is authenticated, is granted access, subsequent activity/interactions need to be tracked/attributed to the same entity. This functions like a hand stamp for re-entry to an event or amusement park accept it is unique to you. For applications, this means setting up and tracking an authenticated user's session, and this is often done using some kind of token, sometimes the same token that gets issued for access.
34+
35+
Authorization, Identity, Credentials, Access, Access Controls, Permissions, Session and Session Management are all terms you will come across when implementing Authentication in applications. While each has distinct definitions, consider them as mutually interactive contributors to an integrated system that works to allow into an application only what is verified and trusted, tracks and monitors the activity of what's been allowed in, and ensures what does get in, only has access to what they absolutely need in order to perform their specific function. How these are implemented and their specific configuration is unique to the design of each application.
36+
37+
## Multi-factor Authentication
38+
39+
### Description
40+
41+
Multi-factor Authentication (MFA) is a way of implementing authentication so that more than one aspect of an entity is checked when it presents itself. from the initial simple example, checking both the information on the id card (something they have) and asking the entity to verbally state their name (something they know), for instance.
42+
43+
When implemented in applications, these aspects are referred to as factors and Authentication can be implemented using one of these factors (single-factor) or 2 or more (multi-factor):
44+
45+
Something you know/that is in your brain - Password
46+
Something you have/possess/have physical or digital access to - Code generated by outside party; key
47+
Something you are/something unique to you as a person - fingerprint, facial recognition, other biometrics, palm scan, retinal scan
48+
49+
Authentication mechanism can be simple or complex. Security industry best practices recommend implementing multi-factor authentication wherever possible.
50+
51+
## Token-based Implementation
52+
53+
### Description
54+
55+
We mentioned earlier how both authorization (access) and sessions can be handled using tokens. Access Tokens are built so that they contain information about what an authenticated user does and does not have access to, for how long, and they can also be used to manage the user's persistence/ongoing interactions with the application in a session.
56+
57+
Tokens are long strings of random characters used to identify an entity, session, as a badge for access and are usually generated by some token generating code, service or server. In token-based implementations, at a highlevel the application or service generates tokens, assign token to users after they have been authenticated, check token validity as users access and use application functionality/features, and end/renew sessions by expiring and refresh tokens.
58+
59+
### OAuth
60+
Open Authorization(OAuth) is a protocol in which a multi-step arrangement generates a token for a specific users, the user presents as a credential in lieu of a password. There is an extra server (authorization/token generating service or server) that after a user authenticates with it, it generates a token, and brokers authentication/authorization between initial entity and a resource.
61+
62+
Originally built for authorization, as it's name suggests, it has evolved for use in the authentication and authorization mechanisms. A very good resource that describes the OAuth in context of it's history and current implementations is here: https://www.youtube.com/watch?v=996OiexHze0
63+
64+
Why use OAuth? When users need access to third party services, outside of your environment where you don't want to share your credentials with those third parties. In OAuth protocol/architecture, an authorization service brokers access and grants users an access token to present, in place of credentials.
65+
66+
### <span style="color:blue;">Example</span>
67+
68+
There are four primary entities involved with the OAuth protocol: requesting, service one, service 2, intermediary server that handles issuing tokens that get presented in lieu of credentials. At a very high level, the flow looks something like
69+
70+
-User Authenticated into Application/Service X
71+
-Application/Service X prompts user if they want to login using social media account credentials
72+
-User Logs into social media account/other service
73+
-Authorization Server/Service Generates Access Token
74+
-Service X sends Token for limited access to Social Media Account (instead of sharing credentials)
75+
76+
77+
```
78+
[OAuth2.Client module ](https://hexdocs.pm/oauth2/OAuth2.Client.html)
79+
80+
```
81+
82+
### JWT
83+
JSON Web Tokens (abbreviated JWT, pronounced "jot") are multi-use tokens for authentication and session management. JWTs have three components, header contains information identifying type of token and algorithm used for the signature, payload/body that contains data about the disposition of the token, signature - which serves as an integrity check to establish if the token has been modified or tampered with.
84+
85+
Tokens, like other authentication credentials, etc. must be protected in transit and at rest and can be Base64 encoded and cryptographically signed
86+
87+
Why use JWT? For post authentication authorization, JWTs can be signed and encrypted which helps establish trust. These tokens place little stress on the authentication and authorization mechanisms and help with implementing access controls throughout the application.
88+
89+
### <span style="color:blue;">Example</span> / <span style="color:red;">Quiz</span>
90+
91+
Create/Generate Token
92+
```
93+
94+
95+
```
96+
97+
Validate Token
98+
```
99+
def connect(%{"authorize" => token}, socket, _connect_info) do //
100+
case JwtChecker.validate_token(token)do
101+
...
102+
...
103+
```
104+
### References
105+
https://dev.to/onpointvn/implement-jwt-authentication-with-phoenix-token-n58
106+
(https://hexdocs.pm/guardian/Guardian.Token.Jwt.html)
107+
https://elixirschool.com/blog/jwt-auth-with-joken/
108+
109+
110+
## Sessions
111+
112+
### Description
113+
114+
Authentication is the first step a user must complete to access a secure application/data. Once an entity is authenticated, subsequent activity/interactions need to be tracked as belonging to the same entity.
115+
116+
Some applications do this by establishing and managing a session. Other applications are "session-less" and required a different approach for keeping the application's "knowledge" of what a user is doing while they use an application.
117+
118+
For session-less applications, once a user authenticates, the server assigns and sends a token to their client. For any following requests, the client sends their token in each request, like with JWTs discussed previously. The server only checks the validity of the token.
119+
120+
In session oriented applications, one the user authenticates, information in subsequent requests are compared to session information kept on the server.
121+
122+
In a way, this is like a museum visit. A session is like showing your membership card or ticket for the day. Generally you can come and go on your day pass (they'll probably stamp you hand if you leave but you can get back in no problem). Once the museum closes, the session is over and you have to leave and come back another day. If you have a membership or ticket for multiple visits, you have to show your card/ticket at the door again.
123+
124+
Session-less, sometimes referred to as "fire and forget it" is like purchasing a single day pass with cash. Your name probably isn't in the system and if you come back a month later to buy another day pass, there's no record of you having been there previously. (For simplicity we'll ignore any tracking.) If you have a membership card with your name, however, and if you're paying cash, probably can't track you individually.
125+
126+
## Authentication and Security Concerns
127+
128+
An application's authentication mechanism is a critical component. If not securely designed, it can provide an attack vector for malicious actors to gain access to legitimate user accounts, privileged application features, and sensitive data.
129+
130+
-Authentication, credentials, should never be stored in cleartext, nor hardcoded in source code
131+
Credential Stuffing Attacks
132+
Security concerns/examples of multi-factor authentication getting hacked
133+
Multi-factor authentication (MFA) fatigue attack - aka MFA Bombing - aka MFA Spamming
134+
Bypassing MFA
135+
JWT token vulnerabilities
136+
137+
Authentication Issues, Weaknesses, Failures make an appearance on multiple lists
138+
OWASP Top 10 for Web Applications A07:2021-Identification and Authentication Failurs (used to be called Broken Authenticication
139+
140+
## Prevention and Countermeasures
141+
Use built and tested authentication mechanisms in your code language framework.
142+
143+
Authentication is a key component of an application but given its integration with some of the other concepts mentioned in this module, it's implementation in your products can become complex. This module touched on some of the highlights but please refer to the references below for extensive explanations of authentication and related.
144+
145+
### <span style="color:red;">Quiz</span>
146+
147+
**Which of the following OWASP Top 10 Web Application Security Risks are related to the abuse of credentials or flaws in mult-factor authentication implementation?**
148+
149+
*Uncomment the line with your answer*
150+
```
151+
152+
# A02:2021-Cryptographic Failures
153+
# A05:2021-Security Misconfiguration
154+
# A07:2021-Identification and Authentication Failures
155+
# A08:2017-Insecure Deserialization
156+
157+
IO.puts(answer)
158+
159+
```
160+
161+
**Which two are examples of a credential that can be used in an application's authentication process?**
162+
163+
*Uncomment the line with your answer*
164+
165+
```
166+
# token
167+
# api call
168+
# session
169+
# username and password
170+
171+
IO.puts(answer)
172+
173+
```
174+
175+
**Which statement best characterizes how an entity trying to be properly authenticated goes about it?**
176+
177+
```
178+
*Uncomment the line with your answer*
179+
180+
# Hello, I just came from X street and would like to enter your establishment. I am who I say I am and I can show you proof. May I enter?
181+
# Let me in, now! Let me in , now! Let me in, now!!!!!
182+
# Trust me, I'm harmless
183+
# Yes, I know that id doesn't look like me but my friend said I can use it so it's ok
184+
185+
```
186+
187+
### References
188+
https://cheatsheetseries.owasp.org/cheatsheets/Authentication_Cheat_Sheet.html
189+
https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html
190+
https://owasp.org/www-project-web-security-testing-guide/stable/4-Web_Application_Security_Testing/06-Session_Management_Testing/README
191+
https://owasp.org/www-community/attacks/Credential_stuffing
192+
https://owasp.org/www-project-web-security-testing-guide/latest/4-Web_Application_Security_Testing/06-Session_Management_Testing/10-Testing_JSON_Web_Tokens
193+
194+
[**<- Previous Module: Secure SDLC Concepts**](./3-ssdlc.livemd) || [**Next Module: Elixir Security ->**](./5-elixir.livemd)

0 commit comments

Comments
 (0)