[Kong]ทำ Authentication with OAuth 2.0

Supanut Laddayam
4 min readJun 12, 2021

มาลองทำ Oauth 2.0 กับตัว kong กัน

มาดู Oauth 2.0 flow

step1 : ฺbob ต้องการเข้าเว็บ app YellowDuckStore

step2 : YellowDuckStore จะ redirect ไปที่หน้า authorization

step3 : bob กด authorize ที่ web resource server

step4 : web resource server รับ data และ POST ไปขอ authorize code

step5 : web resource server จะ redirect กลับไปพร้อม authorize code

step6 : YellowDuckStore POST authorization โดยใช้ access token

ลุย lab กัน.. here we go!

  1. มาเริ่มกันที่ clone ตัว docker-compose กันก่อน
$ git clone https://github.com/narate/kong-docker.git -b pg

*cr: K’Narate -/\-

**กรณีนี้ผม implement กับ db postgres (ถ้าเพื่อนๆต้องการ implement กับ db อื่นก็สามารถ clone branch ชื่อdb ที่ต้องการได้ครับ)

2. แก้ไข file docker-compose

  • ที่ kong environment
- KONG_PROXY_LISTEN= 0.0.0.0:8000, 0.0.0.0:8443 ssl- KONG_ADMIN_LISTEN= 0.0.0.0:8001, 0.0.0.0:8444 ssl
  • ที่ kong ports (map port ที่ต้องการ)
- 443:8443- 444:8444

เสร็จเรียบร้อย…

3. รัน docker compose

$ docker-compose up -d

4. สร้าง app ที่จะทำหน้าที่เป็น Resource server กัน

4.1 สร้าง Cert เพื่อใช้สำหรับ https (openssl)

$ openssl req -x509 -newkey rsa:2048 -keyout tempkey.pem -out cert.pem -days 365$ openssl rsa -in tempkey.pem -out key.pem

ได้ 3 file: cert.pem, key.pem, tempkey.pem

4.2 สร้าง node resource server

clone: https://github.com/Supanut1911/Oauth2-node-resource-server

สร้าง certificates folder ใส่ cert ที่ generate ได้

*data.json เป็น mock db

4.3 test service

GET: https://localhost:7788/verify

กรณีนี้เราต้องส่ง headers เพื่อเป็นการบอกว่าเป็น resource ของใคร

ต่อไปเราจะใช้ access token ในการ get ค่าของ bob ออกมา

5. API gate way via kong

  • 5.1 add Service
POST: https://localhost:9444/servicesBody:
{
"name": "resource-api-server",
"url": "https://localhost:7788"
}
  • 5.2 add Route for resource-api-server
POST: https://localhost:9444/routesBody:
{
"name": "resource-server-route",
"service": {
"name": "resource-api-server"
},
"paths": ["/api/authen"]
}
  • 5.3 เข้าผ่าน kong gate way
GET: https://localhost:9443/api/authen/verify
  • 5.4 เพิ่ม plugin Oauth2.0
POST:https://localhost:9444/services/resource-api-server/pluginsBody:
{
"name":"oauth2",
"config": {
"scopes": ["user_profile", "biometric", "verify_acc"],
"mandatory_scope": true,
"enable_authorization_code": true
},
"protocols": ["https"]
}

กลับมาเข้า app ได้ status 401 Unauthorized แปลว่าเราแอด Oauth 2.0 เข้ากับ service แล้ว

  • 5.5 add consumers
POST: https://localhost:9444/consumersBody:
{
"username": "???"
}
  • 5.6 add credential Oauth 2.0 ให้ consumer
POST:https://localhost:9444/consumers/verifier/oauth2Body:
{
"name": "???",
"redirect_uris": ["???"]
}
  • 5.6 Request authorization code
POST: https://localhost:9443/api/authen/oauth2/authorizeBody:
{
"client_id": "???",
"response_type": "code",
"scope":"verify_acc",
"provision_key": "???",
"authenticated_userid": "bob"
}

client_id: ค่า response add Oauth 2 credential

provision_key: ค่า response add Oauth 2 plugin

authenticated_userid: ชื่อของ user

*response เราจะได้ค่า code มาเพื่อนำค่านี้ไปแลกเป็น acess token

5.7 แลก authorization code กับ access token

POST: https://localhost:9443/api/authen/oauth2/tokenBody:
{
"grant_type": "authorization_code",
"code": "???",
"client_id":"???",
"client_secret": "???"
}
  • code : ค่าจาก Request authorization code
  • client_id : ค่า response add Oauth 2 credential
  • client_secret : ค่า response add Oauth 2 credential
  • response จะได้ access token และ refresh token

5.8 นำ access token มาใช้ authen

เลือก Auth ชนิด bearer , ใส่ access token

เป็นที่เรียบร้อย… ได้ข้อมูล bob มาแล้ว

5.9 ขอ access token ใหม่ จาก refresh token (optional)

POST: https://localhost:9443/api/authen/oauth2/tokenBody:
{
"grant_type":"refresh_token",
"refresh_token": "???",
"client_id":"???",
"client_secret":"???"
}
  • จะได้ access token และ refresh token ชุดใหม่

สรุป

โอเครครับจบกันไปแล้วกับการทำ Oauth 2 บนตัว kong , ทำให้เราสามารถ authen บน kong ไม่ต้องมาเขียน API authentication

ขอบคุณครับ

--

--