A corporation manager and dashboard for EVE Online
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

436 行
16 KiB

  1. # -*- coding: utf-8 -*-
  2. from datetime import datetime, timedelta, timezone
  3. from flask import g, session, url_for
  4. from itsdangerous import BadSignature, URLSafeSerializer
  5. from . import baseLogger
  6. from .exceptions import AccessDeniedError
  7. __all__ = ["AuthManager"]
  8. class AuthManager:
  9. """Authentication manager. Handles user access and management."""
  10. EXPIRY_THRESHOLD = 30
  11. def __init__(self, config, eve):
  12. self._config = config
  13. self._eve = eve
  14. self._logger = baseLogger.getChild("auth")
  15. self._debug = self._logger.debug
  16. def _allocate_new_session(self):
  17. """Create a new session for the current user."""
  18. sid, created = g.db.new_session()
  19. session["id"] = sid
  20. session["date"] = int(created.replace(tzinfo=timezone.utc).timestamp())
  21. self._debug("Allocated session id=%d", sid)
  22. g._session_check = True
  23. g._session_expired = False
  24. def _get_session_id(self):
  25. """Return the current session ID, allocating a new one if necessary."""
  26. if "id" not in session:
  27. self._allocate_new_session()
  28. return session["id"]
  29. def _invalidate_session(self):
  30. """Mark the current session as invalid.
  31. Remove it from the database and from the user's cookies.
  32. """
  33. if "id" in session:
  34. sid = session["id"]
  35. g.db.drop_session(sid)
  36. self._debug("Dropped session id=%d", sid)
  37. session.clear()
  38. def _expire_session(self, always_notify=False):
  39. """Mark the current session as expired, then invalidate it."""
  40. if always_notify or session.get("expire-notify"):
  41. g._session_expired = True
  42. self._debug("Session expired id=%d", session["id"])
  43. self._invalidate_session()
  44. def _check_session(self, always_notify_expired=False):
  45. """Return whether the user has a valid, non-expired session.
  46. This checks for the session existing in the database, but does not
  47. check that the user is logged in or has any particular access roles.
  48. """
  49. if "id" not in session:
  50. return False
  51. if hasattr(g, "_session_check"):
  52. return g._session_check
  53. if "date" not in session:
  54. self._debug("Clearing dateless session id=%d", session["id"])
  55. session.clear()
  56. return False
  57. created = g.db.has_session(session["id"])
  58. if not created:
  59. self._expire_session(always_notify=always_notify_expired)
  60. g._session_check = False
  61. return False
  62. cstamp = int(created.replace(tzinfo=timezone.utc).timestamp())
  63. if session["date"] != cstamp:
  64. self._debug("Clearing bad-date session id=%d", session["id"])
  65. session.clear()
  66. return False
  67. g._session_check = True
  68. return True
  69. def _get_state_hash(self):
  70. """Return a hash of the user's session ID suitable for OAuth2 state.
  71. Allocates a new session ID if necessary.
  72. """
  73. key = self._config.get("auth.session_key")
  74. serializer = URLSafeSerializer(key)
  75. return serializer.dumps(self._get_session_id())
  76. def _verify_state_hash(self, state):
  77. """Confirm that a state hash is correct for the user's session.
  78. Assumes we've already checked the session ID. If the state is invalid,
  79. the session will be invalidated.
  80. """
  81. key = self._config.get("auth.session_key")
  82. serializer = URLSafeSerializer(key)
  83. try:
  84. value = serializer.loads(state)
  85. except BadSignature:
  86. self._debug("Bad signature for session id=%d", session["id"])
  87. self._invalidate_session()
  88. return False
  89. if value != session["id"]:
  90. self._debug("Got session id=%d, expected id=%d", value,
  91. session["id"])
  92. self._invalidate_session()
  93. return False
  94. return True
  95. def _fetch_new_token(self, code, refresh=False):
  96. """Given an auth code or refresh token, get a new token and other data.
  97. If refresh is True, code should be a refresh token, otherwise an auth
  98. code. If successful, we'll return a 5-tuple of (access_token,
  99. token_expiry, refresh_token, char_id, char_name). If the token was
  100. invalid, we'll return None. We may also raise EVEAPIError if there was
  101. an internal API error.
  102. """
  103. cid = self._config.get("auth.client_id")
  104. secret = self._config.get("auth.client_secret")
  105. result = self._eve.sso.get_access_token(cid, secret, code, refresh)
  106. if not result:
  107. return None
  108. token, expiry, refresh = result
  109. expires = (datetime.utcnow().replace(microsecond=0) +
  110. timedelta(seconds=expiry))
  111. result = self._eve.sso.get_character_info(token)
  112. if not result:
  113. return None
  114. char_id, char_name = result
  115. return token, expires, refresh, char_id, char_name
  116. def _get_token(self, cid):
  117. """Return a valid access token for the given character, or None.
  118. If the database doesn't have an auth entry for this character, return
  119. None. If the database's token is expired but the refresh token is
  120. valid, then refresh it, update the database, and return the new token.
  121. If the token has become invalid and couldn't be refreshed, drop the
  122. auth information from the database and return None.
  123. """
  124. result = g.db.get_auth(cid)
  125. if not result:
  126. self._debug("No auth info in database for char id=%d", cid)
  127. return None
  128. token, expires, refresh = result
  129. seconds_til_expiry = (expires - datetime.utcnow()).total_seconds()
  130. if seconds_til_expiry >= self.EXPIRY_THRESHOLD:
  131. self._debug("Using cached access token for char id=%d", cid)
  132. return token
  133. result = self._fetch_new_token(refresh, refresh=True)
  134. if not result:
  135. self._debug("Couldn't refresh token for char id=%d", cid)
  136. g.db.drop_auth(cid)
  137. return None
  138. token, expires, refresh, char_id, char_name = result
  139. if char_id != cid:
  140. self._debug("Refreshed token has incorrect char id=%d for "
  141. "char id=%d", char_id, cid)
  142. g.db.drop_auth(cid)
  143. return None
  144. self._debug("Using fresh access token for char id=%d", cid)
  145. g.db.put_character(cid, char_name)
  146. g.db.update_auth(cid, token, expires, refresh)
  147. return token
  148. def _is_corp_member(self, token, char_id):
  149. """"Return whether the given character is in the site's corp."""
  150. resp = self._eve.esi(token).v3.characters(char_id).get()
  151. return resp.get("corporation_id") == self._config.get("corp.id")
  152. def _check_access(self, token, char_id):
  153. """"Check whether the given character is allowed to access this site.
  154. If allowed, do nothing. If not, raise AccessDeniedError.
  155. """
  156. if not self._is_corp_member(token, char_id):
  157. self._debug("Access denied per corp membership for char id=%d "
  158. "session id=%d", char_id, session["id"])
  159. g.db.drop_auth(char_id)
  160. self._invalidate_session()
  161. raise AccessDeniedError()
  162. def _cache_token(self, cid, token):
  163. """Cache the given token for this request."""
  164. if hasattr(g, "_cached_tokens"):
  165. g._cached_tokens[cid] = token
  166. else:
  167. g._cached_tokens = {cid: token}
  168. def _update_prop_cache(self, module, prop, value):
  169. """Update the value of a character module property in the cache."""
  170. if hasattr(g, "_character_modprops"):
  171. propcache = g._character_modprops
  172. else:
  173. propcache = g._character_modprops = {module: {}}
  174. if module not in propcache:
  175. propcache[module] = {}
  176. propcache[module][prop] = value
  177. def get_character_id(self):
  178. """Return the character ID associated with the current session.
  179. Return None if the session is invalid or is not associated with a
  180. character.
  181. """
  182. if not self._check_session():
  183. return None
  184. if not hasattr(g, "_character_id"):
  185. g._character_id = g.db.read_session(session["id"])
  186. return g._character_id
  187. def get_character_prop(self, prop):
  188. """Look up a property for the current session's character.
  189. Return None if the session is invalid, is not associated with a
  190. character, or the property has no non-default value.
  191. """
  192. cid = self.get_character_id()
  193. if not cid:
  194. return None
  195. if not hasattr(g, "_character_props"):
  196. g._character_props = g.db.read_character(cid)
  197. return g._character_props.get(prop)
  198. def set_character_style(self, style):
  199. """Update the current user's style and return whether successful."""
  200. cid = self.get_character_id()
  201. if not cid:
  202. return False
  203. style = style.strip().lower()
  204. if style not in self._config.get("style.enabled"):
  205. return False
  206. self._debug("Setting style to %s for char id=%d", style, cid)
  207. g.db.update_character(cid, "style", style)
  208. if hasattr(g, "_character_props"):
  209. delattr(g, "_character_props")
  210. return True
  211. def get_character_modprop(self, module, prop):
  212. """Look up a module property for the current session's character.
  213. Return None if the session is invalid, is not associated with a
  214. character, or the property has no non-default value.
  215. """
  216. cid = self.get_character_id()
  217. if not cid:
  218. return None
  219. if hasattr(g, "_character_modprops"):
  220. propcache = g._character_modprops
  221. if module in propcache and prop in propcache[module]:
  222. return propcache[module][prop]
  223. value = g.db.get_character_modprop(cid, module, prop)
  224. self._update_prop_cache(module, prop, value)
  225. return value
  226. def set_character_modprop(self, module, prop, value):
  227. """Update a module property for the current session's character.
  228. Return whether successful.
  229. """
  230. cid = self.get_character_id()
  231. if not cid:
  232. return False
  233. self._debug("Setting module %s property %s to %s for char id=%d",
  234. module, prop, value, cid)
  235. g.db.set_character_modprop(cid, module, prop, value)
  236. self._update_prop_cache(module, prop, value)
  237. return True
  238. def get_token(self, cid=None):
  239. """Return a valid token for the given character.
  240. If no character is given, we use the current session's character. If a
  241. token couldn't be retrieved, return None.
  242. Assuming we want the current character's token and this is called in a
  243. restricted route (following a True result from is_authenticated), this
  244. function makes no API calls and should always succeed. If it is called
  245. in other circumstances, it may return None or raise EVEAPIError.
  246. """
  247. if cid is None:
  248. cid = self.get_character_id()
  249. if not cid:
  250. return None
  251. if hasattr(g, "_cached_tokens"):
  252. if cid in g._cached_tokens:
  253. return g._cached_tokens[cid]
  254. token = self._get_token(cid)
  255. self._cache_token(cid, token)
  256. return token
  257. def is_authenticated(self):
  258. """Return whether the user has permission to access this site.
  259. We confirm that they have a valid, non-expired session that is
  260. associated with a character that is permitted to be here.
  261. EVEAPIError or AccessDeniedError may be raised.
  262. """
  263. cid = self.get_character_id()
  264. if not cid:
  265. return False
  266. self._debug("Checking auth for session id=%d", session["id"])
  267. token = self._get_token(cid)
  268. if not token:
  269. self._debug("No valid token for char id=%d session id=%d", cid,
  270. session["id"])
  271. self._invalidate_session()
  272. return False
  273. self._check_access(token, cid)
  274. self._debug("Access granted for char id=%d session id=%d", cid,
  275. session["id"])
  276. g.db.touch_session(session["id"])
  277. self._cache_token(cid, token)
  278. return True
  279. def make_login_link(self):
  280. """Return a complete EVE SSO link that the user can use to log in."""
  281. cid = self._config.get("auth.client_id")
  282. target = url_for("login", _external=True, _scheme=self._config.scheme)
  283. scopes = self._config.collect_scopes()
  284. state = self._get_state_hash()
  285. return self._eve.sso.get_authorize_url(cid, target, scopes, state)
  286. def handle_login(self, code, state):
  287. """Given an OAuth2 code and state, try to authenticate the user.
  288. If the user has a legitimate session and the state is valid, we'll
  289. check the code with EVE SSO to fetch an authentication token. If the
  290. token corresponds to a character that is allowed to access the site,
  291. we'll update their session to indicate so.
  292. Return whether authentication was successful. EVEAPIError or
  293. AccessDeniedError may be raised.
  294. """
  295. if not code or not state:
  296. return False
  297. if "id" in session:
  298. self._debug("Logging in session id=%d", session["id"])
  299. if not self._check_session(always_notify_expired=True):
  300. return False
  301. if not self._verify_state_hash(state):
  302. return False
  303. sid = session["id"]
  304. result = self._fetch_new_token(code)
  305. if not result:
  306. self._debug("Couldn't fetch token for session id=%d", sid)
  307. self._invalidate_session()
  308. return False
  309. token, expires, refresh, char_id, char_name = result
  310. self._check_access(token, char_id)
  311. self._debug("Logged in char id=%d session id=%d", char_id, sid)
  312. g.db.put_character(char_id, char_name)
  313. g.db.set_auth(char_id, token, expires, refresh)
  314. g.db.attach_session(sid, char_id)
  315. g.db.touch_session(sid)
  316. session["expire-notify"] = True
  317. return True
  318. def handle_logout(self):
  319. """Log out the user if they are logged in.
  320. Invalidates their session and clears the session cookie.
  321. """
  322. if "id" in session:
  323. self._debug("Logging out session id=%d", session["id"])
  324. self._invalidate_session()
  325. def get_valid_characters(self):
  326. """Iterate over all valid corp members that we have tokens for.
  327. Each character is returned as a 2-tuple of (char_id, token).
  328. This function may make a large number of API queries (up to three per
  329. character in the corp), hence it is a generator.
  330. """
  331. chars = g.db.get_authed_characters()
  332. for cid, token, expires, refresh in chars:
  333. seconds_til_expiry = (expires - datetime.utcnow()).total_seconds()
  334. if seconds_til_expiry < self.EXPIRY_THRESHOLD:
  335. result = self._fetch_new_token(refresh, refresh=True)
  336. if not result:
  337. self._debug("Couldn't refresh token for char id=%d", cid)
  338. g.db.drop_auth(cid)
  339. continue
  340. token, expires, refresh, char_id, char_name = result
  341. if char_id != cid:
  342. self._debug("Refreshed token has incorrect char id=%d for "
  343. "char id=%d", char_id, cid)
  344. g.db.drop_auth(cid)
  345. continue
  346. g.db.put_character(cid, char_name)
  347. g.db.update_auth(cid, token, expires, refresh)
  348. if self._is_corp_member(token, cid):
  349. yield cid, token