auth Documentation · Auth is a module that makes authorization simple and also scalable and...
Transcript of auth Documentation · Auth is a module that makes authorization simple and also scalable and...
![Page 1: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/1.jpg)
auth DocumentationRelease latest
Sep 07, 2018
![Page 2: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/2.jpg)
![Page 3: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/3.jpg)
Contents
1 What is Auth? 3
2 requirements 5
3 Installation 7
4 Show me an example 9
5 Authorization Methods 11
6 RESTful API 13
7 RESTful API helpers 15
8 API Methods 17
9 Deployment 19
10 Dockerizing 21
11 Copyright 23
12 Documentation 25
13 Unit Tests and Coverage 27
14 To DO 29
i
![Page 4: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/4.jpg)
ii
![Page 5: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/5.jpg)
auth Documentation, Release latest
RESTful, Simple Authorization system with ZERO configuration.
Contents 1
![Page 6: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/6.jpg)
auth Documentation, Release latest
2 Contents
![Page 7: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/7.jpg)
CHAPTER 1
What is Auth?
Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTfulAPI for use in micro-service architectures and platforms. It is originally desinged to use in Appido, a scalable mediamarket in Iran.
It supports Python2.6+ and if you have a mongodb backbone, you need ZERO configurations steps. Just typeauth-server and press enter!
I use Travis and Codecov to keep myself honest.
3
![Page 8: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/8.jpg)
auth Documentation, Release latest
4 Chapter 1. What is Auth?
![Page 9: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/9.jpg)
CHAPTER 2
requirements
You need to access to mongodb. If you are using a remote mongodb, provide these environment variables:
MONGO_HOST and MONGO_PORT
5
![Page 10: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/10.jpg)
auth Documentation, Release latest
6 Chapter 2. requirements
![Page 11: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/11.jpg)
CHAPTER 3
Installation
pip install auth
7
![Page 12: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/12.jpg)
auth Documentation, Release latest
8 Chapter 3. Installation
![Page 13: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/13.jpg)
CHAPTER 4
Show me an example
ok, lets image you have two users, Jack and Sara. Sara can cook and Jack can dance. Both can laugh.
You also need to choose a secret key for your application. Because you may want to use Auth in various tools andeach must have a secret key for seperating their scope.
my_secret_key = "pleaSeDoN0tKillMyC_at"from auth import Authorizationcas = Authorization(my_secret_key)
Now, Lets add 3 groups, Cookers, Dancers and Laughers. Remember that groups are Roles. So when we create agroup, indeed we create a role:
cas.add_role('cookers')cas.add_role('dancers')cas.add_role('laughers')
Ok, great. You have 3 groups and you need to authorize them to do special things.
cas.add_permission('cookers', 'cook')cas.add_permission('dancers', 'dance')cas.add_permission('laughers', 'laugh')
Good. You let cookers to cook and dancers to dance etc. . . The final part is to set memberships for Sara and Jack:
cas.add_membership('sara', 'cookers')cas.add_membership('sara', 'laughers')cas.add_membership('jack', 'dancers')cas.add_membership('jack', 'laughers')
That’s all we need. Now lets ensure that jack can dance:
if cas.user_has_permission('jack', 'dance'):print('YES!!! Jack can dance.')
9
![Page 14: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/14.jpg)
auth Documentation, Release latest
10 Chapter 4. Show me an example
![Page 15: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/15.jpg)
CHAPTER 5
Authorization Methods
use pydoc to see all methods:
pydoc auth.Authorization
11
![Page 16: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/16.jpg)
auth Documentation, Release latest
12 Chapter 5. Authorization Methods
![Page 17: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/17.jpg)
CHAPTER 6
RESTful API
Lets run the server on port 4000:
from auth import api, serveserve('localhost', 4000, api)
Or, from version 0.1.2+ you can use this command:
auth-server
Simple! Authorization server is ready to use.
You can use it via simple curl or using mighty Requests module. So in you remote application, you can do somethinglike this:
import requestssecret_key = "pleaSeDoN0tKillMyC_at"auth_api = "http://127.0.0.1:4000/api"
Lets create admin group:
requests.post(auth_api+'/role/'+secret_key+'/admin')
And lets make Jack an admin:
requests.post(auth_api+'/permission/'+secret_key+'/jack/admin')
And finally let’s check if Sara still can cook:
requests.get(auth_api+'/has_permission/'+secret_key+'/sara/cook')
13
![Page 18: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/18.jpg)
auth Documentation, Release latest
14 Chapter 6. RESTful API
![Page 19: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/19.jpg)
CHAPTER 7
RESTful API helpers
auth comes with a helper class that makes your life easy.
from auth.client import Clientservice = Client('srv201', 'http://192.168.99.100:4000')print(service)service.get_roles()service.add_role(role='admin')
15
![Page 20: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/20.jpg)
auth Documentation, Release latest
16 Chapter 7. RESTful API helpers
![Page 21: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/21.jpg)
CHAPTER 8
API Methods
pydoc auth.CAS.REST.service
• /ping [GET]
Ping API, useful for your monitoring tools
• /api/membership/{KEY}/{user}/{role} [GET/POST/DELETE]
Adding, removing and getting membership information.
• /api/permission/{KEY}/{role}/{name} [GET/POST/DELETE]
Adding, removing and getting permissions
• /api/has_permission/{KEY}/{user}/{name} [GET]
Getting user permission info
• /api/role/{KEY}/{role} [GET/POST/DELETE]
Adding, removing and getting roles
• /api/which_roles_can/{KEY}/{name} [GET]
For example: Which roles can send_mail?
• /api/which_users_can/{KEY}/{name} [GET]
For example: Which users can send_mail?
• /api/user_permissions/{KEY}/{user} [GET]
Get all permissions that a user has
• /api/role_permissions/{KEY}/{role} [GET]
Get all permissions that a role has
• /api/user_roles/{KEY}/{user} [GET]
Get roles that user assinged to
17
![Page 22: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/22.jpg)
auth Documentation, Release latest
• /api/roles/{KEY} [GET]
Get all available roles
18 Chapter 8. API Methods
![Page 23: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/23.jpg)
CHAPTER 9
Deployment
Deploying Auth module in production environment is easy:
gunicorn auth:api
19
![Page 24: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/24.jpg)
auth Documentation, Release latest
20 Chapter 9. Deployment
![Page 25: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/25.jpg)
CHAPTER 10
Dockerizing
It’s simple:
docker build -t python/auth-server https://raw.githubusercontent.com/ourway/auth/→˓master/Dockerfiledocker run --name=auth -e MONGO_HOST='192.168.99.100' -p 4000:4000 -d --→˓restart=always --link=mongodb-server python/auth-server
21
![Page 26: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/26.jpg)
auth Documentation, Release latest
22 Chapter 10. Dockerizing
![Page 28: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/28.jpg)
auth Documentation, Release latest
24 Chapter 11. Copyright
![Page 29: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/29.jpg)
CHAPTER 12
Documentation
Feel free to dig into source code. If you think you can improve the documentation, please do so and send me a pullrequest.
25
![Page 30: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/30.jpg)
auth Documentation, Release latest
26 Chapter 12. Documentation
![Page 31: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/31.jpg)
CHAPTER 13
Unit Tests and Coverage
I am trying to add tests as much as I can, but still there are areas that need improvement.
27
![Page 32: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/32.jpg)
auth Documentation, Release latest
28 Chapter 13. Unit Tests and Coverage
![Page 33: auth Documentation · Auth is a module that makes authorization simple and also scalable and powerful. It also has a beautiful RESTful API for use in micro-service architectures and](https://reader034.fdocuments.us/reader034/viewer/2022042810/5f999076149dcc6ada0273db/html5/thumbnails/33.jpg)
CHAPTER 14
To DO
• Add Authentication features
• Improve Code Coverage
29