Search This Blog

Saturday, April 30, 2022

Rest API Design - OTP based Auth API

Learn how to design REST API with a real world example. We build an OTP based auth API with /signup, /login, /send-otp and /validate-otp features.

Problem statement:

Design the auth flow apis (signup, login, etc) for an OTP based user authentication. Assume OTP will be created and presented to users upon a successful login.


API Signature:

- URL

- Request Method

- Request Headers

- Request Body

- Response Status code

- Response Headers

- Response Body


Signup API:

  • URL
  • Request method: GET, PUT, POST, DELETE, PATCH, OPTIONS
    • Which method should we use here?
    • POST
    • POST vs PUT? Idempotency
  • Request Headers:
    • Content Type: application/json
    • any others?
  • Request Body: Form Data, XML, JSON, i.e. Content Type
    • JSON body:
  •   {
  •     "email": "foo@bar.com",
  •     "password": "supersecret", // plaintext password, yikes!
  •     "phone": "+91-9876543210"
  •   }

  • Logic on server side?
    • Validate request body
    • Insert record in DB
    • Handle errors
  • Response Status code: 2xx, 3xx, 4xx, 5xx
    • 200 ok or
    • 201 created
  • Response Headers:
    • Content Type if we are sending any content back
    • Any other headers?
  • Response Body:
    • Empty body or
    • Some useful content (user id?)


Login API:

  • URL https://<name>.com/api/v1/login
  • Request method: POST
  • Request Headers:
    • Content Type: application/json
    • any others?
  • Request Body:
    • JSON body:
  •   {
  •     "email": "foo@bar.com",
  •     "password": "supersecret", // plaintext password, yikes!
  •   }

  • Logic on server side?
    • Validate request body
    • Check email and password combination
    • Handle errors
    • Create a token and send token back to user. Why?
  • Response Status code: 2xx, 3xx, 4xx, 5xx
    • 200 ok
    • 4xx client side errors (validation of email, invalid credentials or unauthorized)
    • 5xx server side errors (DB down so not able to login the user)
  • Response Headers:
    • Content Type if we are sending any content back
    • Any other headers?
      • Return a time limited token in headers
      • Why do we need this? Let's come back to it when we talk about validate OTP API
  • Response Body:
    • Empty body or
    • Send OTP as part of response?
    • When do we actually send the OTP to user? Is it a UI triggered action? Or a "side-effect" of successful login


Send OTP API:

  • URL https://<name>.com/api/v1/send-otp
  • Request method: POST
  • Request Headers:
    • Token received from Login API reponse. Why?
    • any others?
  • Request Body:
    • Empty body or
    • Should we send phone number to send OTP to?
  • Logic on server side?
    • Validate token from headers
    • Figure out phone number from token
    • Sent OTP, store OTP in some storage for validation
  • Response Status code: 2xx, 3xx, 4xx, 5xx
    • 200 ok
    • 4xx client side errors (invalid token, i.e. unauthorized)
    • 5xx server side errors (Third party SMS vendor down, DB down, etc. so not able to send OTP the user)
  • Response Headers:
    • None that I can think of
  • Response Body:
    • Empty body


Validate OTP API:

  • URL https://<name>.com/api/v1/validate-otp
  • Request method: POST, or GET. Any query params?
  • Request Headers:
    • Token received from Login API reponse. Why?
    • any others?
  • Request Body:
    • JSON body
  • {
  •     "otp": "123456"
  • }

  • Logic on server side?
    • Validate token
    • Check token and OTP combination are correct
    • Handle errors
  • Response Status code: 2xx, 3xx, 4xx, 5xx
    • 200 ok
    • 4xx client side errors (invalid token, i.e. unauthorized, invalid otp, expired otp, etc.)
    • 5xx server side errors (DB down so not able to validate OTP)
  • Response Headers:
    • None that I can think of
  • Response Body:
    • Empty body


Do you see any challenges/unknowns?

  • Token before and after OTP validation is same. Is it okay? What problems can it cause?
  • What if user requests for OTP multiple times? (both legit and abuser use case)
  • Where do we store OTPs, Tokens?
  • Can this API be used by both web and mobile?

Friday, April 29, 2022

Kubernetes daily use commands

 Here is a quick view on k8 commands which are ideally useful for daily interaction work

  • kubectl config get-contexts
    • display list of contexts
  • kubectx <env-name>
    • to switch the env
  • kubectl get nodes
    • Get all nodes
  • kubectl get namespaces
    • Get all namespaces for a environment
  • kubectl -n services get pod <pod-name>
    • Get specific pod details
  • kubectl -n services get pods
    • Get all pods
  • kubectl -n services delete pod <pod-name>
    • Delete specific pod name
  • -o wide
    • To get data in more details like which node etc
  • -o yaml
    • To open specific file to see the details
  • kubectl -n services describe pod <pod-name>
    • To see description of the pod
  • kubectl -n services exec -it  <pod-name> /bin/bash
    • Logging into the pod to see details at code level
  • kubectl -n services get hpa <hpd-pod-name>
    • To check hpa details if hpa enabled
  • kubectl -n services scale deploy <pod-name> --replicas=3
    • To manual scale
  • kubectl -n services get pods -o wide | grep ½
    • Get all pods which are in unhealthy state
  • kubectl -n services get deployment <pod-name> -o yaml
    • To check min/max replicas or any other data point w.r.t deployment
  • kubectl -n services get deployment <pod-name> -o yaml
    • To edit min/max replicas or any resources
  • kubectl -n services rollout restart deployment <po-name>
    • To restart a service
  • kubectl -n services logs <pod-name> -c install -f
    • This could be used if our pod is in Init stage. Gives us the keys that are missing from config(Search for nil after running this command)
  • kubectl -n configuration get pods
    • Consul and Vault status
  • stern -n configuration <name>
    • Check logs of vault
  • stern -n services <pod-name> -c <app-name> -t --since 1m
    • Prints logs of all the pods of last 1 min
  • stern -n services geolayers-api-primary -c geolayers-api -t --since 1m | grep '<text>' 
    • Prints log only for the text that is in grep
  • kubectl describe canary <pod-name> -n services
    • To check canary deployment of specific pod
  • kubectl get canary -n services
    • To check all pods for which canary enabled

Kubectl commands complete cheatsheet : https://kubernetes.io/docs/reference/kubectl/cheatsheet/

Happy Learning !! :)

My Profile

My photo
can be reached at 09916017317