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โshttp 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 token
s + Session Refresh token
s 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 token
s 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 token
s 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 ็ญ่ดฆๅท็ปๅ ฅใ