Returns result, bool message, string auth, None or class Auth() object
(session,
auth_code_type,
user = None,
project_string_id = None,
permission_level = None,
email_sent_to = None,
org = None)
| 14 | ## NOT checking code creating NEW one |
| 15 | # existing is just for magic login that's why it's confusing |
| 16 | def new(session, |
| 17 | auth_code_type, |
| 18 | user = None, |
| 19 | project_string_id = None, |
| 20 | permission_level = None, |
| 21 | email_sent_to = None, |
| 22 | org = None): |
| 23 | """ |
| 24 | |
| 25 | Returns |
| 26 | result, bool |
| 27 | message, string |
| 28 | auth, None or class Auth() object |
| 29 | |
| 30 | """ |
| 31 | |
| 32 | # TODO review why using "user" parameter here seems like not needed |
| 33 | # (ie could just use email_sent_to or email?) |
| 34 | |
| 35 | # Check if existing code |
| 36 | # Only allow one of each type? |
| 37 | |
| 38 | # THis could get pretty messy with expired codes... |
| 39 | |
| 40 | # TODO not clear on use of this type of filer, ie if using a code to send to multiple |
| 41 | # orgs... |
| 42 | |
| 43 | existing_code = session.query(Signup_code).filter( |
| 44 | Signup_code.email_sent_to == email_sent_to, |
| 45 | Signup_code.type == auth_code_type, |
| 46 | Signup_code.is_available != False, |
| 47 | ).first() |
| 48 | if existing_code: |
| 49 | if existing_code.type == "magic_login": |
| 50 | |
| 51 | # If code is still valid return code |
| 52 | # Careful not < > sign flipped vs checking if valid |
| 53 | # The <= is INVERTED on purpose here since |
| 54 | # We are retruning if the code IF it's still valid |
| 55 | # TODO refactor this into a generic check is valid function |
| 56 | if time.time() <= existing_code.created_time_int + 900: |
| 57 | return False, "Existing code", existing_code |
| 58 | |
| 59 | # Else invalidate code, and continue to create new one |
| 60 | else: |
| 61 | session.add(existing_code) |
| 62 | existing_code.is_available = False |
| 63 | |
| 64 | auth = Signup_code() |
| 65 | session.add(auth) |
| 66 | |
| 67 | auth.type = auth_code_type |
| 68 | auth.project_string_id = project_string_id |
| 69 | auth.permission_level = permission_level |
| 70 | auth.email_sent_to = email_sent_to |
| 71 | auth.created_time_int = time.time() |
| 72 | |
| 73 | # if auth_type: |
nothing calls this directly
no test coverage detected