API 文档

  1. 通过访问 http://Jumpserver的URL地址/docs 来访问( 如 http://192.168.244.144/docs )
  2. 注:需要打开 debug 模式
  3.  
  4. $ vi jumpserver/config.yml
  5.  
  6. ...
  7. Debug: true
  • 手动调用 api 的方法
  1. $ curl -X POST http://localhost/api/users/v1/auth/ -H 'Content-Type: application/json' -d '{"username": "admin", "password": "admin"}' # 获取token
  2. {"token":"937b38011acf499eb474e2fecb424ab3"} # 获取到的token
  3.  
  4. # 如果开启了 MFA, 则返回的是 seed, 需要携带 seed 和 otp_code 再次提交一次才能获取到 token
  5. curl -X POST http://localhost/api/users/v1/auth/ -H 'Content-Type: application/json' -d '{"username": "admin", "password": "admin"}'
  6. {"code":101, "msg":"请携带seed值, 进行MFA二次认证", "otp_url":"/api/users/v1/otp/auth/", "seed":"629ba0935a624bd9b21e31c19e0cc8cb"}
  7. $ curl -X POST http://localhost/api/users/v1/otp/auth/ -H 'Content-Type: application/json' -H 'cache-control: no-cache' -d '{"seed": "629ba0935a624bd9b21e31c19e0cc8cb", "otp_code": "202123"}'
  8. {"token":"937b38011acf499eb474e2fecb424ab3"}
  9. # otp_code 为动态密码
  10.  
  11. $ curl -H 'Authorization: Bearer 937b38011acf499eb474e2fecb424ab3' -H "Content-Type:application/json" http://localhost/api/users/v1/users/
  12. # 使用token访问, token有效期 1小时
  13.  
  14. # 也可以创建一个永久 private_token, 避免二次认证
  15. $ source /opt/py3/bin/activate
  16. $ cd /opt/jumpserver/apps
  17. $ python manage.py shell
  18. >>> from users.models import User
  19. >>> u = User.objects.get(username='admin')
  20. >>> u.create_private_token()
  21. 937b38011acf499eb474e2fecb424ab3
  22.  
  23. $ curl -H 'Authorization: Token 937b38011acf499eb474e2fecb424ab3' -H "Content-Type:application/json" http://localhost/api/users/v1/users/
  • python代码示例
  1. import requests
  2. import json
  3. from pprint import pprint
  4.  
  5. def get_token():
  6.  
  7. url = 'https://demo.jumpserver.org/api/users/v1/auth/'
  8.  
  9. query_args = {
  10. "username": "admin",
  11. "password": "admin"
  12. }
  13.  
  14. response = requests.post(url, data=query_args)
  15.  
  16. return json.loads(response.text)['token']
  17.  
  18. def get_user_info():
  19.  
  20. url = 'https://demo.jumpserver.org/api/users/v1/users/'
  21.  
  22. token = get_token()
  23.  
  24. header_info = { "Authorization": 'Bearer ' + token }
  25.  
  26. response = requests.get(url, headers=header_info)
  27.  
  28. pprint(json.loads(response.text))
  29.  
  30. get_user_info()