Inno II - Token/API Flashcards

1
Q

Symmetric Encryption

A

Symmetric encryption uses a single secret key for both encryption and decryption of data. Key characteristics include:

Uses the same key to encrypt plaintext into ciphertext and decrypt ciphertext back into plaintext

Faster and more efficient than asymmetric encryption, making it ideal for encrypting large amounts of data

Requires secure key exchange between parties

Common algorithms: AES, DES, Triple DES

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Asymmetric Encryption

A

Asymmetric encryption, also known as public key cryptography, uses a pair of mathematically related keys: a public key and a private key. Key features include:

Public key is used for encryption, private key for decryption

Eliminates the need for secure key exchange

More secure but slower and more resource-intensive than symmetric encryption

Common algorithms: RSA, ECC, DSA

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Key Differences between symmetric and asymteric encryption

A

Key usage: Symmetric uses one key, asymmetric uses two related keys

Key length: Asymmetric keys are typically longer for equivalent security

Speed: Symmetric is faster and more efficient for large data sets

Security: Asymmetric is generally considered more secure for key exchange

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Usecases for symteric and asymetric encryption

A

Symmetric encryption is commonly used for:

Data storage encryption
Secure communication
Financial transactions
Device and network security

Asymmetric encryption is often used for:
Secure key exchange
Digital signatures
Email encryption
Secure communication protocols (HTTPS, SSH, TLS)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is JWT and how does it work?

A

JSON Web Token (JWT) is a compact, URL-safe token used to represent claims between two parties.

It works by encoding a JSON object into a string, which is then signed using a cryptographic algorithm to ensure its integrity and authenticity.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain the structure of a JWT.

A

A JSON Web Token (JWT) consists of three parts:
the header,
the payload,
and the signature.

The header contains metadata about the token and the algorithm used for signing, the payload holds the claims, and the signature ensures the token’s integrity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the purpose of the header in a JWT?

A

The purpose of the header in a JWT is to contain metadata about the token, including the type of token and the signing algorithm used. This information is crucial for verifying the token’s integrity and ensuring it hasn’t been tampered with.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Explain the difference between symmetric and asymmetric signing algorithms in JWT

A

Symmetric algorithms use the same key for both signing and verification, making them faster but less secure. Asymmetric algorithms, on the other hand, use a pair of keys: one for signing and another for verification, providing better security at the cost of speed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you handle token expiration in JWT?

A

To handle token expiration in JWT, you can set an expiration time in the token’s payload using the exp claim. On the server side, always check the token’s expiration time before processing any requests to ensure it hasn’t expired

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the role of the payload in a JWT?

A

The payload in a JWT contains the claims, which are statements about an entity (typically, the user) and additional data. These claims can include information such as user ID, roles, and expiration time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can you refresh a JWT token?

A

To refresh a JWT token, you need to implement a refresh token mechanism alongside the access token. The refresh token should be securely stored and used to request a new access token when the old one expires

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the security implications of using JWT?

A

The security implications of using JWT include the risk of token theft if not stored securely and the potential for token tampering if weak signing algorithms are used. To mitigate these risks, it’s essential to implement strong signing algorithms, secure storage practices, and token expiration mechanisms

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you store JWTs securely on the client side?

A

To store JWTs securely on the client side, you should use secure, HTTP-only cookies to prevent access by JavaScript. Additionally, implement token expiration and refresh mechanisms to enhance security.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Explain the concept of “claims” in a JWT

A

Claims in a JWT are statements about an entity, typically the user, and additional data. They can include information such as user ID, roles, and permissions, and are encoded in the payload of the JWT to convey information between parties.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are some common use cases for JWT?

A

JWTs are commonly used for secure user authentication and authorization, allowing users to access protected resources without repeatedly logging in. They are also employed in single sign-on (SSO) systems to streamline user access across multiple applications

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you implement role-based access control using JWT?

A

To implement role-based access control using JWT, you need to include user roles in the JWT payload. On the server side, verify the token and extract the roles, then check the user’s roles against the required permissions for accessing specific resources

17
Q

Explain the difference between access tokens and refresh tokens in the context of JWT.

A

Access tokens are short-lived tokens used to access protected resources, ensuring that users can only interact with the system for a limited time before needing re-authentication.

Refresh tokens, on the other hand, are long-lived tokens that allow users to obtain new access tokens without re-authenticating, providing a seamless user experience.

18
Q

How can you mitigate the risks of token theft when using JWT?

A

To mitigate the risks of token theft when using JWT, you should store tokens in secure, HTTP-only cookies to prevent access by JavaScript. Additionally, always use HTTPS to encrypt token transmission and implement token expiration and refresh mechanisms to limit the lifespan of stolen tokens.

19
Q

What is a JWT, and how does it differ from a session-based cookie?

A

JWT: A compact, self-contained token used for securely transmitting information between parties, typically containing a header, payload, and signature.

