REST API Security Essentials: OAuth2, JWT, and Token Handling Quiz

Explore foundational concepts of REST API security focusing on OAuth2 authorization, JWT structure, and effective token handling. This quiz assesses your understanding of securing APIs, token validation, and best practices for safe implementation.

  1. OAuth2 Grant Types

    Which OAuth2 grant type is typically used when a user wants a third-party application to access their data without revealing their password, such as logging in with an external account?

    1. Resource Owner Password
    2. Authorization Code
    3. Device Flow
    4. Implicit Grant

    Explanation: The Authorization Code grant is most commonly used to allow applications to access user data without sharing the user's password, supporting secure exchanges. Resource Owner Password grant involves sharing credentials directly, which is less secure and discouraged for third-party apps. Implicit Grant is less secure as it exposes tokens directly to the client. Device Flow is intended for devices without browsers and is not suitable for regular web logins.

  2. JWT Structure

    What are the three main parts of a JSON Web Token (JWT) in order?

    1. Payload, Signature, Header
    2. Payload, Header, Signature
    3. Header, Payload, Signature
    4. Header, Signature, Payload

    Explanation: A standard JWT consists of three parts separated by dots: Header, Payload, and Signature. The header contains metadata, the payload holds claims, and the signature provides verification. Other orders presented are incorrect, as switching them would violate the JWT standard. Proper ordering is essential for correct parsing and validation.

  3. Token Expiration

    Why is it important for access tokens used in REST APIs to have an expiration time?

    1. To improve network latency
    2. To reduce token size
    3. To enable token transmission in clear text
    4. To limit potential misuse if stolen

    Explanation: Setting expiration times for tokens minimizes the risk period if they are compromised, reducing possible damage. Network latency is unrelated to token expiration. Secure transmission is achieved through proper channels, not by setting expiration times. Expiration does not affect the actual size of the token.

  4. Scope in OAuth2 Tokens

    What is the primary purpose of the 'scope' parameter in OAuth2 tokens?

    1. To define what actions or resources the client is allowed to access
    2. To specify the token's signature method
    3. To display token expiration time
    4. To identify the type of user agent

    Explanation: The 'scope' parameter limits token permissions to specific actions or resources, enhancing security by reducing unnecessary access. It does not specify signature methods or user agent types, which are done elsewhere. Token expiration is handled in a different parameter, not via scope.

  5. Token Revocation

    Which method allows an authorization server to immediately invalidate a token, such as when a user logs out or revokes access?

    1. Token Encryption
    2. Token Revocation Endpoint
    3. Token Sharing
    4. Token Signing

    Explanation: A Token Revocation Endpoint enables clients to ask the server to invalidate tokens, ensuring they can't be used further if compromised or the user logs out. Token encryption and signing relate to confidentiality and integrity, not direct invalidation. Token sharing is unrelated and could be insecure if done improperly.

  6. JWT Signature Purpose

    Why is the signature included in a JWT (JSON Web Token)?

    1. To store user session data
    2. To verify that the token has not been tampered with
    3. To speed up token validation
    4. To make the token URL-friendly

    Explanation: The signature ensures the contents of the JWT are authentic and unchanged since issuance, supporting data integrity and authenticity. JWTs can store session-like data in the payload, but this is separate from the signature's purpose. URL-friendliness is achieved through token encoding, not signing. Speed depends on implementation, not the presence of a signature.

  7. Bearer Tokens

    In the context of REST API requests, what is the correct way to include a Bearer token?

    1. Embedded directly in the URL path
    2. As a query parameter named 'token'
    3. In a custom HTTP header named 'X-Bearer-Token'
    4. As an Authorization header with 'Bearer' prefix

    Explanation: Tokens should be placed in the Authorization header using the 'Bearer' prefix to ensure secure and standard handling. Query parameters can expose tokens in logs and URLs, making them vulnerable. A custom header is non-standard and may not be supported. Embedding tokens in the URL path is insecure and discouraged.

  8. Refresh Tokens Security

    Why should refresh tokens be stored securely and never exposed to the frontend in a public-facing application?

    1. Because they need to be sent over UDP
    2. Because they allow obtaining new access tokens without user interaction
    3. Because they always contain sensitive user data
    4. Because they are larger than access tokens

    Explanation: Refresh tokens grant the ability to get new access tokens silently, so exposing them increases the risk of unauthorized access. Refresh tokens do not always store sensitive user data, as they usually function as opaque credentials. Their size is largely irrelevant for security. Tokens should be sent over secure protocols like HTTPS, not UDP.

  9. Audience Claim in JWTs

    In JWTs, what is the purpose of the 'aud' (audience) claim?

    1. To record the user's email address
    2. To list the token issuer's public keys
    3. To specify which API or service the token is intended for
    4. To define token expiration

    Explanation: The 'aud' claim ensures the token is only processed by its intended recipient, protecting against misuse. User information like email is typically in the payload, but not under 'aud'. Public keys are distributed differently and not through 'aud'. Expiration is handled by the 'exp' claim, not 'aud'.

  10. CSRF and Tokens

    Which token type helps prevent Cross-Site Request Forgery (CSRF) attacks in web applications?

    1. JWT Header
    2. Refresh Tokens
    3. CSRF Tokens
    4. OAuth2 Access Tokens

    Explanation: CSRF tokens are specifically designed to prevent unauthorized commands transmitted from a user that the web application trusts, blocking CSRF attacks. Refresh tokens and OAuth2 access tokens authenticate users but do not prevent CSRF on their own. The JWT header does not deal with CSRF protection.