Securing APIs with Authentication

Tutorial 3 of 5

Tutorial: Securing APIs with Authentication in Rails

1. Introduction

1.1 Goal of the Tutorial

This tutorial aims to introduce you to the concept of securing your APIs. We will focus on understanding the different methods of authentication and how to implement them in your Rails API.

1.2 Learning Objectives

By the end of this tutorial, you should be able to:
- Understand the importance of API authentication
- Know the different methods of authentication
- Implement authentication in a Rails API

1.3 Prerequisites

Before you begin, you should have a basic understanding of:
- Ruby on Rails
- REST API principles
- Basic knowledge of HTTP and HTTPS

2. Step-by-Step Guide

2.1 Understanding API Authentication

API authentication is a process that validates the identity of the client trying to access the API. It ensures that only authenticated users can access the resources.

2.2 Authentication Methods

There are several methods to authenticate a user, here we will discuss three common methods:
- Basic Authentication
- Token-based Authentication
- OAuth

Basic Authentication: This is the simplest method where the client sends a username and password with every request to the server.

Token-based Authentication: In this method, the client sends a token, instead of a username and password. The token is generated by the server during the login process.

OAuth: OAuth (Open Authorization) is an open standard for token-based authentication and authorization which provides secure delegated access.

2.3 Implementing Authentication in Rails API

In this tutorial, we'll use token-based authentication. Here's how it works:
- When the user logs in, the server generates a unique token, associates it with the user, and sends it back.
- For all subsequent requests, the client must include this token in the Authorization header.
- The server verifies the token and processes the request.

3. Code Examples

3.1 User Model

First, we need to add an authentication token to our User model.

class User < ApplicationRecord
  before_create :generate_authentication_token

  def generate_authentication_token
    loop do
      self.authentication_token = SecureRandom.base64(64) 
      break unless User.find_by(authentication_token: self.authentication_token)
    end
  end
end

In this snippet, we generate a unique token for each user before creating their record.

3.2 Authenticating Requests

Next, we'll add a before_action to our ApplicationController to authenticate all incoming requests.

class ApplicationController < ActionController::API
  before_action :authenticate_request

  def authenticate_request
    @current_user = User.find_by(authentication_token: request.headers['Authorization'])
    render json: { error: 'Not Authorized' }, status: 401 unless @current_user
  end
end

Here, we're checking if the provided token matches any user's token in the database.

4. Summary

In this tutorial, we've learned about API authentication, some common methods of authentication and how to implement token-based authentication in a Rails API.

5. Practice Exercises

Now, try to implement the following on your own:
1. Implement basic authentication.
2. Implement a system where the token expires after a certain period.
3. Implement OAuth using a gem like doorkeeper.

Remember, practice is key to mastering any concept. Happy coding!