Cookie-based sessions: Use a session ID stored in a cookie and maintained on the server side to track users.

Difference: JWTs are stateless (no server storage required), whereas cookie sessions rely on server-side storage.

20
Q

How does the structure of a JWT support its functionality?

A

Header: Specifies the algorithm (e.g., HS256, RS256) and type (JWT).

Payload: Contains claims (e.g., iss, sub, exp) to convey data.

Signature: Ensures token integrity and authenticity.

21
Q

How would you use a JWT in a web application for authentication?

A

After login, generate a JWT with user-specific claims.

Send the JWT to the client in a secure HTTP-only cookie or as part of the response.

Validate the JWT on subsequent requests using the server’s secret or public key.

22
Q

How do you ensure a JWT stored in a cookie is secure?

A

Use HTTP-only cookies to prevent JavaScript access.

Set the Secure flag to allow transmission over HTTPS only.

Enable SameSite to restrict cross-origin usage.

Encrypt the cookie if additional security is needed.

23
Q

What are the potential risks of using JWTs, and how can you mitigate them?

A

Risk: Token leakage due to XSS or logging.

Mitigation: Use HTTP-only cookies and sanitize inputs.

Risk: Replay attacks.

Mitigation: Include unique jti claims and track tokens in a server-side blacklist.

Risk: Expired tokens.

Mitigation: Set reasonable exp claims and implement token refresh mechanisms.

24
Q

Compare the security implications of storing a JWT in a cookie versus local storage.

A

Cookie: Safer due to HTTP-only and Secure flags but susceptible to CSRF.

Local Storage: Vulnerable to XSS attacks since JavaScript can access it.

25
How can you prevent CSRF attacks when using JWTs in cookies?
Use a SameSite attribute (Strict or Lax) for cookies. Implement CSRF tokens for validating requests. Ensure state-changing endpoints are protected with anti-CSRF middleware.
26
How do you handle JWT expiration in long-lived sessions?
Use short-lived access tokens with a refresh token stored securely (e.g., in an HTTP-only cookie). When the access token expires, send the refresh token to obtain a new one. Invalidate refresh tokens upon logout or suspected compromise.
27
What are the trade-offs between stateless JWT authentication and stateful session cookies?
Stateless JWT: No server-side storage, easier to scale, but harder to revoke tokens. Stateful sessions: Require server-side storage, can revoke sessions easily, but scaling is more complex.
28
How can you revoke a compromised JWT?
Use a token blacklist maintained server-side to track revoked jti claims. Reduce token lifetime to minimize the impact window. Implement a logout endpoint to mark tokens as invalid.
29
What is a JWT Token?
A JSON Web Token (JWT) is a standard (RFC 7519) for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using these two algorithms HMAC (Hash-based Message Authentication Code) RSA or ECDSA (Asymmetric cryptographic algorithms)
30
How JWT token works?
User Logs In: The client (browser) sends login credentials to the server. Server Generates JWT: If credentials are valid, the server creates a JWT containing user data and signs it with a secret key. Token Sent to Client: The JWT is sent back to the client and stored (usually in localStorage or a cookie). Client Sends Token in Requests: For protected routes, the client includes the JWT in the Authorization header (Bearer Token). Server Verifies and Responds: The server verifies the token, extracts user info, and processes the request if valid.
31
JWT Structure
Header: Contains metadata about the token, such as the algorithm used for signing. Payload: Stores the claims, i.e., data being transmitted. Signature: Ensures the token’s integrity and authenticity.
32
Header
The header contains metadata about the token, including the signing algorithm and token type here metadata means data about data. alg: Algorithm used for signing (e.g., HS256, RS256). typ: Token type, always “JWT”.
33
Payload
The payload contains the information about the user **also called as a claim and some additional information including the timestamp** at which it was issued and the expiry time of the token. Common claim types: **iss** (Issuer): Identifies who issued the token. **sub** (Subject): Represents the user or entity the token is about. **aud** (Audience): Specifies the intended recipient. **exp** (Expiration): Defines when the token expires. **iat** (Issued At): Timestamp when the token was created. **nbf** (Not Before): Specifies when the token becomes valid.
34
Signature
The signature ensures token integrity and is generated using the header, payload, and a secret key. In this example we will use HS256 algorithm to implement the Signature part
35
Advantages of using JSON Web Token
Stateless Authentication: No need to store user sessions on the server; JWT contains all necessary data. Compact & Fast: Being small in size, JWT is efficiently transmitted in HTTP headers, making it ideal for APIs. Secure & Tamper-Proof: JWTs are signed using a secret key or public/private key pair, ensuring integrity. Cross-Platform Support: Can be used with any technology (JavaScript, Python, Java, etc.) for authentication. Built-in Expiry: Tokens can have an expiration time (expiresIn), reducing the risk of long-term access misuse.