Code Explainers

Code explainers tagged #authentication

python
import time
import uuid
 
from django.utils.deprecation import MiddlewareMixin

Attaching per-request context in Django

middleware request lifecycle multi-tenancy
Intermediate 7 steps
ruby
require "openssl"
require "base32"
 
class TOTP

How TOTP one-time codes work in Ruby

hmac authentication totp
Intermediate 7 steps
php
class TwoFactorController extends Controller
{
    public function show(Request $request): View|RedirectResponse
    {

Two-factor auth challenge flow in Laravel

authentication two-factor middleware
Intermediate 10 steps
python
from fastapi import Depends, FastAPI, Header, HTTPException, status
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
 

Chaining tenant dependencies in FastAPI

dependency injection multi-tenancy authentication
Intermediate 7 steps
ruby
require "openssl"
require "base64"
 
module SecureToken

Authenticated encryption with AES-GCM in Ruby

encryption aes-gcm authentication
Advanced 8 steps
go
package middleware
 
import (
	"crypto/subtle"

Timing-safe API key auth in Gin

middleware authentication constant-time-compare
Intermediate 8 steps
java
@Component
public class ApiKeyRotationFilter extends OncePerRequestFilter {
 
    private static final String API_KEY_HEADER = "X-Api-Key";

Building an API key rotation filter in Spring

servlet-filter authentication caching
Intermediate 10 steps
php
<?php
 
namespace App\Services;
 

Throttling login attempts in Laravel

rate-limiting authentication security
Intermediate 6 steps
rust
use hmac::{Hmac, Mac};
use sha2::Sha256;
use subtle::ConstantTimeEq;
 

HMAC signing and constant-time verify in Rust

hmac cryptography constant-time
Intermediate 7 steps
ruby
require "base64"
 
module Auth
  module BasicCredentials

Parsing HTTP Basic Auth headers in Ruby

parsing authentication base64
Intermediate 6 steps
typescript
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-headerapikey';
import { ConfigService } from '@nestjs/config';

Building an API-key auth strategy in NestJS

authentication passport dependency-injection
Intermediate 8 steps
python
from fastapi import APIRouter, Depends, HTTPException, status
from pydantic import BaseModel
 
from .dependencies import get_current_user, get_project_service

Building a CRUD router with FastAPI

dependency-injection rest-api authentication
Intermediate 6 steps
ruby
class ApplicationController < ActionController::Base
  INACTIVITY_TIMEOUT = 30.minutes
 
  before_action :expire_stale_session

Idle session timeout in a Rails controller

authentication session-management before-action
Intermediate 6 steps
python
from flask import g, session
 
from .extensions import cache
from .models import User

Injecting the current user in Flask templates

context-processor caching authentication
Intermediate 6 steps
go
package middleware
 
import (
	"crypto/hmac"

Verifying signed URLs with Gin middleware

hmac middleware authentication
Intermediate 8 steps
javascript
import { createContext, useContext, useReducer, useCallback, useEffect } from 'react';
 
const AuthContext = createContext(null);
 

Building an auth context in React

context usereducer authentication
Intermediate 8 steps
rust
use axum::{
    extract::{FromRequestParts, Path, Query},
    http::{request::Parts, StatusCode},
    response::{IntoResponse, Redirect},

Signed download links as an Axum extractor

hmac custom-extractor authentication
Advanced 9 steps
rust
use axum::{
    extract::{FromRef, FromRequestParts},
    http::{header, request::Parts, StatusCode},
    RequestPartsExt,

How a JWT extractor works in Axum

jwt authentication extractors
Intermediate 8 steps