Ms Auth

GitbookMicroservices2021-01-11


๐Ÿ“‘ Auth ๅพฎๆœๅŠก


่ฟ™ๆ˜ฏไธ€ไธช issue jwt-token็š„็ฎ€ๅ•็š„ๅพฎๆœๅŠก๏ผŒไฝฟ็”จMongoDB + mongoose

๐Ÿ“‘ Initialize

$ node bin/init.js

๐Ÿ“‘ ๅŠŸ่ƒฝ


  • ๆณจๅ†Š๏ผŒๆŸฅ่ฉข๏ผŒไฟฎๆ”น๏ผŒๅˆช้™ค็”จๆˆถไฟกๆฏ่กจ
  • ็™ปๅฝ•๏ผŒ็™ปๅ‡บ็ณป็ปŸ๏ผŒissue ้ขๅ‘ token ่ฎค่ฏใ€‚
action ่ฐƒ็”จ ่ฏดๆ˜Ž
ๆณจๅ†Œ /auth/signup /auth/register ไฟๅญ˜็”จๆˆทไฟกๆฏ๏ผŒๅŒ…ๆ‹ฌๅฃไปค
็™ปๅฝ• /auth/signin /auth/login ้ชŒ่ฏ็”จๆˆทไฟกๆฏ, ่ฐƒ็”จ authentication/authorization
้€€ๅ‡บ /auth/signout /auth/logout ๅ–ๆถˆ token
User /auth/account ๆŸฅ็œ‹ accounts
Role /auth/role ๆŸฅ็œ‹ roles

Notice: ๅคšไธช่กจไน‹้—ด็š„ๅ…ณ่”

๐Ÿ“‘ Auth Service - Microservices Authentication and Authorization


If you have a single client application then you can do following steps, ๅฝ“ๅ‰ๅฐฑๆ˜ฏ่ฟ™ไนˆๅš็š„๏ผš

  • Make one microservice for authentication that generates jwt token. ่ฆๆœ‰ไธ€ไธชไธ“้—จ็š„ ms ๆฅ issue tokenใ€‚
  • The jwt contains all essential user information in its payload, ie Role, UserId etc. ๅฝ“ๅ‰ๆ˜ฏๅŒ…ๆ‹ฌ็š„๏ผŒlogin ๆˆๅŠŸไน‹ๅŽๅฐฑไผš็”Ÿๆˆ๏ผŒๆฏๆฌก่กจๅ•ๆไบค็š„ๆ—ถๅ€™ไผ ้€’ใ€‚
  • The jwt token will be sent in Authorization header for every authorised request.
  • Before processing any request you can validate and decode the jwt token using middlewares. Now you can set the userโ€™s info in req object easliy and can easily access users role and its id in your controller.
  • if the token is not valid then you can throw error in middlewares and it will provide json response of unauthorised.
  • You can call the authentication api to validate and decode your token or you can write 3 to 4 line of code in every microservice in middleware.

๐Ÿ“‘

validation frontend backend DB notes
password โœ‹ (form)

๐Ÿ“‘ register

  • check existed ?
  • bcrypt.hash(Sync)
  • new User(req.body).save

๐Ÿ“‘ login

  • validate Password
  • email + phone unique?
  • bcrypt.compare(Sync)
  • jwt.sign

More:

  • Role: admin, member, owner?
  • Category: ?
  • compose token
  • User.authenticate() ?

๐Ÿ“‘ authentication

  • middleware: router.use(express-jwt)
  • jwt.verify

๐Ÿ“‘ Express


๐Ÿ“‘ 1. express

  • express.Router

๐Ÿ“‘ 2. express.Request

  • baseUrl:
  • path: // example.com/users?sort=desc -> โ€˜/usersโ€™
  • originalUrl: req.originalUrl = req.baseUrl + req.path
  • url: req.url is not a native Express property, it is inherited from Nodeโ€™s http module.
app.use("/admin", function (req, res, next) {
	// GET 'http://www.example.com/admin/new'
	console.dir(req.originalUrl); // '/admin/new'
	console.dir(req.baseUrl); // '/admin'
	console.dir(req.path); // '/new'
	next();
});

๐Ÿ“‘ 3. express.Response

๐Ÿ“‘ 4. express.Router

Creates a new router object:

  • router.all
  • router.param
  • router.route
  • router.use

๐Ÿ“‘ 5. express.Application

๐Ÿ“‘ bcrypt

  • compare(data, encrypted, cb)
  • hash(data, salt, cb)

๐Ÿ“‘ jwt

  • jwt.sign(payload, secretOrPrivateKey, [options, callback])
  • jwt.verify(token, secretOrPublicKey, [options, callback])

๐Ÿ“‘ express-jwt

๐Ÿ“‘ ่งฃๅ†ณๆœ‰ๆ•ˆๆœŸ็š„้—ฎ้ข˜


JWT Auth tokens + Session Refresh tokens is usually the goto in microservice authentication. A central auth service handles the authentication and hands out 2 tokens: Auth and Refresh.

The Auth token is a very short lived JWT that can be used for stateless authentication across any service. It is not stored on servers at all.

The Refresh token is a very long lived session token that is used by the auth service to regenerate Auth tokens as they expire. The Refresh token would be stored server site and be revokable at any time. If it has not been revoked or expired, then new Auth tokens are handed out as needed.

So, a request to a microservices would pass just the Auth token. if itโ€™s valid, everything proceeds. If it has expired, then a client request is made to the auth service, passing the Refresh token along. If the Refresh token is valid, a new Auth token is returned, then the original microservice request is re-tried.

๐Ÿ“‘ TODO


  • ็”จmongo๏ผŒmysql่ฟ˜ๆ˜ฏredis็ผ“ๅญ˜ authentication ็š„ไฟกๆฏ๏ผŸ
  • ๅฆ‚ไฝ•่งฃๅ†ณ็™ปๅฝ•ๆœ‰ๆ•ˆๆ—ถ้—ดๆ˜ฏ 10 ๅˆ†้’Ÿ็š„้—ฎ้ข˜๏ผŸ
  • oAuth2 ๅ…่ฎธๅพฎไฟก๏ผŒgoogle ็ญ‰่ดฆๅท็™ปๅ…ฅใ€‚

๐Ÿ“‘ ๅ‚่€ƒ

bradtraversy/nodejwtexample jsonplaceholder.typicode.com

Auth Flow: Auth Flow

JWT Flow: JWT Flow