Advertisement
doanhtu

New generate

May 1st, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.73 KB | None | 0 0
  1. import jwt
  2.  
  3. from bond.app import create_app
  4. from bond.models import db
  5. from bond.models.bond import *
  6. from bond.models.auth import Permission, EndpointAction, Role
  7. from tests.unit.bond.models.generator_test import *
  8. from tests.unit.bond.fixtures import get_jwt_payload, JWTRS256_PRIVATE_KEY
  9.  
  10.  
  11. app = create_app('../config.yaml')
  12. app_ctx = app.app_context()
  13. app_ctx.push()
  14.  
  15.  
  16. PERMISSION = {
  17.     '0': 'invalid_permission',
  18.     '100': 'bond_issuer_actions',
  19.     '200': 'bond_investor_actions',
  20.     '300': 'bond_admin_actions',
  21.     '400': 'bond_legal_actions',
  22. }
  23.  
  24.  
  25. ENDPOINT_ACTION = [
  26.     {"path": "/bonds", "method": "GET"},  # 0
  27.     {"path": "/bonds", "method": "POST"},
  28.     {"path": "/bonds/<uuid:pk>", "method": "GET"},
  29.     {"path": "/bonds/<uuid:pk>", "method": "PUT"},
  30.     {"path": "/bonds/<uuid:pk>", "method": "DELETE"},
  31.     {"path": "/bonds/<uuid:pk>/launch", "method": "PUT"},  # 5
  32.     {"path": "/bonds/<uuid:pk>/admin-reject", "method": "PUT"},
  33.     {"path": "/bonds/<uuid:pk>/admin-approve", "method": "PUT"},
  34.     {"path": "/bonds/<uuid:pk>/legal-approve", "method": "PUT"},
  35.     {"path": "/bonds/<uuid:pk>/legal-reject", "method": "PUT"},
  36.     {"path": "/bonds/purchase", "method": "GET"},  # 10
  37.     {"path": "/issuers", "method": "GET"},
  38.     {"path": "/issuers/<uuid:pk>", "method": "GET"},
  39.     {"path": "/investors", "method": "GET"},
  40.     {"path": "/investors/<uuid:pk>", "method": "GET"},
  41.     {"path": "/investor/purchase/<uuid:pk>", "method": "POST"},  # 15
  42.     {"path": "/investor/transactions/<uuid:pk>", "method": "GET"},
  43.     {"path": "/investor/update-balance/<uuid:pk>", "method": "POST"},
  44.     {"path": "/investor/view-balance/<uuid:pk>", "method": "GET"},
  45.     {"path": "/investor/admin-approve/<uuid:pk>", "method": "PUT"},
  46.     {"path": "/investor/admin-reject/<uuid:pk>", "method": "PUT"},  # 20
  47.     {"path": "/investor/dirty-price/<uuid:pk>", "method": "POST"},
  48.     {"path": "/investor/purchase-history", "method": "GET"},
  49. ]
  50.  
  51.  
  52. ROLE = {
  53.     'issuer': 'Bond Issuer',
  54.     'investor': 'Bond Investor',
  55.     'admin': 'Bond Admin',
  56.     'legal': 'Bond Legal'
  57. }
  58.  
  59.  
  60. ENDPOINT_ACTION_PERMISSION_ROLE_MAP = {
  61.     '100': {
  62.         'name': 'bond_issuer_actions',
  63.         'slug': 'bond_issuer_actions',
  64.         'roles': ['issuer'],
  65.         'endpoint_actions': [0, 1, 2, 3, 4, 5, 12]
  66.     },
  67.     '200': {
  68.         'name': 'bond_investor_actions',
  69.         'slug': 'bond_investor_actions',
  70.         'roles': ['investor'],
  71.         'endpoint_actions': [0, 2, 10, 11, 12, 14, 15, 16, 17, 18, 21, 22]
  72.     },
  73.     '300': {
  74.         'name': 'bond_admin_actions',
  75.         'slug': 'bond_admin_actions',
  76.         'roles': ['admin'],
  77.         'endpoint_actions': [0, 2, 6, 7, 11, 12, 13, 14, 19, 20]
  78.     },
  79.     '400': {
  80.         'name': 'bond_legal_actions',
  81.         'slug': 'bond_legal_actions',
  82.         'roles': ['legal'],
  83.         'endpoint_actions': [0, 2, 8, 9, 11, 12, 13, 14]
  84.     }
  85. }
  86.  
  87.  
  88. def get_jwt_token(payload):
  89.     return jwt.encode(payload, JWTRS256_PRIVATE_KEY, algorithm='RS256')
  90.  
  91.  
  92. def get_api_headers_with_authorization(jwt_header, jwt_token):
  93.     if isinstance(jwt_token, bytes):
  94.         jwt_token = jwt_token.decode('ascii')
  95.     return {
  96.         'Accept': 'application/json',
  97.         'Content-Type': 'application/json',
  98.         'Authorization': "{jwt_header} {jwt_token}".format(jwt_header=jwt_header,
  99.                                                            jwt_token=jwt_token),
  100.     }
  101.  
  102.  
  103. def create_role_permission_endpoint():
  104.     endpoints = []
  105.     for i in ENDPOINT_ACTION:
  106.         endpoint_action = EndpointAction()
  107.         endpoint_action.path = i['path']
  108.         endpoint_action.method = i['method']
  109.         endpoints.append(endpoint_action)
  110.     db.session.add_all(endpoints)
  111.  
  112.     roles = {}
  113.     for key, role in ROLE.items():
  114.         r = Role()
  115.         r.slug = name
  116.         roles[key] = r
  117.  
  118.     for k, v in roles.items():
  119.         db.session.add(v)
  120.  
  121.     for id, data in ENDPOINT_ACTION_PERMISSION_ROLE_MAP.items():
  122.         perm = Permission()
  123.         perm.name = data['name']
  124.         perm.slug = data['slug']
  125.         perm.roles.append(roles[data['roles'][0]])
  126.         perm.endpoint_actions.extend(
  127.                 [endpoints[i] for i in data['endpoint_actions']]
  128.         )
  129.         db.session.add(perm)
  130.     db.session.commit()
  131.  
  132.  
  133. def create_jwt_header(user_permission_ids):
  134.     jwt_payload = get_jwt_payload(
  135.         name="UserName",
  136.         user_id='1000',
  137.         user_roles=['Fake Role'],
  138.         user_permission_ids=user_permission_ids
  139.     )
  140.     jwt_token = get_jwt_token(jwt_payload)
  141.     jwt_headers = get_api_headers_with_authorization(
  142.         app.config["JWT_HEADER_TYPE"],
  143.         jwt_token
  144.     )
  145.     return jwt_headers
  146.  
  147.  
  148. if __name__ == '__main__':
  149.     create_role_permission_endpoint()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement