All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

Basic methods of authentication in the API

Post cover: Basic methods of authentication in the API
Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
When we build an API in Ruby on Rails, it's important to control, who has access to resources. Here are the main approaches to authentication:

Basic Authentication

The simplest, but unsafe method. In Basic Authentication, the Authorization header transmits username and password, encoded in Base64.
Header format:
Authorization: Basic <base64_string>
<base64_string> = Base64-encoded string username:password
For example, if you have:
  • username = apiuser
  • password = secret123
First, form a line:
apiuser: secret123
Next, we encode it in Base64:
demand 'base64'

credentials = "apiuser:secret123"
encoded = Base64.strict_encode64(credentials)
puts encoded
# => YXBpdXNlcjpzZWNyZXQxMjM=
So the title is going to look like this:
Authorization: Basic YXBpdXNlcjpzZWNyZXQxMjM=
When Rails sees Authorization: Basic ..., method authenticate_or_request_with_http_basic decodes Base64, separates username:password and checks them on the server.

Token Authentication

The user receives unique token, which adds to each request.
A more secure option for mobile or front-end applications.
class Api::V1::BaseController < ApplicationController
  before_action:authenticate_user!

  private

  def authenticate_user!
    token = request.heads['Authorization']&.split(' ')&.last
    @current_user = User.find_by(api_token: token)
    render json: { error: 'Unauthorized' }, status: :unauthorized unless @current_user
  end
end
Request header:
Authorization: Token abc123

JWT (JSON Web Token)

A popular way for stateless API. The server does not save the session, and the client sends a signed token.
Example with jwt heme:
# Create token
payload = { user_id: user.id, exp: 24.hours.from_now.to_i }
token = JWT.encode(payload, Rails.application.secret_key_base)

# Token check
decoded = JWT.decode(token, Rails.application.secret_key_base).first
user_id = decoded["user_id"]
Heading:
Authorization: Bearer <jwt_token>

OAuth 2.0

Standard for authorization and access to resources through third-party services or APIs.
Rails uses servers to create its own OAuth 2 Doorkeeper:
# Gemfile
gem 'doorkeeper'
After configuration, you can issue access tokens to external clients:
Authorization: Bearer <access_token>
If you need to allow users to log in through third-party services (Google, Facebook, GitHub), use OmniAuth in connection with Devise for authentication.
In simple words: Doorkeeper — for API access, OmniAuth — for user login through other services.

This post doesn't have any additions from the author yet.

What is exponential growth?
16 Sep 18:57

What is exponential growth?

meme code
meme code@memecode
What is factorial complexity?
16 Sep 19:03

What is factorial complexity?

meme code
meme code@memecode
What is NP-complexity?
16 Sep 19:31

What is NP-complexity?

meme code
meme code@memecode
Offset vs Cursor Pagination in Rails: What to Choose and Why
24 Sep 15:22

Offset vs Cursor Pagination in Rails: What to Choose and Why

meme code
meme code@memecode
What is Row Security in PostgreSQL and why is it Rails developers
04 Oct 19:06

What is Row Security in PostgreSQL and why is it Rails developers

meme code
meme code@memecode
What is ivar in Ruby /Rails?
19 Oct 20:12

What is ivar in Ruby /Rails?

meme code
meme code@memecode
How do OAuth 1 differ from OAuth 2
19 Oct 20:34

How do OAuth 1 differ from OAuth 2

meme code
meme code@memecode
What is ORM and why is it needed?
26 Oct 14:00

What is ORM and why is it needed?

meme code
meme code@memecode
MCP: A new internet where sites communicate with AI
04 Nov 11:43

MCP: A new internet where sites communicate with AI

meme code
meme code@memecode
Why is TOON better than JSON when working with AI?
14 Nov 15:14

Why is TOON better than JSON when working with AI?

meme code
meme code@memecode