Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/jupyter_server/auth/utils.py: 30%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

33 statements  

1"""A module with various utility methods for authorization in Jupyter Server.""" 

2 

3# Copyright (c) Jupyter Development Team. 

4# Distributed under the terms of the Modified BSD License. 

5import importlib 

6import random 

7import re 

8import warnings 

9 

10 

11def warn_disabled_authorization(): 

12 """DEPRECATED, does nothing""" 

13 warnings.warn( 

14 "jupyter_server.auth.utils.warn_disabled_authorization is deprecated", 

15 DeprecationWarning, 

16 stacklevel=2, 

17 ) 

18 

19 

20HTTP_METHOD_TO_AUTH_ACTION = { 

21 "GET": "read", 

22 "HEAD": "read", 

23 "OPTIONS": "read", 

24 "POST": "write", 

25 "PUT": "write", 

26 "PATCH": "write", 

27 "DELETE": "write", 

28 "WEBSOCKET": "execute", 

29} 

30 

31 

32def get_regex_to_resource_map(): 

33 """Returns a dictionary with all of Jupyter Server's 

34 request handler URL regex patterns mapped to 

35 their resource name. 

36 

37 e.g. 

38 { "/api/contents/<regex_pattern>": "contents", ...} 

39 """ 

40 from jupyter_server.serverapp import JUPYTER_SERVICE_HANDLERS 

41 

42 modules = [] 

43 for mod_name in JUPYTER_SERVICE_HANDLERS.values(): 

44 if mod_name: 

45 modules.extend(mod_name) 

46 resource_map = {} 

47 for handler_module in modules: 

48 mod = importlib.import_module(handler_module) 

49 name = mod.AUTH_RESOURCE 

50 for handler in mod.default_handlers: 

51 url_regex = handler[0] 

52 resource_map[url_regex] = name 

53 # terminal plugin doesn't have importable url patterns 

54 # get these from terminal/__init__.py 

55 for url_regex in [ 

56 r"/terminals/websocket/(\w+)", 

57 "/api/terminals", 

58 r"/api/terminals/(\w+)", 

59 ]: 

60 resource_map[url_regex] = "terminals" 

61 return resource_map 

62 

63 

64def match_url_to_resource(url, regex_mapping=None): 

65 """Finds the JupyterHandler regex pattern that would 

66 match the given URL and returns the resource name (str) 

67 of that handler. 

68 

69 e.g. 

70 /api/contents/... returns "contents" 

71 """ 

72 if not regex_mapping: 

73 regex_mapping = get_regex_to_resource_map() 

74 for regex, auth_resource in regex_mapping.items(): 

75 pattern = re.compile(regex) 

76 if pattern.fullmatch(url): 

77 return auth_resource 

78 

79 

80# From https://en.wikipedia.org/wiki/Moons_of_Jupiter 

81moons_of_jupyter = [ 

82 "Metis", 

83 "Adrastea", 

84 "Amalthea", 

85 "Thebe", 

86 "Io", 

87 "Europa", 

88 "Ganymede", 

89 "Callisto", 

90 "Themisto", 

91 "Leda", 

92 "Ersa", 

93 "Pandia", 

94 "Himalia", 

95 "Lysithea", 

96 "Elara", 

97 "Dia", 

98 "Carpo", 

99 "Valetudo", 

100 "Euporie", 

101 "Eupheme", 

102 # 'S/2003 J 18', 

103 # 'S/2010 J 2', 

104 "Helike", 

105 # 'S/2003 J 16', 

106 # 'S/2003 J 2', 

107 "Euanthe", 

108 # 'S/2017 J 7', 

109 "Hermippe", 

110 "Praxidike", 

111 "Thyone", 

112 "Thelxinoe", 

113 # 'S/2017 J 3', 

114 "Ananke", 

115 "Mneme", 

116 # 'S/2016 J 1', 

117 "Orthosie", 

118 "Harpalyke", 

119 "Iocaste", 

120 # 'S/2017 J 9', 

121 # 'S/2003 J 12', 

122 # 'S/2003 J 4', 

123 "Erinome", 

124 "Aitne", 

125 "Herse", 

126 "Taygete", 

127 # 'S/2017 J 2', 

128 # 'S/2017 J 6', 

129 "Eukelade", 

130 "Carme", 

131 # 'S/2003 J 19', 

132 "Isonoe", 

133 # 'S/2003 J 10', 

134 "Autonoe", 

135 "Philophrosyne", 

136 "Cyllene", 

137 "Pasithee", 

138 # 'S/2010 J 1', 

139 "Pasiphae", 

140 "Sponde", 

141 # 'S/2017 J 8', 

142 "Eurydome", 

143 # 'S/2017 J 5', 

144 "Kalyke", 

145 "Hegemone", 

146 "Kale", 

147 "Kallichore", 

148 # 'S/2011 J 1', 

149 # 'S/2017 J 1', 

150 "Chaldene", 

151 "Arche", 

152 "Eirene", 

153 "Kore", 

154 # 'S/2011 J 2', 

155 # 'S/2003 J 9', 

156 "Megaclite", 

157 "Aoede", 

158 # 'S/2003 J 23', 

159 "Callirrhoe", 

160 "Sinope", 

161] 

162 

163 

164def get_anonymous_username() -> str: 

165 """ 

166 Get a random user-name based on the moons of Jupyter. 

167 This function returns names like "Anonymous Io" or "Anonymous Metis". 

168 """ 

169 return moons_of_jupyter[random.randint(0, len(moons_of_jupyter) - 1)] # noqa: S311