Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import jwt
- from bond.app import create_app
- from bond.models import db
- from bond.models.bond import *
- from bond.models.auth import Permission, EndpointAction, Role
- from tests.unit.bond.models.generator_test import *
- from tests.unit.bond.fixtures import get_jwt_payload, JWTRS256_PRIVATE_KEY
- app = create_app('../config.yaml')
- app_ctx = app.app_context()
- app_ctx.push()
- PERMISSION = {
- '0': 'invalid_permission',
- '100': 'bond_issuer_actions',
- '200': 'bond_investor_actions',
- '300': 'bond_admin_actions',
- '400': 'bond_legal_actions',
- }
- ENDPOINT_ACTION = [
- {"path": "/bonds", "method": "GET"}, # 0
- {"path": "/bonds", "method": "POST"},
- {"path": "/bonds/<uuid:pk>", "method": "GET"},
- {"path": "/bonds/<uuid:pk>", "method": "PUT"},
- {"path": "/bonds/<uuid:pk>", "method": "DELETE"},
- {"path": "/bonds/<uuid:pk>/launch", "method": "PUT"}, # 5
- {"path": "/bonds/<uuid:pk>/admin-reject", "method": "PUT"},
- {"path": "/bonds/<uuid:pk>/admin-approve", "method": "PUT"},
- {"path": "/bonds/<uuid:pk>/legal-approve", "method": "PUT"},
- {"path": "/bonds/<uuid:pk>/legal-reject", "method": "PUT"},
- {"path": "/bonds/purchase", "method": "GET"}, # 10
- {"path": "/issuers", "method": "GET"},
- {"path": "/issuers/<uuid:pk>", "method": "GET"},
- {"path": "/investors", "method": "GET"},
- {"path": "/investors/<uuid:pk>", "method": "GET"},
- {"path": "/investor/purchase/<uuid:pk>", "method": "POST"}, # 15
- {"path": "/investor/transactions/<uuid:pk>", "method": "GET"},
- {"path": "/investor/update-balance/<uuid:pk>", "method": "POST"},
- {"path": "/investor/view-balance/<uuid:pk>", "method": "GET"},
- {"path": "/investor/admin-approve/<uuid:pk>", "method": "PUT"},
- {"path": "/investor/admin-reject/<uuid:pk>", "method": "PUT"}, # 20
- {"path": "/investor/dirty-price/<uuid:pk>", "method": "POST"},
- {"path": "/investor/purchase-history", "method": "GET"},
- ]
- ROLE = {
- 'issuer': 'Bond Issuer',
- 'investor': 'Bond Investor',
- 'admin': 'Bond Admin',
- 'legal': 'Bond Legal'
- }
- ENDPOINT_ACTION_PERMISSION_ROLE_MAP = {
- '100': {
- 'name': 'bond_issuer_actions',
- 'slug': 'bond_issuer_actions',
- 'roles': ['issuer'],
- 'endpoint_actions': [0, 1, 2, 3, 4, 5, 12]
- },
- '200': {
- 'name': 'bond_investor_actions',
- 'slug': 'bond_investor_actions',
- 'roles': ['investor'],
- 'endpoint_actions': [0, 2, 10, 11, 12, 14, 15, 16, 17, 18, 21, 22]
- },
- '300': {
- 'name': 'bond_admin_actions',
- 'slug': 'bond_admin_actions',
- 'roles': ['admin'],
- 'endpoint_actions': [0, 2, 6, 7, 11, 12, 13, 14, 19, 20]
- },
- '400': {
- 'name': 'bond_legal_actions',
- 'slug': 'bond_legal_actions',
- 'roles': ['legal'],
- 'endpoint_actions': [0, 2, 8, 9, 11, 12, 13, 14]
- }
- }
- def get_jwt_token(payload):
- return jwt.encode(payload, JWTRS256_PRIVATE_KEY, algorithm='RS256')
- def get_api_headers_with_authorization(jwt_header, jwt_token):
- if isinstance(jwt_token, bytes):
- jwt_token = jwt_token.decode('ascii')
- return {
- 'Accept': 'application/json',
- 'Content-Type': 'application/json',
- 'Authorization': "{jwt_header} {jwt_token}".format(jwt_header=jwt_header,
- jwt_token=jwt_token),
- }
- def create_role_permission_endpoint():
- endpoints = []
- for i in ENDPOINT_ACTION:
- endpoint_action = EndpointAction()
- endpoint_action.path = i['path']
- endpoint_action.method = i['method']
- endpoints.append(endpoint_action)
- db.session.add_all(endpoints)
- roles = {}
- for key, role in ROLE.items():
- r = Role()
- r.slug = name
- roles[key] = r
- for k, v in roles.items():
- db.session.add(v)
- for id, data in ENDPOINT_ACTION_PERMISSION_ROLE_MAP.items():
- perm = Permission()
- perm.name = data['name']
- perm.slug = data['slug']
- perm.roles.append(roles[data['roles'][0]])
- perm.endpoint_actions.extend(
- [endpoints[i] for i in data['endpoint_actions']]
- )
- db.session.add(perm)
- db.session.commit()
- def create_jwt_header(user_permission_ids):
- jwt_payload = get_jwt_payload(
- name="UserName",
- user_id='1000',
- user_roles=['Fake Role'],
- user_permission_ids=user_permission_ids
- )
- jwt_token = get_jwt_token(jwt_payload)
- jwt_headers = get_api_headers_with_authorization(
- app.config["JWT_HEADER_TYPE"],
- jwt_token
- )
- return jwt_headers
- if __name__ == '__main__':
- create_role_permission_endpoint()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement