bg_image
header

Bearer Token

A Bearer Token is a type of access token used for authentication and authorization in web applications and APIs. The term "Bearer" means "holder," which implies that anyone in possession of the token can access protected resources—without additional verification.

Characteristics of a Bearer Token:

  • Self-contained: It includes all necessary authentication information.
  • No additional identity check: Whoever holds the token can use it.
  • Sent in HTTP headers: Typically as Authorization: Bearer <token>.
  • Often time-limited: Tokens have expiration times to reduce misuse.
  • Commonly used with OAuth 2.0: For example, when authenticating with third-party services.

Example of an HTTP request with a Bearer Token:

GET /protected-data HTTP/1.1
Host: api.example.com
Authorization: Bearer abcdef123456

Risks:

  • No protection if stolen: If someone intercepts the token, they can impersonate the user.
  • Must be securely stored: Should not be exposed in client-side code or URLs.

💡 Tip: To enhance security, use short-lived tokens and transmit them only over HTTPS.

 

 


Open Authorization - OAuth

OAuth (Open Authorization) is an open standard protocol for authorization that allows applications to access a user's resources without knowing their credentials (e.g., password). It is commonly used for Single Sign-On (SSO) and API access.

How Does OAuth Work?

OAuth operates using tokens, which allow an application to access a user's data on their behalf. The typical flow is as follows:

  1. Authorization Request: An application (client) requests access to a user’s protected data (e.g., Facebook contacts).
  2. User Authentication: The user is redirected to the provider's login page (e.g., Google, Facebook) and enters their credentials.
  3. Permission Granting: The user confirms that the application can access specific data.
  4. Token Issuance: The application receives an access token, which grants permission to access the approved data.
  5. Resource Access: The application uses the token to make requests to the API server without needing the user's password.

OAuth 1.0 vs. OAuth 2.0

  • OAuth 1.0: More complex, uses cryptographic signatures but is secure.
  • OAuth 2.0: Simpler, relies on HTTPS for security, and is the most commonly used version today.

Real-World Uses of OAuth

  • "Sign in with Google/Facebook/Apple" buttons
  • Third-party apps accessing Google Drive, Dropbox, or Twitter APIs
  • Payment services like PayPal integrating with other apps