Mark Ku's Blog

Introduction

After doing a simple OAuth exercise, I also learned the NestJS (Node) backend framework and Prisma (ORM) along the way.

Purpose

Open Authorization (OAuth) is an open standard for authorization, widely used for social logins. It allows third-party websites to access resources on a resource server without obtaining the user's credentials.

Flow Chart

Flow chart
Flow chart

Flow Description

  1. When the user clicks the login button on the main website, the redirect_uri is passed to the OAuth login page via the URL.
  2. After the user enters their username and password to log in, the system generates a code, stores it in the database, and then redirects the page back to the e-commerce site's callback page, passing the code via the URL.
  3. On the callback page, the frontend can extract the code from the URL and send it to the backend. The backend then uses this code to exchange it for an access token via the OAuth API.
  4. The page receives the access token. This token can then be used to access the resource server and retrieve relevant information. When the token expires, it can be refreshed.

Technology Stack Used

  • Frontend (E-commerce site) - (Next.JS, Eslint, Prettier, React, Redux toolkit) - Code
  • Backend (OAuth service) - (NestJS, MySQL, Prisma, Swagger, class-validator for data validation, and Guards for JWT API authentication) - Code
  • Database: MySQL

Demo

Deployment Environment

  • Deployed to a NAS using PowerShell
  • GCP reverse proxy + personal NAS server

Testing

  • Jest
  • Rest Client

Thoughts

This was my first time building a website with NestJS, and I found the development experience to be very pleasant. The modular design of NestJS reminded me of Angular's dependency injection, and it also felt a bit like the layered architecture of .NET MVC. Paired with Prisma, database operations became very intuitive. For lightweight websites, NestJS not only enables rapid development but also makes it easy to achieve modularity and separation of concerns, leading to a more efficient development process.

Additional Notes:

1. A summary of the different OAuth grant types

  • Authorization Code Grant - In this flow, an authorization code is used to exchange for an access token. The application never directly handles the user's credentials.
  • Client Credentials Grant - In this grant type, if Service A wants to access Service B's API, Service A uses its own client credentials (issued by Service B) to obtain an access token.
  • Implicit Grant - Typically used with JavaScript, but it is being phased out due to security concerns.
  • Resource Owner Password Credentials Grant - This grant type directly requests the user's credentials from the client. It is less secure and not commonly used anymore.

2. The difference between OAuth and SSO

  • SSO: The purpose of Single Sign-On (SSO) is to simplify the login process across multiple applications. It allows a user to log in once and then seamlessly use the same identity across different applications. SSO primarily focuses on authentication, which is confirming who the user is.
  • OAuth: OAuth, on the other hand, is an authorization protocol. It allows a third-party application to access protected resources on behalf of a user. The main purpose of OAuth is authorization, enabling an application to securely access certain user resources without needing direct access to the user's credentials (like their password).

3. A newer topic: OpenID Connect (OIDC)

  • OpenID Connect: OIDC is an identity layer built on top of OAuth 2.0. It is specifically designed for authentication and supports SSO. OIDC is a hybrid of SSO and OAuth, addressing both authentication and authorization needs.

4. What is authentication vs. authorization?

  • Authentication - Who are you? Verifying if the identity of the source is valid.
  • Authorization - What can the target user do? Or what can they access?

References

Author

Mark Ku

擁有 10+ 年經驗的資深軟體工程師,現為 AI 應用 Builder,專注於大型平台架構與簡化複雜系統設計,從電商系統到訂閱與收費平台,結合 AI Agent、AI 整合與自動化開發,打造高效率且可持續演進的產品技術基礎。Read More

Found this useful?

The author's free tools, daily podcasts and newsletter are all here.

Mark Ku · This article is licensed under CC BY 4.0. Credit the author and link back to the original when reusing it.

Comments

Subscribe to Newsletter

Subscribe to get new posts delivered instantly — never miss a tech share.

By submitting, you agree to receive emails. You can anytime.

Popular Posts

View all
Mark Ku
··602

Oracle Cloud Always Free Tier: Linux Host and Static IP for a $0 Cloud Solution

Oracle Cloud Always Free Tier: Linux Host and Static IP for a $0 Cloud Solution
Mark Ku
··490

Say Goodbye to Postman's Fee Trap! A Hands-on Guide to Bruno, the Open-Source Git-Native API Testing Powerhouse.

Say Goodbye to Postman's Fee Trap! A Hands-on Guide to Bruno, the Open-Source Git-Native API Testing Powerhouse.
Mark Ku
··333

A Free, Open-Source, Notion-like Knowledge Base — A Complete Guide to Deploying and Backing Up Outline Wiki

A Free, Open-Source, Notion-like Knowledge Base — A Complete Guide to Deploying and Backing Up Outline Wiki
Mark Ku
··264

Training Your Own AI Voice: Hardware Requirements, Open-Source Model Comparison, and LoRA Fine-Tuning

Training Your Own AI Voice: Hardware Requirements, Open-Source Model Comparison, and LoRA Fine-Tuning
Mark Ku
··221

Building an Efficient API Management Platform: Deploying Kong Gateway from Scratch - Part 1

Building an Efficient API Management Platform: Deploying Kong Gateway from Scratch - Part 1
Mark Ku
··215

Setting Up Samba on Ubuntu to Share Folders with Windows 11

Setting Up Samba on Ubuntu to Share Folders with Windows 11