Auth & security at scale
OAuth2, JWT, IAM patterns, secrets rotation, and least-privilege service auth.
How to Mock a Permissions Policy in Python
A lightweight Python class that simulates a browser Permissions-Policy header by tracking allowed/ denied feature permissions with get, set, reset, and bulk operations.
class PermissionsPolicy:
def __init__(self):
self._features = {
"geolocation": "self",
"camera": "self",
"microphone": "self",
"payment": "self",
"usb": "self",
}
def get_feature_policy(self, feature):
return self._features.ge…
How to Mock a TLS Certificate Rotation Schedule in Python
Simulate a TLS certificate rotation schedule with a Python class that tracks last and next rotation dates and decides when to rotate.
import datetime
import random
import time
class CertRotator:
def __init__(self, cert_name, rotation_days=30):
self.cert_name = cert_name
self.rotation_days = rotation_days
self.last_rotated = datetime.date.today() - datetime.timedelta(days=random.randint(10, 25))
self.next_rotatio…
How to Revoke Tokens with a Blacklist Set in Python
A minimal TokenBlacklist class using a Python set to revoke, batch-revoke, check, and remove expired tokens for simple token invalidation.
import time
class TokenBlacklist:
def __init__(self):
self.blacklisted_tokens = set()
def revoke(self, token):
self.blacklisted_tokens.add(token)
print(f"Token {token} revoked. Blacklist size: {len(self.blacklisted_tokens)}")
def revoke_batch(self, tokens):
before = len(s…
How to Set a SameSite Cookie in Python
Set a SameSite cookie attribute in Python using the standard library's SimpleCookie class.
from http.cookies import SimpleCookie
def set_same_site_cookie(name, value, same_site="Lax"):
cookie = SimpleCookie()
cookie[name] = value
cookie[name]["path"] = "/"
cookie[name]["samesite"] = same_site
return cookie[name].OutputString()
if __name__ == "__main__":
print(set_same_site_cookie("…
Browse by section
Each section groups closely related Python snippets.
Auth & security at scale — Python code examples
What you will find here
This page collects auth & security at scale snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.
Samples vs tutorials and challenges
Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.