01 พฤษภาคม 2562

Architecture micro services APIs back-end for web application

ผมได้มีโอกาส research หาข้อมูลการออกแบบ Architecture ของ micro services APIs back-end for web application โดยเน้นที่ Securityเป็นหลัก เลยอยากเอามาแชร์ในบลอกนี้

(Architecture อาจจะค่อนข้างซับซ้อน และยุ่งยากนิดหน่อย ในการพัฒนาครับ)


จากรูปด้านบน เราจะแบ่ง user token ของเป็น 2 zone คือ byReference token และ byValue token

โดย concept หลักๆ คือ byReference token จะเป็นแค่ค่า unique ที่ไม่มีเนื้อหาของ accessToken ส่วน byValue token จะเก็บ accessToken

ตัว APIs gateway จะทำหน้าที่ mapping byReference token และ byValue token

user services จะเป็นตัว authenticate user และสร้าง access token ดังนั้น user services จะเก็บ private key

services อื่นๆ จะทำหน้าที่ verify access token ดังนั้นต้องเก็บ public key







จากรูปด้านบน จะเป็นตัวอย่าง flow ของการ login เข้าสู่ระบบ ซึ่งจะมีขั้นตอนดังนี้

1. กรอก username, password

2. nginx forward request to APIs gateway

3. APIs gateway forward request to user services

4. user services authenticate username, password กับ database

5. user services สร้าง access token

6. user services return access token / userId to APIs gateway

7. APIs gateway ตรวจสอบว่ามี token ใน redis ของ user แล้วหรือยัง (กรณี login มาจากหลายๆ session) ถ้ามี ให้ลบทิ้ง (เตะ session เก่าๆ)

8. APIs gateway สร้าง byReference token และ byValue token 

9. APIs gateway set cookie header of client โดยตามหลัก security จะต้องทำการ set properties ดังต่อไปนี้
- secure cookie: true => cookie จะทำงานบน https เท่านั้น
- httponly: true => ไม่ให้ javascript เข้าถึง cookie ได้
- samesite cookie => ป้องกันการ hack จาก domain อื่นๆ

10. APIs gateway forward response to nginx








จากรูปด้านบน จะเป็นตัวอย่าง flow ของการ consume resource APIs  ซึ่งจะมีขั้นตอนดังนี้

1. client call resource APIs (e.g., ds)

2. APIs gateway get userId from byReference token

3. APIs gateway get access token from byValue token

4. APIs gateway forward request to resource APIs

5. resouce APIs validate access token using public key





จากรูปด้านบน จะเป็นตัวอย่าง flow ของการ logout ซึ่งจะมีขั้นตอนดังนี้

1. client call logout API

2. APIs gateway delete token from redis


ไม่มีความคิดเห็น:

แสดงความคิดเห็น

บทความยอดนิยม (ล่าสุด)

บทความยอดนิยม (All Time)