oauth_models.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import enum
  2. from typing import Set, Union
  3. import flask
  4. class ScopeE(enum.Enum):
  5. """ScopeE to distinguish with Scope model"""
  6. EMAIL = "email"
  7. NAME = "name"
  8. OPENID = "openid"
  9. class ResponseType(enum.Enum):
  10. CODE = "code"
  11. TOKEN = "token"
  12. ID_TOKEN = "id_token"
  13. def get_scopes(request: flask.Request) -> Set[ScopeE]:
  14. scope_strs = _split_arg(request.args.getlist("scope"))
  15. return set([ScopeE(scope_str) for scope_str in scope_strs])
  16. def get_response_types(request: flask.Request) -> Set[ResponseType]:
  17. response_type_strs = _split_arg(request.args.getlist("response_type"))
  18. return set([ResponseType(r) for r in response_type_strs])
  19. def _split_arg(arg_input: Union[str, list]) -> Set[str]:
  20. """convert input response_type/scope into a set of string.
  21. arg_input = request.args.getlist(response_type|scope)
  22. Take into account different variations and their combinations
  23. - the split character is " " or ","
  24. - the response_type/scope passed as a list ?scope=scope_1&scope=scope_2
  25. """
  26. res = set()
  27. if type(arg_input) is str:
  28. if " " in arg_input:
  29. for x in arg_input.split(" "):
  30. if x:
  31. res.add(x.lower())
  32. elif "," in arg_input:
  33. for x in arg_input.split(","):
  34. if x:
  35. res.add(x.lower())
  36. else:
  37. res.add(arg_input)
  38. else:
  39. for arg in arg_input:
  40. res = res.union(_split_arg(arg))
  41. return res